My script displaying text (config file) as expected on screen but with some extra characters when saved in a file /u/zxof Python Education

I have a simple script using nornir to run a command (config file) on a remote device, display it on screen and save it to a file.

The output looks fine on screen but certain lines of the saved config file has extra characters. I’ve tried different encodings without any luck.

I suppose I can clean up the extra characters but I would like to know the root cause of them problem, what’s causing the difference between the screen output and saved file.

###File type using linux 'file' command $ file -i xxxxx.conf xxxxx.conf: text/plain; charset=us-ascii $ file xxxxx.conf xxxxx.conf: ASCII text, with escape sequences ####Python code####### def store_olt_vlanconfig(): #write command output to a file origconfig_filename = os.path.join( file_path, hostprefix + argobj.cabname + ".conf" ) file = open(origconfig_filename, mode="w", encoding="utf-8") file.write(config_raw) file.write("n") file.close() #SSH to device, run a command and print on screen nornir_task = nr.filter(F(hostname__contains=argobj.cabname) &F(role__eq="olt")) results = nornir_task.run(task=netmiko_send_command, command_string="info configure") config_raw = results[hostprefix + argobj.cabname].result print(config_raw) #save original config file store_olt_vlanconfig() ###Screen output: User name: xxxxx Password: configure bridge #----------------------------------------------------------------------------------------------------- echo "bridge" #----------------------------------------------------------------------------------------------------- port 1/1/1/1/1/14/1 vlan-id 1001 tag single-tagged l2fwder-vlan 300 vlan-scope local exit exit #------------------------------------------------------------------------------------------------------ 

saved file content using vim

submitted by /u/zxof
[link] [comments]

​r/learnpython I have a simple script using nornir to run a command (config file) on a remote device, display it on screen and save it to a file. The output looks fine on screen but certain lines of the saved config file has extra characters. I’ve tried different encodings without any luck. I suppose I can clean up the extra characters but I would like to know the root cause of them problem, what’s causing the difference between the screen output and saved file. ###File type using linux ‘file’ command $ file -i xxxxx.conf xxxxx.conf: text/plain; charset=us-ascii $ file xxxxx.conf xxxxx.conf: ASCII text, with escape sequences ####Python code####### def store_olt_vlanconfig(): #write command output to a file origconfig_filename = os.path.join( file_path, hostprefix + argobj.cabname + “.conf” ) file = open(origconfig_filename, mode=”w”, encoding=”utf-8″) file.write(config_raw) file.write(“n”) file.close() #SSH to device, run a command and print on screen nornir_task = nr.filter(F(hostname__contains=argobj.cabname) &F(role__eq=”olt”)) results = nornir_task.run(task=netmiko_send_command, command_string=”info configure”) config_raw = results[hostprefix + argobj.cabname].result print(config_raw) #save original config file store_olt_vlanconfig() ###Screen output: User name: xxxxx Password: configure bridge #—————————————————————————————————– echo “bridge” #—————————————————————————————————– port 1/1/1/1/1/14/1 vlan-id 1001 tag single-tagged l2fwder-vlan 300 vlan-scope local exit exit #—————————————————————————————————— saved file content using vim submitted by /u/zxof [link] [comments] 

I have a simple script using nornir to run a command (config file) on a remote device, display it on screen and save it to a file.

The output looks fine on screen but certain lines of the saved config file has extra characters. I’ve tried different encodings without any luck.

I suppose I can clean up the extra characters but I would like to know the root cause of them problem, what’s causing the difference between the screen output and saved file.

###File type using linux 'file' command $ file -i xxxxx.conf xxxxx.conf: text/plain; charset=us-ascii $ file xxxxx.conf xxxxx.conf: ASCII text, with escape sequences ####Python code####### def store_olt_vlanconfig(): #write command output to a file origconfig_filename = os.path.join( file_path, hostprefix + argobj.cabname + ".conf" ) file = open(origconfig_filename, mode="w", encoding="utf-8") file.write(config_raw) file.write("n") file.close() #SSH to device, run a command and print on screen nornir_task = nr.filter(F(hostname__contains=argobj.cabname) &F(role__eq="olt")) results = nornir_task.run(task=netmiko_send_command, command_string="info configure") config_raw = results[hostprefix + argobj.cabname].result print(config_raw) #save original config file store_olt_vlanconfig() ###Screen output: User name: xxxxx Password: configure bridge #----------------------------------------------------------------------------------------------------- echo "bridge" #----------------------------------------------------------------------------------------------------- port 1/1/1/1/1/14/1 vlan-id 1001 tag single-tagged l2fwder-vlan 300 vlan-scope local exit exit #------------------------------------------------------------------------------------------------------ 

saved file content using vim

submitted by /u/zxof
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *