Skip to content

Instantly share code, notes, and snippets.

@noahlz
Last active January 2, 2016 01:58
Show Gist options
  • Save noahlz/8233527 to your computer and use it in GitHub Desktop.
Save noahlz/8233527 to your computer and use it in GitHub Desktop.
Little script adapted from paramiko simle example to check all your servers one at a time for an error condition. Not necessary if your servers use private key access.
# http://stackoverflow.com/questions/6012665/read-non-blocking-error-while-using-pxssh-ssh-module-for-python
# https://github.com/paramiko/paramiko/blob/master/README
# http://stackoverflow.com/questions/8491194/how-to-handle-socket-errors-as-exceptions-in-python-paramiko
import paramiko, base64, getpass, socket
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
user = getpass.getuser()
pwd = getpass.getpass()
for i in range(1, 100):
worker = 'server{:0>3}.prod'.format(i) # assuming format is server001, server002, server003, etc...
try:
client.connect(worker, username=user, password=pwd, timeout=2)
stdin, stdout, stderr = client.exec_command('grep -c WTFException /var/log/server.log')
for line in stdout:
print worker + ": " + line.strip('\n')
except socket.error:
print "couldn't connect to " + worker
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment