簡體   English   中英

SSH客戶端與paramiko

[英]ssh client with paramiko

import paramiko
import os

def connection():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    privatekey = os.path.expanduser('/home/rabia/private')
    mkey = paramiko.RSAKey.from_private_key_file(privatekey)
    ssh.connect('78.46.172.47', port=22, username='s0urd', password=None, pkey=mkey)
    stdin, stdout, stderr = ssh.exec_command('ls')
    print stdout.readlines()

connection()

我如何才能使一個線程正在等待用戶輸入,而另一個正在進行ssh連接呢?

如果我對您的理解正確,則應在代碼中添加以下內容:

import threading

_paramikoThread = threading.Thread(target=doParamikoConnection)
_paramikoThread.start()
# The following code is executed by parent thread.
_ans = ""
while _ans != "nay":
    _ans = raw_input("Should I loop again? [yay/nay]")
# Now we've ended user I/O session, wait for connection to complete.
_paramikoThread.join()
# At this point we have data collected from user and connection being initialised.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM