使用Python Paramiko通过SSH将输入/变量传递给命令/脚本 [英] Pass input/variables to command/script over SSH using Python Paramiko
问题描述
我在通过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
你叫什么名字?
What is your name:
,而且我似乎找不到一种方法将响应从我的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 + '\n')
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屋!