我刚刚成为我的研究小组集群的系统管理员,在这方面,我还是一个新手。我正在尝试制作一些工具来监控网络,并需要帮助开始使用 python(我的母语)实现它们。
例如,我想查看谁登录到远程计算机。手动地,我会 ssh 和 who
,但是我如何将此信息放入脚本中进行操作?类似的,
import remote_info as ri
ri.open("foo05.bar.edu")
ri.who()
Out[1]:
hutchinson tty7 2009-08-19 13:32 (:0)
hutchinson pts/1 2009-08-19 13:33 (:0.0)
类似于 cat /proc/cpuinfo
之类的东西来获取节点的处理器信息。一个起点真的很棒。谢谢。
I've just become the system admin for my research group's cluster and, in this respect, am a novice. I'm trying to make a few tools to monitor the network and need help getting started implementing them with python (my native tongue).
For example, I would like to view who is logged onto remote machines. By hand, I'd ssh and who
, but how would I get this info into a script for manipulation? Something like,
import remote_info as ri
ri.open("foo05.bar.edu")
ri.who()
Out[1]:
hutchinson tty7 2009-08-19 13:32 (:0)
hutchinson pts/1 2009-08-19 13:33 (:0.0)
Similarly for things like cat /proc/cpuinfo
to get the processor information of a node. A starting point would be really great. Thanks.
发布评论
评论(6)
这是一个简单、廉价的解决方案,可以帮助您开始
返回(例如)
和 cpuinfo:
Here's a simple, cheap solution to get you started
returns (eg)
and for cpuinfo:
我一直在使用 Pexpect,它可以让你通过 ssh 进入机器,发送命令,读取输出,并对其做出反应,并取得成功。我什至围绕它启动了一个开源项目 Proxpect - 该项目已经很久没有更新了,但我离题了……
I've been using Pexpect, which let's you ssh into machines, send commands, read the output, and react to it, with success. I even started an open-source project around it, Proxpect - which haven't been updated in ages, but I digress...
pexpect 模块可以帮助您与 ssh 交互。或多或少,您的示例如下所示。
The pexpect module can help you interface with ssh. More or less, here is what your example would look like.
如果您的需求超出了简单的“
ssh remote-host.example.org who
”,那么有一个很棒的 Python 库,名为 RPyC。它具有所谓的“经典”模式,允许使用几行代码通过网络几乎透明地执行 Python 代码。对于可信环境来说非常有用的工具。以下是来自 Wikipedia 的示例:
如果您有兴趣,可以他们的 wiki 上有一个很好的教程。
但是,当然,如果您完全可以使用
Popen
进行 ssh 调用,或者只是不想运行单独的“RPyC”守护进程,那么这绝对是一种矫枉过正。If your needs overgrow simple "
ssh remote-host.example.org who
" then there is an awesome python library, called RPyC. It has so called "classic" mode which allows to almost transparently execute Python code over the network with several lines of code. Very useful tool for trusted environments.Here's an example from Wikipedia:
If you're interested, there's a good tutorial on their wiki.
But, of course, if you're perfectly fine with ssh calls using
Popen
or just don't want to run separate "RPyC" daemon, then this is definitely an overkill.这涵盖了基础。请注意 sudo 用于需要更多权限的事情。我们将 sudo 配置为允许该用户执行这些命令,而无需输入密码。
另外,请记住,您应该运行 ssh-agent 才能使其“有意义”。但总而言之,它的效果确实很好。运行
deploy-control httpd configtest
将检查所有远程服务器上的 apache 配置。This covers the bases. Notice the use of sudo for things that needed more privileges. We configured sudo to allow those commands for that user without needing a password typed.
Also, keep in mind that you should run ssh-agent to make this "make sense". But all in all, it works really well. Running
deploy-control httpd configtest
will check the apache configuration on all the remote servers.Fabric 是一种自动执行一些简单任务的简单方法,我当前使用的版本允许您像这样包装命令:
您可以为您需要的每台机器指定配置选项(config.fab_user、config.fab_password)(如果您想要自动处理用户名密码)。
有关 Fabric 的更多信息,请访问:
http://www.nongnu.org/fab/
新版本更Pythonic - 我不确定这是否对你来说更好......目前对我来说效果很好......
Fabric is a simple way to automate some simple tasks like this, the version I'm currently using allows you to wrap up commands like so:
you can specify config options (config.fab_user, config.fab_password) for each machine you need (if you want to automate username password handling).
More info on Fabric here:
http://www.nongnu.org/fab/
There is a new version which is more Pythonic - I'm not sure whether that is going to be better for you int his case... works fine for me at present...