简体   繁体   English

Python SSH 并将输出与导入的列表进行比较

[英]Python SSH and comparing output to an imported list

I am looking to ssh to multiple servers 1 at a time , compare the output of an ssh command to a list, and then run a command on the items in that appear in both the output and a separate list.我希望一次通过 ssh 连接到多个服务器 1,将 ssh 命令的输出与列表进行比较,然后对出现在输出和单独列表中的项目运行命令。

I'd like to ssh to each server in a loop "ssh " but I'm unsure how to import the next server from the list into the middle of the ssh command.我想在循环“ssh”中通过 ssh 连接到每个服务器,但我不确定如何将列表中的下一个服务器导入到 ssh 命令的中间。

The other issue I'm having is obtaining the results of a command run which I could then compare.我遇到的另一个问题是获取命令运行的结果,然后我可以进行比较。

Any guidance or direction to a helpful post would be fantastic.任何对有用帖子的指导或方向都会很棒。
Thank you!谢谢!

The answer to your question if I understood correctly lies in Operative Systems principles,如果我理解正确的话,你的问题的答案在于操作系统原则,

You iterate the SSH connection list, and by using Popen you are able to SSH via Shell.您迭代 SSH 连接列表,通过使用 Popen,您可以通过 Shell 进行 SSH。

After that you need to keep a second process open as well a Pipe for the 2 processes to communicate(the one that opened the ssh via shell, this one is blocked in IO) and use the second process to make verification etc and send new commands via PIPE created by the first process.之后,您需要保持第二个进程打开以及两个进程通信的管道(通过shell打开ssh的那个,这个在IO中被阻止)并使用第二个进程进行验证等并发送新命令通过第一个进程创建的 PIPE。 PIPE.STDIN管道标准输入

In short, check Popen Python Module.简而言之,检查 Popen Python 模块。

https://docs.python.org/2/library/subprocess.html https://docs.python.org/2/library/subprocess.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM