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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM