Created
December 26, 2019 06:17
-
-
Save abidkhan484/b282f11c83adfc9151920678f67bb4a2 to your computer and use it in GitHub Desktop.
Python script for SSH login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| from pexpect import pxssh | |
| s = pxssh.pxssh() | |
| hostname = '' | |
| username = '' | |
| password = '' | |
| if not s.login (hostname, username, password): | |
| print "SSH session failed on login." | |
| print str(s) | |
| else: | |
| print "SSH session login successful" | |
| s.sendline ('uptime') | |
| s.prompt() # match the prompt | |
| get_uptime = s.before.split("\r\n") # print everything before the prompt. | |
| uptime = "there's some error in executing uptime" | |
| if len(get_uptime) > 1: | |
| uptime = get_uptime[1].split(",") | |
| print uptime | |
| # cpu execution | |
| s.sendline ("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'") | |
| s.prompt() # match the prompt | |
| cpu = s.before.split("\r\n") # print everything before the prompt. | |
| try: | |
| cpu_usage = cpu[1] | |
| active_time = (cpu_usage[0] + cpu_usage[1]).strip() | |
| active_user = cpu_usage[2].strip() | |
| one_minute_report = cpu_usage[3][-4:] | |
| five_minute_report = cpu_usage[4].strip() | |
| fifteen_minute_report = cpu_usage[5].strip() | |
| except: | |
| print "there's some error in executing cpu usage" | |
| print active_time, active_user, one_minute_report, five_minute_report, fifteen_minute_report | |
| activity = "there's some error in executing sevice sshd staus" | |
| s.sendline ('service sshd status') | |
| s.prompt() # match the prompt | |
| if s.before: | |
| activity = "running" if 'active (running)' in s.before else "not running" | |
| s.sendline ('q') | |
| print activity | |
| s.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment