1 #!/usr/bin/env python 2 3 """This runs Apache Status on the remote host and returns the number of requests per second. 4 5 ./astat.py [-s server_hostname] [-u username] [-p password] 6 -s : hostname of the remote server to login to. 7 -u : username to user for login. 8 -p : Password to user for login. 9 10 Example: 11 This will print information about the given host: 12 ./astat.py -s www.example.com -u mylogin -p mypassword 13 14 """ 15 16 import os, sys, time, re, getopt, getpass 17 import traceback 18 import pexpect, pxssh 19 20 def exit_with_usage(): 21 22 print globals()['__doc__'] 23 os._exit(1) 24 25 def main(): 26 27 ###################################################################### 28 ## Parse the options, arguments, get ready, etc. 29 ###################################################################### 30 try: 31 optlist, args = getopt.getopt(sys.argv[1:], 'h?s:u:p:', ['help','h','?']) 32 except Exception, e: 33 print str(e) 34 exit_with_usage() 35 options = dict(optlist) 36 if len(args) > 1: 37 exit_with_usage() 38 39 if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]: 40 print "Help:" 41 exit_with_usage() 42 43 if '-s' in options: 44 hostname = options['-s'] 45 else: 46 hostname = raw_input('hostname: ') 47 if '-u' in options: 48 username = options['-u'] 49 else: 50 username = raw_input('username: ') 51 if '-p' in options: 52 password = options['-p'] 53 else: 54 password = getpass.getpass('password: ') 55 56 # 57 # Login via SSH 58 # 59 p = pxssh.pxssh() 60 p.login(hostname, username, password) 61 p.sendline('apachectl status') 62 p.expect('([0-9]+\.[0-9]+)\s*requests/sec') 63 requests_per_second = p.match.groups()[0] 64 p.logout() 65 print requests_per_second 66 67 if __name__ == "__main__": 68 try: 69 main() 70 except Exception, e: 71 print str(e) 72 traceback.print_exc() 73 os._exit(1) 74 75