第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用Python Paramiko通過(guò)SSH將輸入/變量傳遞給命令/腳本

使用Python Paramiko通過(guò)SSH將輸入/變量傳遞給命令/腳本

瀟湘沐 2019-12-06 11:02:13
我在通過(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è)您知道該怎么做)。


查看完整回答
反對(duì) 回復(fù) 2019-12-06
  • 1 回答
  • 0 關(guān)注
  • 1026 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)