簡體   English   中英

將使用 Paramiko SSH 執行的遠程命令的輸入鏈接到本地​​ Python 控制台

[英]Linking input of remote command executed with Paramiko SSH to local Python console

我正在使用 Python 使用 Paramiko ssh 到遠程服務器並運行 shell 腳本來執行命令

.py 文件:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password)
print("Connected")
stdin, stdout, stderr = ssh.exec_command(myscript)

# read the standard output and print it
print(stdout.read().decode())
# print errors if there are any
err = stderr.read().decode()
if err:
    print(err)
ssh.close()

Unix 腳本:

cd /usr/data
pwd
read -r username  
echo "$username"

輸出將是:

/usr/data

它不允許輸入用戶名變量的輸入並且不顯示它。

如何使用 Python (pycharm ide) 和 Unix 實現這一點?

您可能正在尋找:

stdin, stdout, stderr = ssh.exec_command(myscript)

interactive_shell(stdin.channel)

其中interactive_shell來自Paramiko interactive.py demo

暫無
暫無

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

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