gpt4 book ai didi

python - 使用 pxssh/pexpect 发送管道命令

转载 作者:太空宇宙 更新时间:2023-11-03 17:35:50 25 4
gpt4 key购买 nike

我目前正在使用 Python 的 pxssh,它的工作方式就像一个魅力,但它似乎不处理管道 (|) 命令 - 它将它们解释为单独的命令。

目前我有以下内容:

s = pxssh.pxssh()
s.login(sshHost, sshUsername, sshPassword)
s.sendline("cat somefile | grep something | cut -d \' -f 4")

它可以与任何非管道命令一起正常工作,但是我现在需要发送一个管道命令。

有没有办法用 pxssh 解决这个问题,或者您能否建议一种方法来实现此命令的另一个解决方案?

最佳答案

我不清楚为什么 pxssh 的行为会像您所描述的那样。您确定问题不是您的 \' 由 Python 解释,而您希望它由远程 shell 解释吗?这样拼写会更好:

s.sendline("cat somefile | grep something | cut -d \\' -f 4")

你当然有其他选择。一种是使用单个命令而不是管道,例如:

s.sendline("sed -n '/something/ { s/\\([^,]*,\\)\\{3\\}\\([^,]*\\),.*/\\2/; p }'")

作为一种特殊情况,您可以启动一个子 shell 来运行管道:

s.sendline('''bash -c "cat somefile | grep something | cut -d \\' -f 4"''')

关于python - 使用 pxssh/pexpect 发送管道命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com