它在 CentOS Linux 版本 7.6.1810(核心)系统上运行 Python 2.7.5。我没有太多使用 Python 的经验。但我已经查看了之前的一些问题/示例,但无法弄清楚如何继续处理这个问题。我无法从 Python 脚本运行 Linux ssh 命令。但是,当我运行 Linux echo 命令时,我得到了预期的结果。我将提供代码和两个输出:#!/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()产生以下负面结果: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然而,当我“注释掉” ssh 并“取消注释” echo 时,我得到了预期的结果: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 经验,我错过了一些简单的东西。任何帮助,将不胜感激。
1 回答
忽然笑
TA贡献1806条经验 获得超5个赞
的各个参数应Popen()
作为列表传递,即类似
Popen(['/bin/ssh', '-f', '-N', tunnelUser+"@"+localIP[x], '-R', ...])
但相反,您将参数作为一个巨大的字符串传递。因此它试图将整个文字字符串解释localhost -r 8080:192.168.1.3:8080
为一个主机名。
-r
另外,当代码将其设置为 时,为什么不成功的输出块包含该标志-R
?
添加回答
举报
0/150
提交
取消