【问题标题】:How to pass variable as input in pxssh如何在 pxssh 中将变量作为输入传递
【发布时间】:2016-10-22 11:50:58
【问题描述】:

我正在使用 pxssh 与另一台主机连接并使用 python 读取文件。我能够成功连接并传递简单的命令。如何使用 cat 命令读取文件,其中文件路径存储在变量中

s = pxssh.pxssh()
s.login(hostname,username,pwd)
path = "mydir/mysubdir/a.txt"
s.sendline('cat ') # How to pass the path variable here 

我也想逐行读取文件。有没有办法使用 cat 或任何其他命令或如何使用 python 命令来做到这一点。

【问题讨论】:

    标签: python ssh pexpect


    【解决方案1】:

    你可以创建字符串并像 s.sendline(variable) 一样传递

        cmd1 = 'cat '
        path = 'mydir/mysubdir/a.txt'
        cmd = cmd1 + path
        s.sendline(cmd)
        s.prompt()
        file = s.before
        print file.splitlines()[no of line] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 2023-03-03
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      相关资源
      最近更新 更多