【问题标题】:Linking input of remote command executed with Paramiko SSH to local Python console将使用 Paramiko SSH 执行的远程命令的输入链接到本地​​ Python 控制台
【发布时间】:2021-02-28 21:05:08
【问题描述】:

我正在使用 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 实现这一点?

【问题讨论】:

    标签: python shell unix ssh paramiko


    【解决方案1】:

    您可能正在寻找:

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

    interactive_shell 来自Paramiko interactive.py demo

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多