Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import telnetlib
- import time
- import argparse
- import sys
- class MyTelConn(object):
- connection_timeout = 5
- reading_timeout = 5
- def __init__(self, user, passwd, host, port=23):
- self.user = user
- self.passwd = passwd
- self.host = host
- self.port = port
- self.connection = None
- self.login_prompt = b"Username:"
- self.password_prompt = b"Password:"
- self.command_file = False
- self._file = None
- @property
- def file(self):
- return self._file
- @file.setter
- def file(self, file):
- self.command_file = True
- self._file = file
- def connect(self):
- try:
- #====> Login to the Device
- self.connection = telnetlib.Telnet(self.host, self.port, self.connection_timeout)
- self.connection.read_until(self.login_prompt, self.reading_timeout)
- self.connection.write(self.user.encode('ascii') + b"\n")
- time.sleep(1)
- #=====> Setup Terminal Length for get the entire output
- self.connection.read_until(self.password_prompt, self.reading_timeout)
- self.connection.write(self.passwd.encode('ascii') + b"\n")
- time.sleep(1)
- print("successfully login to device {} host !".format(self.host))
- except IOError as e:
- return "Input parameter error {}, Please check username, passwd and file name".format(e)
- def send_config_manual(self, code):
- try:
- # ========> Enter to Global Config
- self.connection.write(b"\n")
- self.connection.write("config t\n".encode('ascii'))
- time.sleep(1)
- self.connection.write(code.encode('ascii') + b'\n')
- time.sleep(1)
- print("successfully send command code <{}> to device {} !".format(code, self.host))
- except IOError as e:
- return "Input parameter error {}".format(e)
- finally:
- output = self.connection.read_very_eager()
- print(output)
- self.connection.close()
- def send_show_code(self, code):
- try:
- self.connection.write('term mon\n'.encode('ascii'))
- time.sleep(1)
- self.connection.write(code.encode('ascii') + b'\n')
- time.sleep(1)
- print("successfully send show command <{}> to device! {} ".format(code, self.host))
- except IOError as e:
- return "Input parameter error {}".format(e)
- finally:
- output = self.connection.read_very_eager()
- print(output)
- self.connection.close()
- def send_config_file(self):
- try:
- # ========> Enter to Global Config
- self.connection.write(b"\n")
- self.connection.write("config t\n".encode('ascii'))
- time.sleep(1)
- with open(self.file, 'r') as f_command:
- for line in f_command.readlines():
- self.connection.write(line.encode('ascii') + b'\n')
- time.sleep(1)
- print("successfully send file configuration <{}> to device {} !".format(self.file, self.host))
- except IOError:
- return "invalid File Access!.."
- finally:
- output = self.connection.read_very_eager().decode(encoding='utf-8#')
- print(output)
- self.connection.close()
- def main():
- if len(sys.argv) <= 1:
- print("Use {} --h or --help to get help menu".format(sys.argv[0]))
- else:
- parser = argparse.ArgumentParser(description="||======= REMOTE TELNET MENU =======>\n{} <=====||".format(sys.argv[0]))
- parser.add_argument('--ip', help="Ipv4 address eg. 192.168.1.1")
- parser.add_argument('--u', help="username or login account ")
- parser.add_argument('--p', help="password")
- parser.add_argument('--send', help='''<show> send show command,
- <sm> send configuration manually,
- <sf> send configuration from file
- ''',choices = ['show', 'sm', 'sf'])
- args = parser.parse_args()
- conn = MyTelConn(args.u, args.p, args.ip)
- conn.connect()
- if args.send == 'sf':
- import_file = input("Enter Your file command: ")
- conn.file = import_file
- conn.send_config_file()
- elif args.send == 'show':
- show_command = input("Enter Your Show Command\n>: ")
- conn.send_show_code(show_command)
- elif args.send == 'sm':
- manual_command = input("Enter your Command\n>: ")
- conn.send_config_manual(manual_command)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment