Python 在第一个命令上执行以收集特定接口并添加到列表中。第二个命令获取列表项(接口)以运行命令。不确定如何让第二个命令一个一个地抓取列表项。我在想我需要利用 for 循环来完成这个吗?任何帮助将不胜感激。请参阅下面的 Python 代码和从 Jumpstation 执行时产生的输出。谢谢!
Python Code:
#!/usr/bin/env python3.4
from os.path import expanduser
import sys
import subprocess
#define Function
# while loop to provide user input
def get_user_input(prompt):
while True:
try:
value = input(prompt)
if not value:
raise ValueError("You must provide a valid response to continue!")
except ValueError as error:
print(error)
continue
else:
break
return value
#define variables
home = expanduser("~")
logincfg = home+"/.cloginrc"
source_device = input("Please enter network TID (e.g. tamqflpmc6002 or tamrflws4a001): ")
hub_vlans = home+"/"+"hub_vlans"+".cfg"
#let's pull down configuration from TID so script can extract and verify configuration
cmd_one = "show interfaces description | include METRO - Ring"
#run cmd against TID with clogin
output=subprocess.check_output(['clogin', '-f', logincfg, '-c', cmd_one, source_device])
file=open(home+"/"+"hub_vlans"+".cfg", "w")
for i in output.splitlines():
i=i.decode("utf-8")
file.write(i+"\n")
file.close
#let's grab a list of only the RING interfaces and put them into a list to determine VLANs configured
ring_int_list = []
with open(hub_vlans) as file_out:
for interface in file_out:
if interface.startswith("Gi") or interface.startswith("Te"):
i=interface.split(" ")
print(i[0])
ring_int_list.append(i[0])
print()
print('-' * 232)
#print ring inteface list
print(ring_int_list)
#create a second list to utilize for second command
interface_list = ring_int_list
cmd_two = "show run interface " + str(interface_list)
#let's utilie interface_list to run second command against for VLANs
# for int in list:
# for interface in interface_list:
# if ()
jumpserver 的 Python 输出: [jsanch24@nlan scripts]$ "1.0 hub vlan script.py" /home/jsanch24/.cloginrc Gi2/10 Gi2/11 Gi2/13 Gi2/14 Gi2/15 Gi2/16 Gi2/17 Gi2/18 Gi2/19 Gi2/20 Gi2/21 Gi2/22 Gi2/23 Gi2/24 GI3/10 GI3/11 GI3/13 GI3/14 GI3/15 Gi3/16 GI3/17 GI3/18 Gi3/19 GI3/20 GI3/21 Gi3/22 Gi3/23 Gi3/24
------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------
['Gi2/10', 'Gi2/11', 'Gi2/13', 'Gi2/14', 'Gi2/15', 'Gi2/16', 'Gi2/17', 'Gi2/18', 'Gi2/19', 'Gi2/20', 'Gi2/21', 'Gi2/22', 'Gi2/23', 'Gi2/24', 'Gi3/10', 'Gi3/11', 'Gi3/13', 'Gi3/14', 'Gi3/15', 'Gi3/16', 'Gi3/17', 'Gi3/18', 'Gi3/19', 'Gi3/20', 'Gi3/21', 'Gi3/22', 'Gi3/23', 'Gi3/24']