jseiser
April 11th, 2008, 05:16 PM
With the help of these forums, a little while ago I wrote a small code snippet in python, that telnets to our company's router, and runs 2 commands, and then shows me the output. This is fine, and program runs how I wanted. The only thing is, when a customer calls, I need to check their 'vci' number ( 1/302, 2/250) in the router, and the way my program is set up, i have to transform 1/250, into 101250 myself, then run the command. I wanted to set up the program to take a vci of 1/250 and have it check that the first part of the string is a 1, so take it, add a "01" to it, and then slap the rest of the vci back on the number, so the program will now convert for me. To do this, I needed to set up some error checking, and check which number Im given, and then format it. I tried to do this, and Im getting invalid syntax and indentation errors. I though that anything after the If statement just needs to be indented more then the if statement, I obviously have something wrong, here is the orginial code, then what i tried to add.
import sys
import telnetlib
while True:
HOST = "000.000.220.120"
password = "password"
vpc = raw_input("Enter The VPC Number: ")
tn = telnetlib.Telnet(HOST)
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("sh int atm6/0." + str(vpc) + "\n")
tn.write("sh arp | in " + str(vpc) + "\n")
tn.write("exit\n")
print tn.read_all()
Here is what i added, that doesnt work.
import sys
import telnetlib
while True:
HOST = 000.000.220.120"
password = "password"
vpc = raw_input("Enter The VPC Number: ")
If vpc[:1] = '1': # check first number
vpc = vpc[:1] + '01' + vpc[3:]
elif vpc[:1] = '2': # if its not a 1, check for a 2
vpc = vpc[:1] + '01' + vpc[3:]
elif vpc[:1] = '3': # if its neither 1 nor 2, check for 3, error if its 3
vpc = vpc[:1] + '01' + vpc[3:]
print ' Wrong Server ( 3/ VPI )'
time.sleep(5)
sys.exit(1)
elif vpc[:1] = > 3 or < 0: # if the first num is neg, or >3, error
print 'Invalid VPI'
time.sleep(5)
sys.exit(1)
tn = telnetlib.Telnet(HOST) # if its a 1 or 2, telnet & run cmd.
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("sh int atm6/0." + str(vpc) + "\n")
tn.write("sh arp | in " + str(vpc) + "\n")
tn.write("exit\n")
print tn.read_all()
Where am i going wrong? the code itself might be wrong (probably) but what is the proper 'setup' of that file? I want to get this part working, so i can make a simple gui. Thanks all
import sys
import telnetlib
while True:
HOST = "000.000.220.120"
password = "password"
vpc = raw_input("Enter The VPC Number: ")
tn = telnetlib.Telnet(HOST)
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("sh int atm6/0." + str(vpc) + "\n")
tn.write("sh arp | in " + str(vpc) + "\n")
tn.write("exit\n")
print tn.read_all()
Here is what i added, that doesnt work.
import sys
import telnetlib
while True:
HOST = 000.000.220.120"
password = "password"
vpc = raw_input("Enter The VPC Number: ")
If vpc[:1] = '1': # check first number
vpc = vpc[:1] + '01' + vpc[3:]
elif vpc[:1] = '2': # if its not a 1, check for a 2
vpc = vpc[:1] + '01' + vpc[3:]
elif vpc[:1] = '3': # if its neither 1 nor 2, check for 3, error if its 3
vpc = vpc[:1] + '01' + vpc[3:]
print ' Wrong Server ( 3/ VPI )'
time.sleep(5)
sys.exit(1)
elif vpc[:1] = > 3 or < 0: # if the first num is neg, or >3, error
print 'Invalid VPI'
time.sleep(5)
sys.exit(1)
tn = telnetlib.Telnet(HOST) # if its a 1 or 2, telnet & run cmd.
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("sh int atm6/0." + str(vpc) + "\n")
tn.write("sh arp | in " + str(vpc) + "\n")
tn.write("exit\n")
print tn.read_all()
Where am i going wrong? the code itself might be wrong (probably) but what is the proper 'setup' of that file? I want to get this part working, so i can make a simple gui. Thanks all