import paramikoimport sysimport oshostname = '192.168.1.202'port = 22username = 'root'password = '123456' def shells(args): #从外部调用脚本参数 例如: python paramiko_demo.py ls ifconfig #开启日志 paramiko.util.log_to_file('paramiko.log') s = paramiko.SSHClient() #s.load_host_keys() #不适用公钥登陆 s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(hostname, port, username, password) #print(commds()) for i in args: print(i) #打印循环好的列表 ls,ifconfig stdin, stdout, stderr = s.exec_command(i) #分别执行这两个命令 print(stdout.read().decode()) s.close() if __name__ == '__main__': args=sys.argv #args=args shells(args)