我在通過(guò)SSH將響應(yīng)傳遞到遠(yuǎn)程服務(wù)器上的bash腳本時(shí)遇到問(wèn)題。我正在用Python 3.6.5編寫程序,該程序?qū)SH到遠(yuǎn)程Linux服務(wù)器。在此遠(yuǎn)程Linux服務(wù)器上,有一個(gè)正在運(yùn)行的bash腳本,需要用戶輸入才能填充。無(wú)論出于何種原因,我都無(wú)法通過(guò)SSH傳遞來(lái)自原始python程序的用戶輸入,并使其填充bash腳本用戶輸入問(wèn)題。main.pyfrom tkinter import *import SSHhostname = '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.pyfrom paramiko import clientclass 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.")/xxxxxx類Connect中的最后一個(gè)是啟動(dòng)的遠(yuǎn)程腳本。它將打開(kāi)一個(gè)文本響應(yīng),等待諸如你叫什么名字:而且我似乎無(wú)法找到一種方法將響應(yīng)從我main.py的類文件中正確傳遞到腳本Connect。我嘗試以任何方式name作為參數(shù)或變量傳遞的答案似乎都消失了(可能是因?yàn)樗噲D在Linux提示符下而不是在bash腳本中打印它)我認(rèn)為使用read_until函數(shù):在問(wèn)題末尾查找可能有效。有什么建議嗎?
1 回答

慕仙森
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
將命令所需的輸入寫到stdin:
stdin, stdout, stderr = self.client.exec_command(command)
stdin.write(name + '\n')
stdin.flush()
(您當(dāng)然需要將name變量從傳播module到sendCommand,但我假設(shè)您知道該怎么做)。
添加回答
舉報(bào)
0/150
提交
取消