]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - third_party/Python/module/pexpect-2.4/examples/astat.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / third_party / Python / module / pexpect-2.4 / examples / astat.py
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
17 import sys
18 import time
19 import re
20 import getopt
21 import getpass
22 import traceback
23 import pexpect
24 import pxssh
25
26
27 def exit_with_usage():
28
29     print globals()['__doc__']
30     os._exit(1)
31
32
33 def main():
34
35     ######################################################################
36     # Parse the options, arguments, get ready, etc.
37     ######################################################################
38     try:
39         optlist, args = getopt.getopt(
40             sys.argv[
41                 1:], 'h?s:u:p:', [
42                 'help', 'h', '?'])
43     except Exception as e:
44         print str(e)
45         exit_with_usage()
46     options = dict(optlist)
47     if len(args) > 1:
48         exit_with_usage()
49
50     if [elem for elem in options if elem in [
51             '-h', '--h', '-?', '--?', '--help']]:
52         print "Help:"
53         exit_with_usage()
54
55     if '-s' in options:
56         hostname = options['-s']
57     else:
58         hostname = raw_input('hostname: ')
59     if '-u' in options:
60         username = options['-u']
61     else:
62         username = raw_input('username: ')
63     if '-p' in options:
64         password = options['-p']
65     else:
66         password = getpass.getpass('password: ')
67
68     #
69     # Login via SSH
70     #
71     p = pxssh.pxssh()
72     p.login(hostname, username, password)
73     p.sendline('apachectl status')
74     p.expect('([0-9]+\.[0-9]+)\s*requests/sec')
75     requests_per_second = p.match.groups()[0]
76     p.logout()
77     print requests_per_second
78
79 if __name__ == "__main__":
80     try:
81         main()
82     except Exception as e:
83         print str(e)
84         traceback.print_exc()
85         os._exit(1)