Nowadays Devnet or Devops has become very popular. Using Python in general or netmiko for ssh in particular has become extremely important in the networking industry. However, very few articles talk about netmiko support for Nokia’s SROS. Today I will show you some basic ways to approach Netmiko using Python to process data on Nokia SROS, the first step to becoming Devops.
Mục lục
Prepare Nokia SROS router to test with python netmiko
First you must have a router device running SROS like the 7750SR series. You can use real device or use emulator to achieve it. If you want to simulate, you can refer to the article How to emulate Nokia Service Router (TiMOS SROS). You can download the lab file (unl) and the code at the bottom of the article.
Once you have the environment, you need to enable SSH for the router. As my lab model, a 7750SR device connects to the computer through the Net network. The IP for SSH is the ip of the direct connection interface: 10.18.8.158/24.

Here is the sample configuration I made:
#Enable SSH
/configure system security ssh version 2
/configure system security ssh preserve-key
#Open port
/configure port 1/1/1 no shutdown
#Configure router interface
/configure router interface "ToNet"
address 10.18.8.158/24
port 1/1/1
no shutdown
#Configure default route to outside
/configure router static-route-entry 0.0.0.0/0
next-hop 10.18.8.1 no shutdown
When configuration is complete, you need to make sure that from the side of your PC you can SSH to the 7750SR router. Let ping from PC and try ssh from putty!
Next, you need to install Netmiko library into python to be able to use it.
pip install netmiko
After that, let’s start coding!
Send show command and print to screen
The simplest approach that I recommend to you is to execute the show command on the device via Netmiko. The following are the lines of code to execute the “admin display-config” command and display it on the python screen.
# Import library
from netmiko import ConnectHandler
device = {
'device_type': 'nokia_sros', #device type is fixed, you shouldn't change this
'ip': '10.18.8.158', #ip connect to your device via SSH
'username': 'admin', #default username
'password': 'admin' #default password
}
# Establish SSH connection to device
SSH = ConnectHandler(**device)
# Send a command and print output
OUTPUT = SSH.send_command('admin display-config')
print(OUTPUT)
# Close SSH connection
SSH.disconnect()
Let’s take a screenshot of your results and post them in the comments section of the post!
Send show command and save to the files
Next, execute the show command and then write the result to a text file. This code will execute the command to display the entire configuration on the router device 7750SR and save it to the data.txt file.
# Import library
from netmiko import ConnectHandler
device = {
'device_type': 'nokia_sros', #device type is fixed, you shouldn't change this
'ip': '10.18.8.158', #ip connect to your device via SSH
'username': 'admin', #default username
'password': 'admin' #default password
}
# Establish SSH connection to device
SSH = ConnectHandler(**device)
# Send a command and save to file
OUTPUT = SSH.send_command('admin display-config')
file = open("data.txt","w") #Save admin display-config to file data.txt
file.write(OUTPUT)
file.close()
# Close SSH connection
SSH.disconnect()
After executing python, check the directory containing the python file. If there is a data.txt file and it contains the correct information, you are successful!
Send configuration commands
On a higher level, let’s do some basic command line configuration on the 7750 SROS router in python using the Netmiko library. Perform loopback1 configuration with ip address 1.1.1.1/24.
# Import library
from netmiko import ConnectHandler
device = {
'device_type': 'nokia_sros',
'ip': '10.18.8.158',
'username': 'admin',
'password': 'admin'
}
# Establish SSH connection to device
SSH = ConnectHandler(**device)
# Send Config
SSH.send_config_set(["/configure router interface loopback1","address 1.1.1.1/24","loopback",])
#Check the result:
OUTPUT=SSH.send_command("show router interface")
print(OUTPUT)
# Close SSH connection
SSH.disconnect()
After running will display the result as below:

Send configuration commands in bulk
If a single command line isn’t hard enough for you, run a batch of commands onto the 7750 SROS. Let’s configure 3 more loopbacks on Nokia 7750 device using python. The list of loopbacks is as follows:
Loopback Name | IP address |
---|---|
loopback10 | 172.1.10.1/24 |
loopback20 | 172.1.20.1/24 |
loopback30 | 172.1.30.1/24 |
The lines highlighted in red execute the command:
# Import library
from netmiko import ConnectHandler
device = {
'device_type': 'nokia_sros',
'ip': '10.18.8.158',
'username': 'admin',
'password': 'admin'
}
# Establish SSH connection to device
SSH = ConnectHandler(**device)
# Create a dictionary
INT={
"loopback10": "172.1.10.1/24",
"loopback20": "172.1.20.1/24",
"loopback30": "172.1.30.1/24",
}
# Implement a for loop
for i in INT:
SSH.send_config_set(["/configure router interface "+i,"address "+INT[i],"loopback"])
#Check the result:
OUTPUT=SSH.send_command("show router interface")
print(OUTPUT)
# Close SSH connection
SSH.disconnect()
Output Send configuration commands in bulk:

Execute batch command from text file
Finally, you can precompile the command to a text file and perform batch landing on the 7750 SR device in Python using the Netmiko library. We create a text file named 5_Command_file and the content inside is as follows:
/configure router interface LB100
address 192.168.10.1/24
loopback
/configure router interface LB200
address 192.168.20.1/24
loopback
/configure router interface LB300
address 192.168.30.1/24
loopback
/configure router interface LB400
address 192.168.40.1/24
loopback
/configure router interface LB500
address 192.168.50.1/24
loopback
/admin save
Save the file 5_Command_file.txt in the same directory as the .py. Execute the code as below, notice the lines highlighted in red:
# Import library
from netmiko import ConnectHandler
device = {
'device_type': 'nokia_sros',
'ip': '10.18.8.158',
'username': 'admin',
'password': 'admin'
}
# Establish SSH connection to device
SSH = ConnectHandler(**device)
# Execute batch command from text file
SSH.send_config_from_file(config_file="5_Command_file.txt")
#Check the result:
OUTPUT=SSH.send_command("show router interface | match LB")
print(OUTPUT)
# Close SSH connection
SSH.disconnect()
Execute batch command from text file result:

To perform the configuration set on many devices at the same time with different usernames and passwords, we should save and process it in an excel file with the openpyxl library. If you need guidance, please leave a comment below. Below is the entire code, lab file and text file guided in the article. You can download it from my github below: