def show_routing_table(self): try: tc = telnet(self.target, self.telnet_port, self.default_timeout) except socket.timeout: print("Connection failed !") else: try: tc.read_until(b"Username:"******"ascii") + b"\n") if self.password: tc.read_until(b"Password:"******"ascii") + b"\n") except socket.timeout: print("Connection failed !") else: tc.write(b"show ip route\n") tc.write(b"exit\n") result = tc.read_all().decode("ascii") start_routing_text = result.find( "Gateway of last resort is not set") last_routing_text = result.rfind(">") print(result[start_routing_text:last_routing_text]) with open("logs/log.txt", "a") as txt_file: import datetime txt_file.write("----------------------\n") command_execution_time = datetime.datetime.now() txt_file.write(str(command_execution_time) + "\n") txt_file.write("show_routing_table command executed ... ") txt_file.write(result) txt_file.write("----------------------\n") show_result = input("Do you want to open log file ? (y) :") if show_result == "y" or show_result == "yes": import os os.system("start logs/log.txt")
def enable_ssh(self): '''enable ssh on network device''' host_name = input("write a host name :") local_pass = self.localpassword encryption_bits = input( "RSA encryption bits [(default = 512) , [from 360 to 4096]] :") if not encryption_bits: encryption_bits = "512" try: tc = telnet(self.target, self.telnet_port, self.default_timeout) except socket.timeout: print("Connection failed !") else: try: tc.read_until(b"Username:"******"ascii") + b"\n") if self.password: tc.read_until(b"Password:"******"ascii") + b"\n") except socket.timeout: print("Connection failed !") else: tc.write(b"enable\n") tc.read_until(b"Password:"******"ascii") + b"\n") tc.write(b"conf t\n") tc.write(b"ip domain-name " + host_name.encode("ascii") + b"\n") tc.write(b"crypto key generate rsa\n") # check if a rsa key exists try: tc.read_until(b"How many bits in the modulus [512]:", 1.5) except socket.timeout: print("Connection failed !") else: tc.write(b"yes\n") tc.write(encryption_bits.encode("ascii") + b"\n") tc.write(b"line vty 0 4\n") tc.write(b"transport input ssh telnet\n") tc.write(b"exit\n") tc.write(b"exit\n") tc.write(b"exit\n") result = tc.read_all().decode("ascii") with open("logs/log.txt", "a") as txt_file: import datetime txt_file.write("----------------------\n") command_execution_time = datetime.datetime.now() txt_file.write(str(command_execution_time) + "\n") txt_file.write("enable_ssh command executed ... ") txt_file.write(result) txt_file.write("----------------------\n") show_result = input("Do you want to open log file ? (y) :") if show_result == "y" or show_result == "yes": import os os.system("start logs/log.txt")
def set_static_route(self): print("") network = input("Network :") subnet_mask = input("Subnet mask :") destination = input("Destination ip or router interface :") local_pass = self.localpassword try: tc = telnet(self.target, self.telnet_port, self.default_timeout) except socket.timeout: print("Connection failed !") else: try: tc.read_until(b"Username:"******"ascii") + b"\n") if self.password: tc.read_until(b"Password:"******"ascii") + b"\n") except socket.timeout: print("Connection failed !") else: tc.write(b"enable\n") tc.read_until(b"Password:"******"ascii") + b"\n") tc.write(b"configure terminal\n") tc.write(b"ip route " + network.encode("ascii") + b" " + subnet_mask.encode("ascii") + b" " + destination.encode("ascii") + b"\n") print("") Routing.show_routing_table(self) print("") is_correct = input("Correct (y) :") if is_correct == "y" or is_correct == "yse": pass else: tc.write(b"no ip route " + network.encode("ascii") + b" " + subnet_mask.encode("ascii") + b" " + destination.encode("ascii") + b"\n") tc.write(b"exit\n") tc.write(b"exit\n") result = tc.read_all().decode("ascii") with open("logs/log.txt", "a") as txt_file: import datetime txt_file.write("----------------------\n") command_execution_time = datetime.datetime.now() txt_file.write(str(command_execution_time) + "\n") txt_file.write("set_static_route command executed ... ") txt_file.write(result) txt_file.write("----------------------\n") show_result = input("Do you want to open log file ? (y) :") if show_result == "y" or show_result == "yes": import os os.system("start logs/log.txt")
def set_ip(self): '''set a new ip address for an interface''' interface = input("Interface :") ip_address = input("IP address :") subnet_mask = input("Subnet mask :") local_pass = self.localpassword try: tc = telnet(self.target, self.telnet_port, self.default_timeout) except socket.timeout: print("Connection failed !") else: try: tc.read_until(b"Username:"******"ascii") + b"\n") if self.password: tc.read_until(b"Password:"******"ascii") + b"\n") except socket.timeout: print("Connection failed !") else: tc.write(b"enable\n") tc.read_until(b"Password:"******"ascii") + b"\n") tc.write(b"configure terminal\n") tc.write(b"interface " + interface.encode("ascii") + b"\n") tc.write(b"ip address " + ip_address.encode("ascii") + b" " + subnet_mask.encode("ascii") + b"\n") tc.write(b"no shutdown\n") tc.write(b"exit\n") tc.write(b"exit\n") tc.write(b"exit\n") result = tc.read_all().decode("ascii") with open("logs/log.txt", "a") as txt_file: import datetime txt_file.write("----------------------\n") command_execution_time = datetime.datetime.now() txt_file.write(str(command_execution_time) + "\n") txt_file.write("set_ip command executed ... ") txt_file.write(result) txt_file.write("----------------------\n") show_result = input("Do you want to open log file ? (y) :") if show_result == "y" or show_result == "yes": import os os.system("start logs/log.txt")
def create_user(self): '''create a new user''' create_user = input("Username that you wanna create :") create_pass = input("Password that you wanna create :") local_pass = self.localpassword try: tc = telnet(self.target, self.telnet_port, self.default_timeout) except socket.timeout: print("Connection failed !") else: try: tc.read_until(b"Username:"******"ascii") + b"\n") if self.password: tc.read_until(b"Password:"******"ascii") + b"\n") except socket.timeout: print("Connection failed !") else: tc.write(b"enable\n") tc.read_until(b"Password:"******"ascii") + b"\n") tc.write(b"conf t\n") tc.write(b"username " + create_user.encode("ascii") + b" password " + create_pass.encode("ascii") + b"\n") tc.write(b"exit\n") tc.write(b"exit\n") result = tc.read_all().decode("ascii") with open("logs/log.txt", "a") as txt_file: import datetime txt_file.write("----------------------\n") command_execution_time = datetime.datetime.now() txt_file.write(str(command_execution_time) + "\n") txt_file.write("create_user command executed ... ") txt_file.write(result) txt_file.write("----------------------\n") show_result = input("Do you want to open log file ? (y) :") if show_result == "y" or show_result == "yes": import os os.system("start logs/log.txt")
#url:https://www.jb51.net/article/14305.htm def telnetdo(host=none,user=none,pass=none,command=none): import telnetlib import sys if not host: try: host=sys.argv[1] user=sys.argv[2] pass=sys.argv[3] command=sys.argv[4] except: print('usage:telnetdo.py host user pass command') return msg=['debug messges:\n'] tn=telnetlib.telnet() try: tn.open(host) except: print('can not open host') retrun msg.append(tn.expect(['login:'******'\n') if pass: msg.append(tn.expect(['password:'******'\n') msg.append(tn.expect([user],5)) tn.close() del tn return msg[len(msg)-1][2] if __name__ == 'main' print telnetdo()