0

So this script is meant to telnet into a router and change the IP address on the given interface. However, my script runs into errors and I'm not sure why. The line that errors out is line 44.

This is my python script:

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)

Now when I run the script, i input "python script.py deviceIPaddress newInterfaceIPaddress" this is what i get:

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>

Any idea why I'm getting that invalid input error? Thank you in advanced!

angelstra
  • 13
  • 5
  • After every command, you've included `\n` for newline, but you've written `\r\n` for the `ip address` command. Why? Have you tested with just `\n`? – esqew Feb 19 '20 at 01:10
  • hi! i have tried it with just ```\n``` as well and i get the same result. I use the ```\r\n``` because that is how the original script had it (this is homework and am allowed to use parts of a previous script). However when i look up why it is necessary, i found this: ```The '\r' character is the carriage return, and the carriage return-newline pair is both needed for newline in a network virtual terminal session.``` – angelstra Feb 19 '20 at 01:16
  • 1
    The "Invalid input" message is coming *from the router*. I suspect that you aren't in the right mode to issue that command, due to the preceding `tn.write("int" + interface + "\n")` - note that there's no space between the `int` command and the interface name, which I suspect makes it an invalid command. – jasonharper Feb 19 '20 at 01:25
  • thank you jasonharper! that was exactly the issue. Now when i added the space, it looks like ```tn.write("int" + " " + interface + "\n")``` and it runs as it should. Thank you for pointing that out! It works as planned now. – angelstra Feb 19 '20 at 01:30

0 Answers0