我想在我们的设备中自动创建vlan,但我的脚本会抛出此错误:
tn.write(cmd + "\n") TypeError: can only concatenate tuple (not "str") to tuple.
vlannumbers = int(input("Enter the number for vlans :"))
for i in range(2,vlannumbers):
cmd = (("/cfg/l2/vlan "), i)
tn.write(cmd + "\n")
cmd1 = "apply"
tn.write(cmd1 + "\n")
print "ok"
tn.close()
答案 0 :(得分:0)
cmd
在代码中保存元组值(不是字符串)。例如:
>>> cmd = (("/cfg/l2/vlan "), 2)
>>> type(cmd)
<type 'tuple'> # <--- it's tuple
由于你想把它初始化为字符串,你应该这样做:
cmd = "/cfg/l2/vlan {}".format(i)
答案 1 :(得分:0)
字符串连接是string
类型的对象之间的连接。
你可以试试这个:
vlannumbers = int(input("Enter the number for vlans :"))
cmd_base = "/cdg/l2/vlan"
for i in range(2,vlannumbers):
# cmd_base is not affected,
# since concatenated string value is stored in `cmd`
cmd = cmd_base + "%d"%i
tn.write(cmd + "\n")
cmd1 = "apply"
tn.write(cmd1 + "\n")
print "ok"
tn.close()