def backup(router): client = myparamiko.connect(**router) shell = myparamiko.get_shell(client) myparamiko.send_command(shell, 'terminal length 0') myparamiko.send_command(shell, 'enable') myparamiko.send_command(shell, 'cisco') # this is the enable command myparamiko.send_command(shell, 'show run') output = myparamiko.show(shell) # print(output) output_list = output.splitlines() output_list = output_list[11:-1] # print(output_list) output = '\n'.join(output_list) # print(output) from datetime import datetime now = datetime.now() year = now.year month = now.month day = now.day hour = now.hour minute = now.minute file_name = f'{router["server_ip"]}_{year}-{month}-{day}.txt' with open(file_name, 'w') as f: f.write(output) myparamiko.close(client)
import myparamiko ssh_client = myparamiko.connect('10.1.1.1', 22, 'andrei', 'cisco') remote_connection = myparamiko.get_shell(ssh_client) myparamiko.send_command(remote_connection, 'enable') myparamiko.send_command(remote_connection,'cisco') myparamiko.send_command(remote_connection,'terminal length 0') output = myparamiko.send_command(remote_connection,'show run') print(output.decode()) myparamiko.close(ssh_client)
import myparamiko as myp ssh_client = myp.connect('10.1.10.200', 22, 'ftrijillo', 'Ratajoa786!') remote_connection = myp.get_shell(ssh_client) myp.send_command(remote_connection, 'set cli screen-length 0') output = myp.send_command(remote_connection, 'show configuration | display set') output_str = output.decode() #print(output_str) list = output_str.split('\n') list = list[1:-1] config = '\n'.join(list) print(config) with open('router1-running-config.txt', 'w') as f: f.write(config) myp.close(ssh_client)
myparamiko.send_command(shell, 'terminal length 0') myparamiko.send_command(shell, 'enable') myparamiko.send_command(shell, 'cisco') # this is the enable command myparamiko.send_command(shell, 'show run') output = myparamiko.show(shell) # processing the output output_list = output.splitlines() output_list = output_list[11:-1] output = '\n'.join(output_list) # creating the backup filename from datetime import datetime now = datetime.now() year = now.year month = now.month day = now.day hour = now.hour minute = now.minute file_name = f'{router["server_ip"]}_{year}-{month}-{day}.txt' print(file_name) # writing the backup to the file with open(file_name, 'w') as f: f.write(output) myparamiko.close(client)