Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 3 | |
| 4 | import time |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 5 | import socket |
| 6 | import telnetlib |
| 7 | from collections import deque |
| 8 | |
| 9 | |
| 10 | class TelnetRemoteclient: |
| 11 | |
| 12 | r""" |
| 13 | Class to create telnet connection to remote host for command execution. |
| 14 | """ |
| 15 | |
| 16 | def __init__(self, hostname, username, password, port=23, read_timeout=None): |
| 17 | |
| 18 | r""" |
| 19 | Description of argument(s): |
| 20 | |
| 21 | hostname Name/IP of the remote (targeting) host |
| 22 | username User on the remote host with access to FFCD files |
| 23 | password Password for user on remote host |
| 24 | read_timeout New read timeout value to override default one |
| 25 | """ |
| 26 | |
| 27 | self.hostname = hostname |
| 28 | self.username = username |
| 29 | self.password = password |
| 30 | self.tnclient = None |
| 31 | self.port = port |
| 32 | self.read_timeout = read_timeout |
| 33 | |
| 34 | def tn_remoteclient_login(self): |
| 35 | |
| 36 | is_telnet = True |
| 37 | try: |
| 38 | self.tnclient = telnetlib.Telnet(self.hostname, self.port, timeout=15) |
| 39 | if b'login:' in self.tnclient.read_until(b'login:', timeout=self.read_timeout): |
| 40 | self.tnclient.write(self.username.encode('utf-8') + b"\n") |
| 41 | |
| 42 | if b'Password:' in self.tnclient.read_until(b'Password:', timeout=self.read_timeout): |
| 43 | self.tnclient.write(self.password.encode('utf-8') + b"\n") |
| 44 | |
| 45 | n, match, pre_match = \ |
| 46 | self.tnclient.expect( |
| 47 | [b'Login incorrect', b'invalid login name or password.', br'\#', br'\$'], |
| 48 | timeout=self.read_timeout) |
| 49 | if n == 0 or n == 1: |
| 50 | print("\n>>>>>\tERROR: Telnet Authentication Failed. Check userid and password.\n\n") |
| 51 | is_telnet = False |
| 52 | else: |
| 53 | # login successful |
| 54 | self.fifo = deque() |
| 55 | else: |
| 56 | # Anything else, telnet server is not running |
| 57 | print("\n>>>>>\tERROR: Telnet Connection Refused.\n\n") |
| 58 | is_telnet = False |
| 59 | else: |
| 60 | is_telnet = False |
| 61 | except Exception: |
| 62 | # Any kind of exception, skip telnet protocol |
| 63 | is_telnet = False |
| 64 | |
| 65 | return is_telnet |
| 66 | |
| 67 | def __del__(self): |
| 68 | self.tn_remoteclient_disconnect() |
| 69 | |
| 70 | def tn_remoteclient_disconnect(self): |
| 71 | try: |
| 72 | self.tnclient.close() |
| 73 | except Exception: |
| 74 | # the telnet object might not exist yet, so ignore this one |
| 75 | pass |
| 76 | |
| 77 | def execute_command(self, cmd, |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 78 | i_timeout=300): |
| 79 | |
| 80 | r''' |
| 81 | Executes commands on the remote host |
| 82 | |
| 83 | Description of argument(s): |
| 84 | cmd Command to run on remote host |
| 85 | rtnpartial Set to True to return command output even |
| 86 | if we haven't read the command prompt |
| 87 | wait_cnt Number of times to check for command output |
| 88 | default is 5 |
| 89 | i_timeout Timeout for command output |
| 90 | default is 300 seconds |
| 91 | ''' |
| 92 | |
| 93 | # Execute the command and read the command output |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 94 | # Execute the command and read the command output |
| 95 | return_buffer = b'' |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 96 | try: |
| 97 | |
| 98 | # Flush whatever data is in the read buffer by doing |
| 99 | # a non-blocking read |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 100 | self.tnclient.read_very_eager().decode('utf-8') |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 101 | |
| 102 | # Execute the command |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 103 | self.tnclient.write(cmd.encode('utf-8') + b'\n') |
| 104 | time.sleep(i_timeout) |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 105 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 106 | # Read the command output. |
| 107 | return_buffer = self.tnclient.read_very_eager() |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 108 | |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 109 | except (socket.error, EOFError) as e: |
| 110 | self.tn_remoteclient_disconnect() |
| 111 | |
| 112 | if str(e).__contains__("Connection reset by peer"): |
| 113 | msg = e |
| 114 | elif str(e).__contains__("telnet connection closed"): |
| 115 | msg = "Telnet connection closed." |
| 116 | else: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 117 | msg = "Some other issue.%s %s %s\n\n" % (cmd, e.__class__, e) |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 118 | |
| 119 | print("\t\t ERROR %s " % msg) |
Peter D Phan | e732f77 | 2021-07-01 10:16:35 -0500 | [diff] [blame] | 120 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 121 | return return_buffer |