Почему я получаю "Обнаружен неверный ввод" для этой строки tn.write ("IP-адрес 1.1.1.1 255.255.255.255")? - PullRequest
0 голосов
/ 19 февраля 2020

Так что этот скрипт предназначен для lnet подключения к маршрутизатору и изменения 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>

Есть идеи, почему я получаю эту недопустимую ошибку ввода? Спасибо заранее!

...