Python PEXPECT 中的循环线
Looping lines in Python PEXPECT
我创建了一个名为 "commands.txt" 的 txt 文件,它包含两个命令(2 行)
sh run vlan 158
sh run vlan 159
我想 运行 这两个命令在一个开关中,但只有一个命令得到 executed.here 我的脚本是这样的:
def shvlan(device,child):
commandFile = open('commands.txt', 'r')
commands = [i for i in commandFile]
for command in commands:
child.sendline(command)
child.expect('.*#')
vlans = child.after
child.close()
print device + ' conn closed'
print 'sh run vlan executed'
print vlans
return vlans
为什么它只占用 .txt 文件的第一行有什么建议吗?
您正在关闭循环内的连接。您必须在循环之后移动那部分代码。尝试这样的事情
def shvlan(device,child):
vlans = []
with open('commands.txt', 'r') as commands:
for command in commands:
child.sendline(command)
child.expect('.*#')
vlans.extend(child.after.splitlines())
child.close()
print device + ' conn closed'
print 'sh run vlan executed'
print vlans
return vlans
我创建了一个名为 "commands.txt" 的 txt 文件,它包含两个命令(2 行)
sh run vlan 158
sh run vlan 159
我想 运行 这两个命令在一个开关中,但只有一个命令得到 executed.here 我的脚本是这样的:
def shvlan(device,child):
commandFile = open('commands.txt', 'r')
commands = [i for i in commandFile]
for command in commands:
child.sendline(command)
child.expect('.*#')
vlans = child.after
child.close()
print device + ' conn closed'
print 'sh run vlan executed'
print vlans
return vlans
为什么它只占用 .txt 文件的第一行有什么建议吗?
您正在关闭循环内的连接。您必须在循环之后移动那部分代码。尝试这样的事情
def shvlan(device,child):
vlans = []
with open('commands.txt', 'r') as commands:
for command in commands:
child.sendline(command)
child.expect('.*#')
vlans.extend(child.after.splitlines())
child.close()
print device + ' conn closed'
print 'sh run vlan executed'
print vlans
return vlans