import threading import paramiko import subprocess def ssh_command(ip,user,passwd,command): client=paramiko.SSHClient() #client.load_host_keys("filename")使用密钥验证 #设置自动添加和保存ssh服务器的ssh密钥 client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(ip,username=user,password=passwd) ssh_session=client.get_transport().open_session() if ssh_session.active: #调用ssh_comand函数运行command ssh_session.exec_command(command) a=ssh_session.recv(1024) return a a=ssh_command("192.168.6.128",‘root‘,‘123456‘,‘ls -lh‘) print(a.decode("gbk"))
时间: 2024-10-21 13:45:09