Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python Paramiko通过SSH将输入/变量传递给命令/脚本_Python_Linux_Bash_Ssh_Paramiko - Fatal编程技术网

使用Python Paramiko通过SSH将输入/变量传递给命令/脚本

使用Python Paramiko通过SSH将输入/变量传递给命令/脚本,python,linux,bash,ssh,paramiko,Python,Linux,Bash,Ssh,Paramiko,我在通过SSH将响应传递到远程服务器上的bash脚本时遇到问题 我正在用Python 3.6.5编写一个程序,该程序将SSH连接到远程Linux服务器。 在这个远程Linux服务器上,我正在运行一个bash脚本,它需要用户输入来填充。无论出于何种原因,我无法通过SSH传递来自原始python程序的用户输入,并让它填写bash脚本用户输入问题 main.py from tkinter import * import SSH hostname = 'xxx' username = 'xxx' pa

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

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

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
是启动的远程脚本。 它将打开一个文本响应,等待以下格式

你叫什么名字

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

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

我认为使用
read_until
函数查找问题末尾的
可能会起作用


建议?

将命令需要的输入写入
stdin

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


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

将命令所需的输入写入
stdin

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


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

这很有效,非常感谢!我不知道。为stdinthis编写函数很有效,非常感谢!我不知道stdin的.write函数