Search code examples
pythonpython-2.7pexpect

PExpect:: Numbers inside while loop is not getting incremented... throws type() error


Following is my Pexpect Script:

import pexpect

child = pexpect.spawn('telnet 10.1.1.1')
child.logfile = sys.stdout    
child.expect(":")
child.send("admin" + "\r")
child.expect(":")
child.send("test" + "\r")
child.send("\r\n")
child.expect(">")
child.send("enable" + "\r")
child.expect("#")
child.send("self" + "\r")
child.expect("#")
n=1
while (n < 5):
    #n1 = float(n)
    child.send("interface vlan" (float (n)) + "\r")    # ISSUE IS HERE
    child.expect("#")
    n = n+1
child.expect("#")

I am using the above script, to configure number of VLAN's in the router. When I run this script, I am getting "Trace back error ... type(str) cant be concatenated". I tried to convert this to "int()" and "float()". But its not working.

I want to send the following lines in "child.expect()":

interface vlan 1

interface vlan 2

interface vlan 3

interface vlan 4

interface vlan 5

 I am trying to use while loop to  print number from 1 to 5. 

Please advice me... if you have any ideas to resolve this issue.

Thanks,

Kumar.


Solution

  • You are missing a % after "interface vlan" along with the format specifier. That line should look like this:

    child.send("interface vlan %d" % (n) + "\r")