它在 CentOS Linux 版本 7.6.1810(核心)系統(tǒng)上運(yùn)行 Python 2.7.5。我沒有太多使用 Python 的經(jīng)驗(yàn)。但我已經(jīng)查看了之前的一些問題/示例,但無法弄清楚如何繼續(xù)處理這個(gè)問題。我無法從 Python 腳本運(yùn)行 Linux ssh 命令。但是,當(dāng)我運(yùn)行 Linux echo 命令時(shí),我得到了預(yù)期的結(jié)果。我將提供代碼和兩個(gè)輸出:#!/bin/pythonimport sysimport subprocesstunnelUser='testuser'localIP = []localPort = []remoteIP = []remotePort = []x = 0vfile=open('/home/'+tunnelUser+'/development/scripts/port-dummy-data','rt')for line in vfile: columns = line.split(',') localIP.append(columns[0]) localPort.append(columns[1]) remoteIP.append(columns[2]) remotePort.append(columns[3]) sshCommand = " -f -N "+tunnelUser+"@"+localIP[x]+" -R "+localPort[x]\ +":"+remoteIP[x]+":"+remotePort[x] subprocess.Popen(['/bin/ssh', sshCommand]) # subprocess.Popen(['/bin/echo', sshCommand]) x=x+1sys.stdout.close()產(chǎn)生以下負(fù)面結(jié)果:prompt > ./create-tunnels.py prompt > ssh: Could not resolve hostname localhost -r 8080:192.168.1.3:8080: Name or service not knownssh: Could not resolve hostname localhost -r 8090:192.168.1.11:8090: Name or service not knownssh: Could not resolve hostname localhost -r 8081:192.168.1.5:8081: Name or service not knownssh: Could not resolve hostname localhost -r 8085:192.168.1.9:8085: Name or service not knownssh: Could not resolve hostname localhost -r 8095:192.168.1.12:8095: Name or service not known然而,當(dāng)我“注釋掉” ssh 并“取消注釋” echo 時(shí),我得到了預(yù)期的結(jié)果:prompt > ./create-tunnels.py -f -N testuser@localhost -R 8080:192.168.1.3:8080 -f -N testuser@localhost -R 8081:192.168.1.5:8081 -f -N testuser@localhost -R 8085:192.168.1.9:8085 -f -N testuser@localhost -R 8090:192.168.1.11:8090 -f -N testuser@localhost -R 8095:192.168.1.12:8095我相信由于缺乏 Python 經(jīng)驗(yàn),我錯(cuò)過了一些簡(jiǎn)單的東西。任何幫助,將不勝感激。
1 回答

忽然笑
TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
的各個(gè)參數(shù)應(yīng)Popen()
作為列表傳遞,即類似
Popen(['/bin/ssh', '-f', '-N', tunnelUser+"@"+localIP[x], '-R', ...])
但相反,您將參數(shù)作為一個(gè)巨大的字符串傳遞。因此它試圖將整個(gè)文字字符串解釋localhost -r 8080:192.168.1.3:8080
為一個(gè)主機(jī)名。
-r
另外,當(dāng)代碼將其設(shè)置為 時(shí),為什么不成功的輸出塊包含該標(biāo)志-R
?
添加回答
舉報(bào)
0/150
提交
取消