Archive for the 'Python' Category

Feb 03 2014

Remotely restart the SNMP daemon on Ubuntu 12.04 LTS via a Python skript

Published by under Python

It can happen that the SNMP daemon on a server crashes or stops, sometimes without leaving any hints in the system’s logfiles. If you’re running a monitoring server like WhatsUp, you can define a monitor for the SNMP service and should it be reported as down, you can define an action that launches the following Python skript to restart the daemon:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy())
ssh.connect('', username='pythonuser', password='password')

stdin, stdout, stderr = ssh.exec_command( "sudo service snmpd restart")


In order for this skript to work properly, the user “pythonuser” needs to be able to execute sudo commands without having to type in a password again. The used Python ssh-library, paramiko, does not easily support the use of sudo commands, so it is easier to work around the problem by allowing the user “pythonuser” to run certain commands without having to provide a password again.

In order to allow “pythonuser” to run the “service” command via “sudo” without entering an additional password, this line has to be added to the end of /etc/sudoers on the server on which you need to restart the snmp daemon:

pythonuser ALL=NOPASSWD: /usr/sbin/service

IMPORTANT: This line MUST be at the end of the file, otherwise it will NOT override any settings defined for the “sudo” or other user groups (whose members must provide passwords).

Comments Off on Remotely restart the SNMP daemon on Ubuntu 12.04 LTS via a Python skript

Jan 30 2014

Installing Paramiko for Python 2.7 on Windows 8.1

Published by under Programming,Python

I have the need to write a couple of Python skripts that can login to Linux servers via ssh and do some “magic” there. Like, for example, restart a stopped snmpd daemon. Or something like that. From what I’ve read, the Paramiko module for Python will be my friend here.

The problem is that those Python modules will be triggered from a monitoring server running on 64-Bit Windows 2008, and my daily workhorse and development machine is running 64-Bit Windows 8.1. Accordingly, I also prefer to use the 64-Bit version of Python. (Just because, there is no real technical reason for that at this point in time.)

I found these websites indispensable when it comes to Windows-installers for certain Python packages:

What you need from there are the packages named Base-13.2.11-<xyz> and pip.

In Windows, you should enhance your PATH variable not only to include the Python installation directory, but also to include the Python\Scripts directory as well – because then you can run commands like “pip install paramiko” from the command line.

Paramiko depends on the “Crypto” module, which it will implicitly load when you execute “import paramiko” in Python. A Windows installer for this package can be found here:

Download the file and run it.

The fun part is that either Python or Windows 8.1 itself are quite case sensitive. The pycrypto Windows installer creates a lower-case folder “crypto”, but Paramiko uses an upper-case name to load the “Crypto” module. While this will work on Windows Server 2008 regardless, it will fail on Windows 8.1. So you have to go to the Python\Lib\site-packages folder and rename the folder “crypto” to “Crypto”.

After that, install the Paramiko module via “pip install paramiko”.

Now you can launch the Python interpreter and type “import paramiko”. If Python shows you the interpreter prompt without any messages at all, you should have a working Paramiko installation.

Comments Off on Installing Paramiko for Python 2.7 on Windows 8.1

Next »