【问题标题】:plink ssh command as a variable from txt file using pythonplink ssh 命令作为 txt 文件中的变量使用 python
【发布时间】:2017-04-13 09:58:24
【问题描述】:

我有一个关于使用 plink 并将命令作为外部文件中的变量发送的问题。

test.txt 里面有:

id -a user.name1\n; id -a user.name2\n; id -a user.name3\n;exit\n

此代码不起作用:

import subprocess
sshoutput = open("c:/sshoutput.txt", "w")
sshoutputerror = open("c:/sshoutputerror.txt", "w")
sshinput = open("C:/test.txt", "r").read()
ssh = subprocess.Popen("plink user.name@server -pw password",shell=True, stdin=subprocess.PIPE, stdout=sshoutput, stderr=sshoutputerror)
ssh.communicate(sshinput)

但如果我将最后一行 sshinput 更改为 "id -a user.name1\n; id -a user.name2\n; id -a user.name3\n;exit \n"

ssh.communicate("id -a user.name1\n; id -a user.name2\n; id -a user.name3\n;exit\n")

这确实有效,但我想使用来自外部文件的命令。

感谢您的帮助。

【问题讨论】:

    标签: python text ssh command subprocess


    【解决方案1】:

    尝试在 test.txt 文件中包含引号内的行。

    像这样:

     "id -a user.name1\n; id -a user.name2\n; id -a user.name3\n;exit\n"
    

    并使用 readline() 代替 read() :

    import subprocess
    sshoutput = open("c:/sshoutput.txt", "w")
    sshoutputerror = open("c:/sshoutputerror.txt", "w")
    sshinput = open("C:/test.txt", "r").readline()
    ssh = subprocess.Popen("plink user.name@server -pw password",shell=True, stdin=subprocess.PIPE, stdout=sshoutput, stderr=sshoutputerror)
    ssh.communicate(sshinput)
    

    【讨论】:

    • 感谢您的快速回答!它的工作!我笨! :)
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多