ssh 执行命令并实时显示结果

时间:2023-03-10 04:42:27
ssh 执行命令并实时显示结果

ssh 执行命令并实时显示结果

import paramiko

def main():
sshClient = paramiko.SSHClient()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshClient.connect(hostname = "192.168.1.104", port = 22, username = "usr", password = "pwd") shell = sshClient.invoke_shell()
shell.sendall("ping www.baidu.com -c 3\n")
shell.sendall("exit\n")
while True:
data = shell.recv(2048).decode()
if not data:
print("quit now")
break
print(data, end = "")
sshClient.close() if __name__ == '__main__':
main()