import paramiko import sys def sshExecmd(hostname,port,username,password,cmd): ssh_client=paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh_client.connect(hostname,port,username,password) except Exception as e: print(e) sys.exit() stdin,stdout,stderr=ssh_client.exec_command(cmd); print(stdout.read().decode(encoding='utf-8'),end='') ssh_client.close() if __name__ == '__main__': servers={ "192.168.1.3":{ 'username':'root', 'passwd':'123456', 'port':22 } } cmd='df -h' for ip,info in servers.items(): sshExecmd(hostname=ip,port=info.get('port'),username=info.get('username'),password=info.get('passwd'),cmd=cmd)
运行结果:
0.0分
0 人评分