所以这个脚本的目的是远程登录到路由器并更改给定接口上的 IP 地址。 但是,我的脚本遇到了错误,我不知道为什么。出错的行是第 44 行。
这是我的Python脚本:
import os
import sys
import telnetlib
if (len(sys.argv) != 3):
print "syntax: python hw06.py <device> <newip>"
sys.exit()
router = sys.argv[1]
newip = sys.argv[2]
interface = "Serial0/0" # must hard code the interface to avoid disaster
TIMEOUT = 3
password1 = "user"
password2 = "cisco"
cmd = "ip address 111.11.111.11 255.255.255.0"
# 1. create a telnet object
tn = telnetlib.Telnet(router, timeout=TIMEOUT)
# 2. login/telnet to the router
tn.read_until("Password: ", TIMEOUT)
tn.write(password1 + "\n")
# 3. enter into the privilege mode
tn.write("enable\n")
tn.read_until("Password:")
tn.write(password2 + "\n")
# 4. enter into the configuration mode
tn.write("configure terminal\n")
tn.read_until("(config)#", TIMEOUT)
# 5. enter into the interface configuration mode
tn.write("int" + interface + "\n")
tn.read_until("(config-if)#", TIMEOUT)
# 6. set the new IP address
tn.write(cmd + "\r\n")
# 7. exit
# exit from the interface configruaiton mode
tn.write("exit\n")
# exit from the configuraiotn mode
tn.write("exit\n")
# exit from the privilege mode
tn.write("exit\n")
print tn.read_all() # this line is required, but not sure why?
tn.close()
oid = ".1.3.6.1.2.1.4.20.1.1"
snmp = "snmpwalk -v2c -c public %s %s" % (router, oid)
# Verify the output via SNMP
fp = os.popen( snmp )
snmp = fp.read().splitlines() # split the outout into a list of "lines"
flag = 0
for line in snmp:
inline = line.rstrip('\n')
list = inline.split()
ip = list[3] # IP address is the 4th item on the list
if ip == newip:
print "The new IP address (%s) is successfully configured on Serial0/0 of %s" % (ip, router)
flag = 1
break
if flag == 0:
print "failed operation: %s is not configured on Serial0/0 of %s" % (newip, router)
现在,当我运行脚本时,我输入“python script.py deviceIPaddress newInterfaceIPaddress”,这就是我得到的:
ip address 111.11.111.11 255.255.255.0
^
% Invalid input detected at '^' marker.
Router4(config)#exit
Router4#exit
failed operation: 111.11.111.11 is not configured on Serial0/0 of <device>
知道为什么我会收到无效输入错误吗? 提前谢谢您!
需要注意的几句话...
telnet
,因为通过 telnet 发送密码是不安全的。netmiko
非常有用。虽然看起来您已经找到了具体问题,但使用
netmiko
... 更容易做到这一点
from netmiko import ConnectHandler
router = sys.argv[1]
newip = sys.argv[2]
interface = "Serial0/0" # must hard code the interface to avoid disaster
password1 = "user" # I'm keeping this insecure value here for example
password2 = "cisco" # I'm keeping this insecure value here for example
cmd = "ip address 111.11.111.11 255.255.255.0"
cisco_01 = {
"device_type": "cisco_ios",
"host": router,
"username": "admin",
"password": password1,
"secret": password2 # Enable password
}
connection = ConnectHandler(**cisco_01)
prompt = connection.find_prompt()
if '>' in prompt:
connection.enable() # Enable mode only 'if' '>' is present in the prompt
connection.config_mode() # Global config mode
connection.send_command(f'interface {interface}')
connection.send_command(cmd)
connection.send_command('end')
print('Closing Connection')
connection.disconnect()
如你所见,这是:
telnetlib
容易得多,需要大量的战术工作,例如手动提示处理才能完成此任务。