Network Automation NAPALM (Python script example for network engineers)
In this network automation example, we will use the get_facts() method to export details from the device and save the output in a text file.
Here, I have created an empty file. The name of the file is RouterConfig.txt. When the program is executed, the command output will be saved in the file.

First, import get_network_driver from the NAPALM library. We are also importing JSON to format the output for saving it into a file.
from napalm import get_network_driver
import json
As we are trying to connect to a Cisco IOS router, we have to use the ‘ios’ driver.
driver = get_network_driver('ios')
Now, submit the credentials and IP address to connect to the router. Here, i am trying to connect to a Cisco router hosted in the Sandbox.
device = driver('131.226.217.143','developer','C1sco12345')
Opening a file for the purpose of saving the output received from a device. We are using the device.open() method to connect to a device. Once connected, send the get_facts command to retrieve device information. The program will also display a message stating “Data copied to a file”.
with open('C:/Users/Rohan.Jadhav/AppData/Roaming/JetBrains/PyCharmCE2022.2/scratches/RouterConfig.txt','w') as f:
try:
device.open()
output = json.dumps(device.get_facts(), indent=2)
f.write(output)
print('Data copied to file')
except:
print('Connection Failed')
device.close()
Output

Complete Code
from napalm import get_network_driver
import json
driver = get_network_driver('ios')
device = driver('131.226.217.143','developer','C1sco12345')
with open('C:/Users/Rohan.Jadhav/AppData/Roaming/JetBrains/PyCharmCE2022.2/scratches/RouterConfig.txt','w') as f:
try:
device.open()
output = json.dumps(device.get_facts(), indent=2)
f.write(output)
print('Data copied to file')
except:
print('Connection Failed')
device.close()