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 ssh_client = myparamiko.connect('10.58.85.18', 22, 'chance', 'Baseba!!358cd') remote_connection = myparamiko.get_shell(ssh_client) output1 = myparamiko.send_command(remote_connection, 'ifconfig') print(output1.decode())
import myparamiko import getpass password = getpass.getpass() ssh_client = myparamiko.connect('192.168.31.53', 22, 'root', password) remote_connection = myparamiko.get_shell(ssh_client) myparamiko.send_command(remote_connection,'sudo useradd -m -d /home/user2 -s /bin/bash user2') myparamiko.send_command(remote_connection, password) users = myparamiko.send_command(remote_connection,'cat /etc/passwd') print(users.decode()) myparamiko.close(ssh_client)
'passwd': password2 } router3 = { 'server_ip': '10.1.1.30', 'server_port': '22', 'user': '******', 'passwd': password3 } routers = [router1, router2, router3] for router in routers: 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) # 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
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)
def create_loopback(list, user, passwrd, ensecret): loop_count = 0 for x in list: loop_count += 1 ssh_client = myparamiko.connect(x, 22, user, passwrd) remote_connection = myparamiko.get_shell(ssh_client) command0 = 'configure terminal' command1 = 'int loopback' + str(loop_count) command2 = 'ip address ' + str(loop_count) + '.' + str( loop_count) + '.' + str(loop_count) + '.' + str( loop_count) + ' 255.255.255.0' command3 = 'no shut' command4 = 'end' command5 = 'show ip int br' myparamiko.send_command(remote_connection, 'enable') myparamiko.send_command(remote_connection, ensecret) myparamiko.send_command(remote_connection, command0) myparamiko.send_command(remote_connection, command1) myparamiko.send_command(remote_connection, command2) myparamiko.send_command(remote_connection, command3) myparamiko.send_command(remote_connection, command4) output1 = myparamiko.send_command(remote_connection, command5) with open('Output.txt', 'a', newline='') as file: file.writelines('CiscoRouter' + str(loop_count) + ' Interface Report \n') file.writelines('----------------------------- \n') file.writelines(output1.decode()) file.close()
import myparamiko ssh_client=myparamiko.connect('10.1.10.200', 22, 'ftrijillo', 'Ratajoa786!') remote_connection = myparamiko.get_shell(ssh_client) myparamiko.send_command(remote_connection, 'set cli screen-length 0') output = myparamiko.send_command(remote_connection, 'show configuration | display set') version = myparamiko.send_command(remote_connection, 'show version') print(output.decode()) print(version.decode()) myparamiko.close(ssh_client)
import myparamiko import getpass password = getpass.getpass(prompt="Password: ") ssh_client = myparamiko.connect('127.0.0.1', 22, 'ftrijillo', password) remote_connection = myparamiko.get_shell(ssh_client) myparamiko.send_command( remote_connection, 'sudo touch /Users/ftrijillo/Dropbox/Bhaloo/Python_lvl2/Udemy/Section3_Part57/file1.txt' ) myparamiko.send_command(remote_connection, password) output = myparamiko.send_command( remote_connection, 'ls -al /Users/ftrijillo/Dropbox/Bhaloo/Python_lvl2/Udemy/Section3_Part57') print(output.decode()) myparamiko.close(ssh_client)
import myparamiko import getpass username = input('Username:'******'192.168.0.133', 2299, username, password) remote_connection = myparamiko.get_shell(ssh_client) new_user = input('Enter the user you want to create:') command = 'sudo useradd -m -d /home/' + new_user + ' -s /bin/bash ' + new_user myparamiko.send_command(remote_connection, command) myparamiko.send_command(remote_connection, password) print('A new user has been created.') answer = input('Display the users ? <y|n>') if answer == 'y': users = myparamiko.send_command(remote_connection, 'cat /etc/passwd') print(users.decode()) myparamiko.close(ssh_client)
connection.send(command + "\n") time.sleep(3) output = connection.recv(4906) return output def close(ssh_client): if ssh_client.get_transport().is_active(): ssh_client.close() SCRIPT # 1 || “SHOW RUN” on Cisco IOS (Child Paramiko) import myparamiko ssh_client = myparamiko.connect("192.168.32.200", 22, "admin", "cisco") connection = myparamiko.get_shell(ssh_client) myparamiko.send_command(connection, 'terminal length 0') output = myparamiko.send_command(connection, 'show run') print(output.decode()) # output1 = paramiko1.send_command(connection,'show clock') # print(output1.decode()) myparamiko.close(ssh_client) # SCRIPT 2 Create “Users” on Linux (Child Paramiko) import myparamiko ssh_client = myparamiko.connect( "192.168.32.169", 22, "network-automation", "cisco") connection = myparamiko.get_shell(ssh_client) # ssh_estbld myparamiko.send_command(