Remote SSH and Configuring code help

Dec 13, 2023
Reaction score
The following is my code to remotely connect to a network device (cisco router), It currently is not functional, but I don’t see any errors. It also needs to be able to "Configure a loopback and at least one other interface with an appropriate IP address on a router within your topology. Configure OSPF protocol on a network device within the topology and advertise appropriately.

I have tried to run the code on Netlab to connect to the router remoutly, the connection intially
works, but times out. I assume the commands I am sending over are coded incorrectly.

When running the code and after entering the username, password and ssh choice, I received this response.
INFO:paramiko.transport:Connected (version 1.99, client Cisco-1.25)
INFO:paramiko.transport:Authentication (password) successful!
INFO:root:Connection established
ERROR:root:An error occurred: Timed-out reading channel, data not available.
INFO:root:The connection has concluded

I am also reviving the message the the line of net_connect.disconect() might be undefined.

Any help would be greatly appreciated.

#Getting imports

from netmiko import ConnectHandler
import getpass
import difflib
import logging



#Saving config

def save_config_to_file(config, filename):
with open(filename, 'w') as config_file:
config_file.write(config)'Configuration saved to {filename}')

#Displaying differences

def show_differences(config1, config2):
difference = difflib.Differ()
diff = list(, config2.splitlines()))

for line in diff:
if line.startswith('- '):'Present in ({config1}): {line[2:]}')
elif line.startswith('+ '):'Present in ({config2}): {line[2:]}')

#Config commands

def configure_device(net_connect):
loopback_config = [
'conf t\n'
'interface Loopback0\n',
'ip address\n',

acl_config = [
'ip access-list extended MY_ACL\n',
'permit ip any\n',
'deny ip any any\n',

interface0_config = [
'interface GigabitEthernet0/0\n',
'ip address\n',

interface1_config = [
'interface GigabitEthernet0/1\n',
'ip address\n',
configs = loopback_config + acl_config + interface0_config + interface1_config
output = net_connect.send_config_set(configs)

#User info and choice of connection type

def main():
host = ''
username = input('Please enter your username: ')
password = getpass.getpass('Please enter your password: ')
secret = 'cisco'

choice = input('Would you like to connect by using telnet or ssh? ')

if choice.lower() == 'telnet':
device_type = 'cisco_ios_telnet'
elif choice.lower() == 'ssh':
device_type = 'cisco_ios'
raise ValueError('Invalid - Please choose either telnet or ssh: ')

device = {
'device_type': device_type,
'host': host,
'username': username,
'password': password,
'secret': secret,
'port': 22 if device_type == 'cisco_ios' else 23,
'timeout': 100,

#Connecting to device

with ConnectHandler(**device) as net_connect:'Connection established')

#Sending config commands


# Saving config
running_configuration = net_connect.send_command('show running-config')
local_config_file_name = 'local_config.txt'
save_config_to_file(running_configuration, local_config_file_name)'The configuration has been saved to {local_config_file_name}')

#comaprign config

local_config_file_name = 'local_config.txt'

with open(local_config_file_name, 'r') as local_config_file:
local_config =

if running_configuration and local_config:
if running_configuration == local_config:'The running configuration is the same as the local configuration')
logging.warning('The running configuration does not match the local configuration: ')
show_differences(running_configuration, local_config)
logging.error('Failed to access configurations')

except FileNotFoundError:
logging.error(f'The local configurations file, ({local_config_file_name}), could not be found')

except Exception as e:
logging.error(f'An error occurred: {e}')

#Ending connection'The connection has concluded')

if name == "main":

GitHub link to code -

Thank you

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Latest member

Latest Threads