我是python的初学者,我通过(for x in range (1,3) :
)循环通过telnet访问两个路由器R1和R2,我想在同一循环中包括每个路由器示例的环回接口:
R1仅具有接口环回1 1.1.1.1 255.255.255.255
R2仅具有接口环回2 2.2.2 255.255.255.255
我成功创建了回送接口,但无法为每个接口分配IP地址。
import getpass
import sys
import telnetlib
user = raw_input("Enter your Telnet Username: ")
password = getpass.getpass()
for n in range (1,3):
HOST = "10.1.1." + str(n)
tn = telnetlib.Telnet(HOST)
tn.read_until("Username: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("config t\n")
tn.write("int l " + str(n) +"\n")
tn.write("ip add str(n) . str(n) . str(n) . str(n) 255.255.255.255 \n")
tn.write("router eigrp 1\n")
tn.write("net 0.0.0.0\n")
tn.write("end\n")
tn.write("sh ip int br\n")
tn.write("exit\n")
print tn.read_all()
谢谢
答案 0 :(得分:0)
结果:
tn.write("ip add str(n) . str(n) . str(n) . str(n) 255.255.255.255 \n")
将是一个字符串:ip add str(n)。 str(n)。 str(n)。 str(n)255.255.255.255
将该行更改为:
tn.write("ip add " + str(n) + "." + str(n) + "." + str(n) + "." + str(n) + " 255.255.255.255 \n")