Simple remote process monitoring with Python - python

I'd like to write a python script to perform some very simple "agentless" monitoring of remote processes running on linux servers.
It would perform the following tasks, in psuedocode:
for each remoteIPAddress in listOfIPAddresses:
log into server#remoteIPAddress via ssh
execute the equivalent of a 'ps -ef' command
grep the result to make sure a particular process (by name) is still running
One way to do this is to have python call shell scripts in a subprocess and parse their output.
That seems pretty inefficient. Is there a better way to do this via python libraries?
All I could find via research here and elsewhere was:
psutil - looks like it doesn't do remote monitoring, so I'd have to run agents on the remote machines to report stats back via RPC.
pymeter - I would have to write my own plugin for monitoring a specific remote service.
stackoverflow #4546492 - Some helpful links but the poster was looking for a different solution.
Thanks, and please go easy on me, it's my first question :-)

The Fabric library may be of interest to you.

Check out paramiko. You can use it to ssh into the server and run commands. You can then parse the results and do what you'd like with them.

Taking cues from the answers above, I investigated Fabric and found the following presentation particularly interesting/helpful. It is an overview of three libraries -- Fabric, Cuisine, and Watchdog -- for server monitoring and administration.
For posterity:
Using Fabric, Cuisine, and Watchdog for server administration in Python

It might be heavier than what you're looking for, but Zenoss supports agentless monitoring.
paramiko and Fabric, suggested in the other answers, are great options too.

Why don't you use a dedicated monitoring tool like Nagios ?
Nagios has agent and agent less monitoring through NRPE plugins and SSH plugins etc.
Try it out.

Related

How to make an Api which can retrieve Linux system details?

I got a assignment to implement this api and I don't know where to start and I've looked with no clear results.
The closest thing I've found is using ssh to access Linux system and running commands which give me the details about the system from there. I could run a python code to run commands on local host using subprocess library. Then saw somewhere I can't run ssh using a api.
I was hoping someone could tell me where to start or how to go about this.
Thank you.
try to use a backdoor ( I would recommand using python because it's easy) with a client listening ( on your computer), to retreive information about system, do some (relatively real-time ) monitoring ( cmd typing automated !) ....., (infos processing can be done in the client side ).
here is a close example of a (keylooger backdoor ) : https://github.com/JAMAIKA-MHD/Simple-Key-Logger-with-Backdoor

Is it possible to use python to establish a putty ssh session and send some input?

Fist of all, due to Company Policy, Paramiko, or installing anything that requires administrative access to local machine it right out; otherwise I would have just done that.
All I have to work with is python with standard libraries & putty.
I am attempting to automate some tedious work that involves logging into a network device (usually Cisco, occasionally Alcatel-Lucent, or Juniper), running some show commands, and saving the data. (I am planning on using some other scripts to pull data from this file, parse it, and do other things, but that should be irrelevant to the task of retrieving the data.) I know this can be done with telnet, however I need to do this via ssh.
My thought is to use putty's logging ability to record output from a session to a file. I would like to use Python to establish a putty session, send scripted log-in and show commands, and then close the session. Before I set out on this crusade, does anyone know of any way to do this? The closest answers I have found to this all suggest to use Paramiko, or other python ssh library; I am looking for a way to do this given the constraints I am under.
The end-result would ideal be able to be used as a function, so that I can iterate through hundreds of devices from a list of ip addresses.
Thank you for your time and consideration.
If you can't use paramiko, and Putty is all you get so the correct tool is actually not Putty - it's his little brother Plink - you can download it here
Plink is the command line tool for Putty and you can your python script to call it using os.system("plink.exe [options] username#server.com [command])
See MAN Page here
Hope it will help,
Liron

How to launch a python process in Windows SYSTEM account

I am writing a test application in python and to test some particular scenario, I need to launch my python child process in windows SYSTEM account.
I can do this by creating exe from my python script and then use that while creating windows service. But this option is not good for me because in future if I change anything in my python script then I have to regenerate exe every-time.
If anybody have any better idea about how to do this then please let me know.
Bishnu
Create a service that runs permanently.
Arrange for the service to have an IPC communications channel.
From your desktop python code, send messages to the service down that IPC channel. These messages specify the action to be taken by the service.
The service receives the message and performs the action. That is, executes the python code that the sender requests.
This allows you to decouple the service from the python code that it executes and so allows you to avoid repeatedly re-installing a service.
If you don't want to run in a service then you can use CreateProcessAsUser or similar APIs.
You could also use Windows Task Scheduler, it can run a script under SYSTEM account and its interface is easy (if you do not test too often :-) )
To run a file with system account privileges, you can use psexec. Download this :
Sysinternals
Then you may use :
os.system
or
subprocess.call
And execute:
PSEXEC -i -s -d CMD "path\to\yourfile"
Just came across this one - I know, a bit late, but anyway. I encountered a similar situation and I solved it with NSSM (Non_Sucking Service Manager). Basically, this program enables you to start any executable as a service, which I did with my Python executable and gave it the Python script I was testing on as a parameter.
So I could run the service and edit the script however I wanted. I just had to restart the service when I made any changes to the script.
One point for productive environments: Try not to rely on third party software like NSSM. You could also achieve this with the standard SC command (see this answer) or PowerShell (see this MS doc).

Identify who has killed a python windows service

I have a python windows service running in the background on a shared computer. I would like to know who is bad mannered enough to kill my processes without asking.
Is there a way to know which user kills a given service ?
Started reading here, but didn't find what i was looking for.
http://timgolden.me.uk/python/win32_how_do_i/track-session-events.html
Thanks for your help.
There is an interesting article I found on this in the following link:
http://bugslasher.net/2011/04/17/who-the-hell-killed-my-process/.
I have not tried this yet, but would be interested to hear how you get on.
UPDATE
In summary you can download debugging tools for Windows (available here)
This includes a version of GFlags (global flags)
GFlags can be configured to log data when processes are ended (via silent process exit tab)
Now you should be able to view details of killed processes (including the perpetrator) in event manager

How to remotely restart a service on a password protected machine using Python?

I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to be given to me but maybe some pointers on what libraries I should use or any issues I need to address when writing the script.
EDIT: All the remote machines are using Windows 2003
People usually recommend paramiko as a library to do ssh (and I'm assuming that you need ssh to get into the remote machine). There is a good tutorial for it.
Edit: On windows, the easiest way is probably to use SysInternals psservice utility, to be invoked with os.system; this can start a remote service, and accepts logon credentials.
If you want to do it directly in Python, you need win32service.StartService. Before that, you need to open the remote service manager, and then the remote service. Before that, you need to impersonate the user as which you want to perform the operation, see the example.
Take a look at Fabric wich is based on paramiko.
This is really a good tool to automate remote tasks with python.
Fabric Documentation will show you how easy it is to use.
What kind of OS is your remote machine running? If it's linux, run ssh(1) using the subprocess module.
If it's windows, then get the win32 extensions. They allow you to call Windows functions. There should be an API to allow to access services. If they don't, there is a tool called sc (docs) which you can run using the subprocess module.
Which OS for the target machines? If 'service' is 'Windows NT service', and your local machine is also Windows, I'd use IronPython as the Python language implementation and call straight into the WMI facilities in the .net System.Management namespace -- they're meant for remote admin like that.
On Windows, the wmi module is now fantastic for this.

Categories

Resources