import sys import os import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) def ssh_cmd(user,passwd,port,userfile,cmd): check_ok=[] check_er=[] file = open(userfile, "r") line = file.readlines() file.close() for i in range(len(line)): print("============================================>" + line[i].strip('\n') + "<============================================" ) try: ssh.connect(hostname=line[i].strip('\n'),port=port,username=user,password=passwd) cmd=cmd stdin, stdout, stderr = ssh.exec_command(cmd) result = stdout.read() if not result: result=stderr.read() ssh.close() print(result.decode()) check_ok.append(line[i].strip('\n')) except Exception: check_er.append(line[i].strip('\n')) continue print("========================================================================================================") print("主机IP \t\t\t 执行端口 \t\t\t 命令 \t\t\t状态 \t\t\t \n") for i in range(len(check_ok)): print(check_ok[i] + "\t\t\t" + port + "\t\t\t" + cmd + "\t\t [完成]") print("========================================================================================================\n") for i in range(len(check_er)): print(check_er[i] + "\t\t\t" + port + "\t\t\t" + cmd + "\t\t [失败]") print("\n========================================================================================================\n") def ssh_help(): print("========================================================================================================\n\n") print("By:LyShark Blogs: http://cnblogs.com/lyshark \n\n") print(" clear 清屏\n\n") print(" help 帮助 \n\n") print("瑞王 更新中....\n\n") print("========================================================================================================\n\n") def ssh_clear(): os.system("cls") while True: shell=str(input("LyShark--> ")) if (shell == ""): continue elif (shell =="help"): ssh_help() elif (shell == "clear"): ssh_clear() elif (shell== "exit"): exit() else: ssh_cmd("root","123123","22","./ip.txt",shell)
运行结果: