使用 Python Paramiko 通过 SSH 将输入/变量传递给命令/脚本 [英] Pass input/variables to command/script over SSH using Python Paramiko

查看:54
本文介绍了使用 Python Paramiko 通过 SSH 将输入/变量传递给命令/脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 SSH 将响应传递给远程服务器上的 bash 脚本时遇到问题.

I am having issues passing responses to a bash script on a remote server over SSH.

我正在用 Python 3.6.5 编写一个程序,它将通过 SSH 连接到远程 Linux 服务器.在这个远程 Linux 服务器上,我正在运行一个 bash 脚本,它需要用户输入才能填写.无论出于何种原因,我都无法通过 SSH 从我的原始 python 程序传递用户输入,并让它填写 bash 脚本用户输入问题.

I am writing a program in Python 3.6.5 that will SSH to a remote Linux server. On this remote Linux server there is a bash script that I am running which requires user input to fill in. For whatever reason I cannot pass a user input from my original python program over SSH and have it fill in the bash script user input questions.

main.py

from tkinter import *
import SSH

hostname = 'xxx'
username = 'xxx'
password = 'xxx'

class Connect:
    def module(self):
        name = input()
        connection = SSH.SSH(hostname, username, password)
        connection.sendCommand(
            'cd xx/{}/xxxxx/ && source .cshrc && ./xxx/xxxx/xxxx/xxxxx'.format(path))

SSH.py

from paramiko import client

class SSH:

    client = None

    def __init__(self, address, username, password):
        print("Login info sent.")
        print("Connecting to server.")
        self.client = client.SSHClient()    # Create a new SSH client
        self.client.set_missing_host_key_policy(client.AutoAddPolicy())
        self.client.connect(
            address, username=username, password=password, look_for_keys=False) # connect

    def sendCommand(self, command):
        print("Sending your command")
        # Check if connection is made previously
        if (self.client):
            stdin, stdout, stderr = self.client.exec_command(command)
            while not stdout.channel.exit_status_ready():
                # Print stdout data when available
                if stdout.channel.recv_ready():
                    # Retrieve the first 1024 bytes
                    alldata = stdout.channel.recv(1024)
                    while stdout.channel.recv_ready():
                        # Retrieve the next 1024 bytes
                        alldata += stdout.channel.recv(1024)


                    # Print as string with utf8 encoding
                    print(str(alldata, "utf8"))
        else:
            print("Connection not opened.")

Connect 类中的最后一个 /xxxxxx 是启动的远程脚本.它将打开一个等待格式的文本响应,例如

The final /xxxxxx in class Connect is the remote script that is launched. It will open a text response awaiting a format such as

你叫什么名字:

而且我似乎无法找到一种方法将响应从我的 main.py 文件中的 Connect 类正确传递给脚本.

and I cannot seem to find a way to properly pass the response to the script from my main.py file within the class Connect.

我尝试将 name 作为参数或变量传递的每一种方式,答案似乎都消失了(可能是因为它试图在 Linux 提示符下而不是在 bash 脚本中打印它)

Every way I have tried to pass name as an argument or a variable the answer seems to just disappear (likely since it is trying to print it at the Linux prompt and not within the bash script)

我认为使用 read_until 函数查找问题末尾的 : 可能有效.

I think using the read_until function to look for the : at the end of the question may work.

建议?

推荐答案

将您的命令所需的输入写入stdin:

Write the input that your command needs to the stdin:

stdin, stdout, stderr = self.client.exec_command(command)
stdin.write(name + '
')
stdin.flush()

<小时>

(您当然需要将 name 变量从 module 传播到 sendCommand,但我假设您知道如何做那部分).


(You will of course need to propagate the name variable from module to sendCommand, but I assume you know how to do that part).

这篇关于使用 Python Paramiko 通过 SSH 将输入/变量传递给命令/脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆