sys.argv
一个列表,其中包含了被传递给 Python 脚本的命令行参数。 argv[0] 为脚本的名称(是否是完整的路径名取决于操作系统)。如果是通过 Python 解释器的命令行参数 -c 来执行的, argv[0] 会被设置成字符串 '-c' 。如果没有脚本名被传递给 Python 解释器, argv[0] 为空字符串。
为了遍历标准输入,或者通过命令行传递的文件列表,参照 fileinput 模块
import paramiko
import time
import getpass
import sys
username = input("Username: ")
password = getpass.getpass("Password: ")
ip_file = sys.argv[1]
cmd_file = sys.argv[2]
iplist = open(ip_file,'r')
for line in iplist.readlines():
ip = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
print('已经成功登陆交换机 ' + ip)
command = ssh_client.invoke_shell()
cmdlist = open(cmd_file, 'r')
cmdlist.seek(0)
for line in cmdlist.readlines():
each_command = line.strip()
command.send(each_command + 'n')
time.sleep(5)
cmdlist.close()
output = command.recv(65535).decode('ASCII')
print(output)
ssh_client.close()
E:logpythonpythonProjecttestdemo>dir 驱动器 E 中的卷是 文件 卷的序列号是 FE2A-789B E:logpythonpythonProjecttestdemo 的目录 2022/05/31 11:34. 2022/05/31 11:34 .. 2022/05/31 11:34 1,416 Demo05.py 2022/05/31 11:06 11 g1_comm.txt 2022/05/31 11:06 13 g1_ip.txt 2022/05/31 11:06 11 g2_comm.txt 2022/05/31 11:06 28 g2_ip.txt 2022/05/31 11:20 908 TestDemo04.py 6 个文件 2,387 字节 2 个目录 124,421,648,384 可用字节 E:logpythonpythonProjecttestdemo> python TestDemo01.py g1_ip.txt g1_comm.txt Username: python Password: =-=-=-=-=-=-=-=-=-=-=-=-=-= 已经成功登陆交换机 192.168.8.201 Info: The max number of VTY users is 5, and the number of current VTY users on line is 1. The current login time is 2022-05-31 11:21:03. sys Enter system view, return user view with Ctrl+Z. [Huawei]dis cu # sysname Huawei # cluster enable ……



