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