def transfer_file(net_connect, file): net_connect.config_mode() net_connect.send_command('ip scp server enable') scp_conn = SCPConn(net_connect) s_file = file d_file = file scp_conn.scp_transfer_file(s_file, d_file)
def main(): ''' SCP transfer cisco_logging.txt to network device Use ssh_conn as ssh channel into network device scp_conn must be closed after file transfer ''' ssh_conn = ConnectHandler(**cisco_881) scp_conn = SCPConn(ssh_conn) s_file = 'cisco_logging.txt' d_file = 'cisco_logging.txt' print "\n\n" scp_conn.scp_transfer_file(s_file, d_file) scp_conn.close() output = ssh_conn.send_command("show flash: | inc cisco_logging") print ">> " + output + '\n' # Disable file copy confirmation output = ssh_conn.send_config_set(["file prompt quiet"]) # Execute config merge print "Performing config merge\n" output = ssh_conn.send_command("copy flash:cisco_logging.txt running-config") # Verify change print "Verifying logging buffer change" output = ssh_conn.send_command("show run | inc logging buffer") print ">> " + output + '\n' # Restore copy confirmation output = ssh_conn.send_config_set(["file prompt alert"])
def __init__(self, *args, **kwargs): super(SimpleSwitch13, self).__init__(*args, **kwargs) self.mac_to_port = {} self.ip_to_port = {} #We are creating a netmiko connection to the backup ryu server. #This will be used to transfer the state configuration file #between servers so that same state is maintained across both #ryu controller applications in case one goes down and comes up #again. ryu_vm = { "device_type": "linux", "ip": "10.10.10.40", "username": "******", "password": "******", "secret": "ryu", } net_connect = ConnectHandler(**ryu_vm) net_connect.enable() self.scp_conn = SCPConn(net_connect) #Check if the state configuration file exists and takes the #information into variable so that the dictionary mapping does not #have to be learned again if os.path.exists("/home/ryu/state.conf"): print("State configuration file exists. Importing from /home/ryu/state.conf...") with open("/home/ryu/state.conf","r") as state_file: lines = state_file.readlines() if lines: self.ip_to_port = ast.literal_eval(lines[0][:-1]) self.mac_to_port = ast.literal_eval(lines[1][:-1]) print(self.ip_to_port) print(self.mac_to_port)
def scp_put_file(self, source_file=None, dest_file=None): """Put file using SCP.""" try: scp = SCPConn(ssh_conn=self) scp.scp_put_file(source_file=source_file, dest_file=dest_file) except Exception: raise finally: scp.close()
def download_config(self): _f = self.f5temp_file + self.f5temp_suffix with ConnectHandler(**self.conn_dict) as remote_conn: _out = remote_conn.send_command(self.cli) syslog.syslog('@{} {}'.format(self.conn_dict['ip'], _out)) if self.cli_ans_chk(_out, _f + self._keyword): scp_conn = SCPConn(remote_conn) scp_conn.scp_get_file(_f, self.filename) syslog.syslog('scp ucs file from {} to {}'.format( self.conn_dict['ip'], self.filename))
def open(self): self.device = ConnectHandler(device_type='vyos', host=self.hostname, username=self.username, password=self.password, **self.netmiko_optional_args) try: self._scp_client = SCPConn(self.device) except: raise ConnectionException("Failed to open connection ")
def load_file(self, file, dst_file=None): if not dst_file: dst_file = file scp_status = self.ssh.send_command( 'show running-config | i "ip scp server enable"') if not scp_status: self.enable_scp() scp = SCPConn(self.ssh) scp.scp_transfer_file(file, dst_file) scp.close() if not scp_status: self.disable_scp()
def main(self): ssh_conn = ConnectHandler( device_type='cisco_ios', ip = self.ip, username = self.username, password = self.password ) config_commands = self.cmdlist output = ssh_conn.send_config_set(config_commands) print(output) for f in myfile: s_file = f d_file = f scp_conn = SCPConn(ssh_conn) scp_conn.scp_transfer_file(s_file, d_file) scp_conn.close()
def main(self): ssh_conn = ConnectHandler(device_type='cisco_ios', ip=self.ip, username=self.username, password=self.password) static = os.chdir( '/Users/shaun/dev/netmiko_django/netmiko_django/static/') myfile = os.listdir(static) for f in myfile: s_file = f d_file = f scp_conn = SCPConn(ssh_conn) scp_conn.scp_transfer_file(s_file, d_file) config_commands = self.cmdlist output = ssh_conn.send_config_set(config_commands) print(output) return output scp_conn.close()
def sendFileToMachine(host: machine.Machine, source_file, dest_file): net_device = { 'device_type': 'linux', 'ip': host.ip, 'username': host.username, 'password': host.password, } try: ssh_conn = ConnectHandler(**net_device) scp_conn = SCPConn(ssh_conn) putFileState = scp_conn.scp_put_file(source_file, dest_file) scp_conn.close() ssh_conn.disconnect() host.status = 2 # Passed return True # succeeded sending the file to machine except: print(f"Failed connect to {host.ip}") host.status = 0 # Failed return False # Filed sending the file to machine
def main(self): ssh_conn = ConnectHandler(device_type='cisco_asa', ip=self.ip, username=self.username, password=self.password) static = os.chdir( 'C:\\Users\corcoras\\Desktop\\FY14 INSTALLS\\PythonMotors\\netmiko_django\\netmiko_django\\static' ) myfile = os.listdir(static) for f in myfile: s_file = f d_file = f scp_conn = SCPConn(ssh_conn) scp_conn.scp_transfer_file(s_file, d_file) config_commands = self.cmdlist output = ssh_conn.send_config_set(config_commands) print(output) return output scp_conn.close()
def main(self): ssh_conn = ConnectHandler(device_type='cisco_ios', ip=self.ip, username=self.username, password=self.password) static = os.chdir('C:\\Users\\corcoras\\netmiko_django\\hold') print(static) myfile = os.listdir(static) print(myfile) for f in myfile: if f == 'desktop.ini': pass else: print(f) s_file = f d_file = f scp_conn = SCPConn(ssh_conn) scp_conn.scp_transfer_file(s_file, d_file) config_commands = self.cmdlist output = ssh_conn.send_config_set(config_commands) print(output) return output scp_conn.close()
def file_transfer(self): scp_conn = SCPConn(self.vyos) s_file = "/openvpn-1.key" d_file = "/config/auth/openvpn-1.key" scp_conn.scp_transfer_file(s_file, d_file)
def scp_file(self, source_file, dest_file): try: scp_transfer = SCPConn(self.device) scp_transfer.scp_put_file(source_file, dest_file) except: raise ConnectionException("SCP transfer to remote device failed")
'ip': '192.168.1.1', 'username': '******', 'password': '******', } # open file containing commands with open('commands.txt') as f: commands_list = f.read().splitlines() # Establish ssh session to device and send commands net_connect = ConnectHandler(**netscaler_device) net_connect.send_config_set(commands_list, delay_factor=4) # open secure copy protocol to transfer backup file to server ssh_conn = ConnectHandler(**netscaler_device) scp_conn = SCPConn(ssh_conn) s_file = '/var/ns_sys_backup/backupfile.tgz' d_file = '/usr/backups/backupfile.tgz' scp_conn.scp_get_file(s_file, d_file) # Close secure copy protocol session scp_conn.close() # Close ssh session net_connect.disconnect() timestr = time.strftime("%Y%m%d-%H%M%S") # Rename backup file to contain time and date stamp
def _transfer_file_scp(self, source_file, destination_file): scp_conn = SCPConn(self.device) scp_conn.scp_transfer_file(source_file,destination_file)
def getSerial(node=None, username=None, password=None): # print(f'what is the value at intf{intf} and the type is {type(intf)}') try: # regx = ['[G,P,E,F}[A-Za-z]{2,3}[0-9]{1,3}.[0-9]{1,2}[\/][0-9]{1,2}', '[G,P,E,F}[A-Za-z]{2,3}[0-9]{1,3}.[0-9]{1,2}','[G,P,E,F}[A-Za-z]{2,3}[0-9]{1,3}'] # regx = '[A-Za-z]{2,3}[0-9]{1,3}.[0-9]{1,2}[\/][0-9]{1,2}' eachLine = [] # int_for_device = [] hostname = str(node) device = { 'device_type': 'cisco_ios', #'autodetect', 'ip': hostname, 'username': username, 'password': password, 'verbose': False } ssh_session = netmiko.ConnectHandler(**device) scp_conn = SCPConn(ssh_session) #add commands you wish run here. # cmdList = [ # 'show ver | in Proc', # 'show int des | in up'+' ', # ] cmdList = ['show ver | in Proc', 'show int status | in connected'] for cmd in cmdList: print(hostname + ': ' + cmd) output = ssh_session.send_command(cmd) # if output == '': # cmdList = [ # 'show ver | in Proc', # 'show int status | in connected' # ] # for cmd in cmdList: # print(hostname + ': ' + cmd) # outputToSend = ssh_session.send_command(cmd) # eachLine = whenConnected(outputToSend) # else: eachLine = whenConnected(output) for intf in eachLine: if intf == '' or intf == 'Processor': break else: intfCmd = [ 'show int ' + intf + ' | in rate|errors|Desc', ] for cmd in intfCmd: # print(hostname + ': ' + cmd) print(cmd) output = ssh_session.send_command(cmd) for linedesc in output.splitlines(): print(linedesc) print('') #close the connections scp_conn.close() ssh_session.disconnect() # print(line) except IOError as eWriteFile: print(f'here is the exception : {eWriteFile}') scp_conn.close() ssh_session.disconnect()
def scpconnect(self): self.connect() self.scp_connect = SCPConn(self.net_connect)