Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface with remote computers using Python

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.

like image 545
physicsmichael Avatar asked Aug 19 '09 22:08

physicsmichael


1 Answers

Here's a simple, cheap solution to get you started

from subprocess import *
p = Popen('ssh servername who', shell=True, stdout=PIPE)
p.wait()
print p.stdout.readlines()

returns (eg)

['usr      pts/0        2009-08-19 16:03 (kakapo)\n',
 'usr      pts/1        2009-08-17 15:51 (kakapo)\n',
 'usr      pts/5        2009-08-17 17:00 (kakapo)\n']

and for cpuinfo:

p = Popen('ssh servername cat /proc/cpuinfo', shell=True, stdout=PIPE)
like image 145
Peter Avatar answered Oct 04 '22 17:10

Peter