diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2003-03-12 20:11:40 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2003-03-12 20:11:40 +0000 |
commit | 4dcc5570d8df8e3c5edd1b3b3164027eef168ebd (patch) | |
tree | edc02278032deb3550d6bc96b137ddaa511ee1e3 /pexpect/examples/monitor.py | |
parent | c0ece6dc8d807edee0609382795cc9b9c42ae145 (diff) | |
download | pexpect-4dcc5570d8df8e3c5edd1b3b3164027eef168ebd.tar.gz |
Improved simple monitor script
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@164 656d521f-e311-0410-88e0-e7920216d269
Diffstat (limited to 'pexpect/examples/monitor.py')
-rwxr-xr-x | pexpect/examples/monitor.py | 78 |
1 files changed, 64 insertions, 14 deletions
diff --git a/pexpect/examples/monitor.py b/pexpect/examples/monitor.py index 723813e..fbed084 100755 --- a/pexpect/examples/monitor.py +++ b/pexpect/examples/monitor.py @@ -1,40 +1,90 @@ #!/usr/bin/env python -'''This runs "uname" on a remote host using SSH. +'''This runs some commands on a remote host using SSH. At the prompts enter hostname, user, and password. + + It works like this: + Login via SSH (This is the hardest part). + Run and parse 'uptime'. + Run 'iostat'. + Run 'vmstat'. + Run 'free'. + Exit the remote host. + ''' import pexpect import getpass +# +# Some constants +# + # This is the prompt we get if SSH does not have the remote host's + # public key stored in the cache yet. SSH_NEWKEY = 'Are you sure you want to continue connecting (yes/no)?' +TERMINAL_PROMPT = 'Terminal type?' +TERMINAL_TYPE = 'vt100' +COMMAND_PROMPT = '[$#] ' ### This is way too simple for industrial use :-) ... + +print 'Enter the host that you wish to monitor.' host = raw_input('Hostname: ') user = raw_input('User: ') password = getpass.getpass('Password: ') -child = pexpect.spawn("ssh -l %s %s"%(user, host)) +# +# Login via SSH +# +child = pexpect.spawn('ssh -l %s %s'%(user, host)) i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, 'password: ']) -if i == 1: - child.sendline ('yes') - child.expect ('password: ') +if i == 0: # Timeout + print 'ERROR!' + print 'SSH could not login. Here is what SSH said:' + print child.before, child.after + sys.exit (1) +if i == 1: # SSH does not have the public key. Just accept it. + child.sendline ('yes') + child.expect ('password: ') child.sendline(password) -i = child.expect (['[#$] ', 'Terminal type?']) + # Now we are either at the command prompt or + # the login process is asking for our terminal type. +i = child.expect ([COMMAND_PROMPT, TERMINAL_PROMPT]) if i == 1: - child.sendline ('vt100') - child.expect ('[#$] ') + child.sendline (TERMINAL_TYPE) + child.expect (COMMAND_PROMPT) -child.sendline ('uptime') +# Now we should be at the command prompt and +# ready to run some commands. +print +print '---------------------------------------' +print 'Report of commands run on remote host.' +# Run and parse 'uptime'. +child.sendline ('uptime') child.expect ('up ([0-9]+) days?,.*?,\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]), ([0-9]+\.[0-9][0-9]), ([0-9]+\.[0-9][0-9])') duration, users, av1, av5, av15 = child.match.groups() -print '%s days, %s users, %s (1 min), %s (5 min), %s (15 min)' % ( +print +print 'Uptime: %s days, %s users, %s (1 min), %s (5 min), %s (15 min)' % ( duration, users, av1, av5, av15) +child.expect (COMMAND_PROMPT) -child.expect ('[#$] ') +# Run iostat. child.sendline ('iostat') -child.expect ('[#$] ') +child.expect (COMMAND_PROMPT) +print +print child.before + +# Run vmstat. +child.sendline ('vmstat') +child.expect (COMMAND_PROMPT) +print print child.before -child.sendline ('exit') -child.expect(pexpect.EOF) +# Run free. +child.sendline ('free') # Linux systems only. +child.expect (COMMAND_PROMPT) +print print child.before +# Now exit the remote host. +child.sendline ('exit') +child.expect(pexpect.EOF) + |