Python learning for Network Engineers Part 19 | Netmiko ssh and configure Cisco using text file

preview_player
ะŸะพะบะฐะทะฐั‚ัŒ ะพะฟะธัะฐะฝะธะต
๐—™๐—ผ๐—ฟ ๐—–๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜๐—ฒ ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—™๐—ผ๐—ฟ ๐—ก๐—ฒ๐˜๐˜„๐—ผ๐—ฟ๐—ธ ๐—˜๐—ป๐—ด๐—ถ๐—ป๐—ฒ๐—ฒ๐—ฟ๐˜€ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ:
๐—˜๐—ป๐—ฟ๐—ผ๐—น๐—น ๐—ณ๐—ผ๐—ฟ ๐—ฏ๐—ฒ๐—น๐—ผ๐˜„ ๐—จ๐—ฑ๐—ฒ๐—บ๐˜† ๐—–๐—น๐—ฎ๐˜€๐˜€: ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ ๐•๐ž๐ซ๐ฌ๐ข๐จ๐ง
๐‘ซ๐’Š๐’”๐’„๐’๐’–๐’๐’•๐’†๐’… ๐‘น๐’†๐’‡๐’†๐’“๐’“๐’‚๐’ ๐‘ณ๐’Š๐’๐’Œ:

from netmiko import ConnectHandler
with open('15_devices') as IP_LIST:
for IP in IP_LIST:
RTR = {
'ip': IP,
'username': 'admin',
'password': 'admin',
'device_type': 'cisco_ios',
}

net_connect = ConnectHandler(**RTR)

with open('15_config') as CONFIG_LINES:
CONFIG = CONFIG_LINES.read()
print(output)

print(output)
ะ ะตะบะพะผะตะฝะดะฐั†ะธะธ ะฟะพ ั‚ะตะผะต
ะšะพะผะผะตะฝั‚ะฐั€ะธะธ
ะะฒั‚ะพั€

๐—ฃ๐—น๐—ฎ๐˜†๐—น๐—ถ๐˜€๐˜: ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ก๐—ฒ๐˜๐˜„๐—ผ๐—ฟ๐—ธ ๐—˜๐—ป๐—ด๐—ถ๐—ป๐—ฒ๐—ฒ๐—ฟ๐˜€(๐Ÿญ๐Ÿฎ๐Ÿฌ+ ๐—ฉ๐—ถ๐—ฑ๐—ฒ๐—ผ๐˜€)
โ˜ธ๐—ฃ๐—น๐—ฒ๐—ฎ๐˜€๐—ฒ ๐—ณ๐—ผ๐—น๐—น๐—ผ๐˜„ ๐—ฏ๐—ฒ๐—น๐—ผ๐˜„ ๐—š๐—ถ๐˜๐—›๐˜‚๐—ฏ ๐—ฃ๐—ฎ๐—ด๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐˜๐—ต๐—ฒ ๐—น๐—ฎ๐˜๐—ฒ๐˜€๐˜ ๐—ฐ๐—ผ๐—ฑ๐—ฒ๐˜€:
๐—ฃ๐—น๐—ฎ๐˜†๐—น๐—ถ๐˜€๐˜: ๐—–๐—ถ๐˜€๐—ฐ๐—ผ ๐—ก๐—ซ๐—”๐—ฃ๐—œ-๐—–๐—Ÿ๐—œ ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—”๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: ๐—ก๐—ฒ๐˜…๐˜‚๐˜€ ๐Ÿต๐—ธ :๐—ฃ๐—ฎ๐—ฟ๐˜€๐—ฒ ๐—ท๐˜€๐—ผ๐—ป ๐—ซ๐— ๐—Ÿ ๐—–๐—ผ๐—ป๐—ณ๐—ถ๐—ด๐˜‚๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐——๐—ฎ๐˜๐—ฎ
๐—ฃ๐—น๐—ฎ๐˜†๐—น๐—ถ๐˜€๐˜: ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐˜๐—ผ ๐—ฃ๐—ฎ๐—ฟ๐˜€๐—ฒ ๐—–๐—ถ๐˜€๐—ฐ๐—ผ ๐—–๐—ผ๐—ป๐—ณ๐—ถ๐—ด๐˜‚๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—ฅ๐—ฒ๐—ด๐—˜๐˜…:๐—ฟ๐—ฒ ๐—ง๐˜‚๐˜๐—ผ๐—ฟ๐—ถ๐—ฎ๐—น(๐Ÿญ๐Ÿฑ+ ๐—ฉ๐—ถ๐—ฑ๐—ฒ๐—ผ๐˜€)
๐—ฃ๐—น๐—ฎ๐˜†๐—น๐—ถ๐˜€๐˜: ๐—–๐—ถ๐˜€๐—ฐ๐—ผ ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—ณ๐—ฎ๐—ฐ๐—ฒ ๐—–๐—ผ๐—ป๐—ณ๐—ถ๐—ด๐˜‚๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฃ๐—ฎ๐—ฟ๐˜€๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐— ๐˜‚๐—น๐˜๐—ถ๐˜๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—ฅ๐—ฒ๐—ด๐—˜๐˜… ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜:๐—ง๐—ฎ๐—ฏ๐˜‚๐—น๐—ฎ๐˜๐—ฒ ๐—ง๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ฒ๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ(๐Ÿฐ ๐—ฉ๐—ถ๐—ฑ๐—ฒ๐—ผ๐˜€)
๐—ฃ๐—น๐—ฎ๐˜†๐—น๐—ถ๐˜€๐˜: ๐—˜๐—ป๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐— ๐˜‚๐—น๐˜๐—ถ๐˜๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐—ถ๐—ป ๐—–๐—ถ๐˜€๐—ฐ๐—ผ ๐—–๐—ผ๐—ป๐—ณ๐—ถ๐—ด ๐—•๐—ฎ๐—ฐ๐—ธ๐˜‚๐—ฝ ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜ ๐—ณ๐—ผ๐—ฟ ๐—ฃ๐—ฎ๐—ฟ๐—ฎ๐—น๐—น๐—ฒ๐—น ๐—ฒ๐˜…๐—ฒ๐—ฐ๐˜‚๐˜๐—ถ๐—ผ๐—ป:๐—ง๐—ต๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐—ง๐˜‚๐˜๐—ผ๐—ฟ๐—ถ๐—ฎ๐—น(๐Ÿฐ ๐—ฉ๐—ถ๐—ฑ๐—ฒ๐—ผ๐˜€)
๐—ฃ๐—น๐—ฎ๐˜†๐—น๐—ถ๐˜€๐˜: ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด ๐—™๐—ผ๐—ฟ ๐—ก๐—ฒ๐˜๐˜„๐—ผ๐—ฟ๐—ธ ๐—˜๐—ป๐—ด๐—ถ๐—ป๐—ฒ๐—ฒ๐—ฟ๐˜€ : ๐—ก๐—ฒ๐˜๐˜„๐—ผ๐—ฟ๐—ธ ๐—”๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐˜๐˜‚๐˜๐—ผ๐—ฟ๐—ถ๐—ฎ๐—น ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ถ๐˜€๐—ฐ๐—ผ ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ๐˜€(๐Ÿฏ๐Ÿฌ+ ๐—ฉ๐—ถ๐—ฑ๐—ฒ๐—ผ๐˜€)

NetworkEvolution
ะะฒั‚ะพั€

strange, the scripts gave me error "Pattern not detected: 'int\\ lo1\\\nip\\ add\\ 1\\.1\\.1\\.1\\ 255\\.255\\.255\\.255\\\nno\\ shut' in output". Until I realized that the output of read file's content is actually string type, and I had to replace the method from read() into readlines()

sutiono
ะะฒั‚ะพั€

Perhaps it is covered in an other video, but it seems more optimal to have your 15_device.txt file formatted to JSON (or another standard data format) saved and imported as a 15_devices.json

Random_user-sznk
ะะฒั‚ะพั€

Nice playlist. I am facing issues to fetch files from notepad. Don't know where to save it in windows 10. Could any one pls help

adityaprasaddash
ะะฒั‚ะพั€

It's fine, but the same loop back on both devices?

NetworkTechnician
ะะฒั‚ะพั€

how to input the enable secret password ??

I try with net_connect.enable() . But not type automatic enable and password

gusyee
ะะฒั‚ะพั€

Hello Team, Its an amazing playlist. But in this video script is not working properly.
Its working only when I have single line in config file. however, devices are fine with multiple lines.
please guide.

HimanshuGupta
ะะฒั‚ะพั€

I tried the script, but only my last device gets read out of the device list. Any idea of why that would happen?

mjk
ะะฒั‚ะพั€

mjk2374
, the same problem! result of ther script:


Connecting to device 10.24.82.33

Connecting to device 10.24.81.161
config term
Enter configuration commands, one per line. End with CNTL/Z.
a-rtr(config)#int lo1
a-rtr(config-if)#des TEST
a-rtr(config-if)#ip add 10.5.5.5 255.255.255.255
a-rtr(config-if)#exi
a-rtr(config)#exi
a-rtr#wr
Building configuration...
[OK]
a-rtr#


As we can see the script worked correctly only on the last router...if it will be 5 routers he will print "Connecting to device X.X.X.X" 5 times and only last router will be with commands...This is because the end of the cycle....and I donโ€™t know how to make the script set up commands on each router...can you help me? Here the cript
from netmiko import ConnectHandler
import getpass
import sys


USER = input('Username: ')
PASSWORD = getpass.getpass()


IP_LIST = open('3_devices.txt')
for IP in IP_LIST:
RTR = {'device_type': 'cisco_ios',
'ip': IP,
'username': USER,
'password': PASSWORD}




print ('Connecting to device ' + IP)


with ConnectHandler(**RTR) as ssh:
ssh.enable()


output =

print(output)

When I try to insert with ConnectHandler (** RTR) as ssh into the cycle i have a mistake:
File "file5.py", line 23
with ConnectHandler(**RTR) as ssh:
^
TabError: inconsistent use of tabs and spaces in indentation
Help plz

milan
ะะฒั‚ะพั€

Hello Team, Script is not working properly for me getting error. And i'm using Linux OS. Can you please help in it.


Script:-
from netmiko import ConnectHandler
with open('Devices') as IP_LIST:
for IP in IP_LIST:
Switches = {
'ip': IP,
'username': 'cisco',
'password': 'cisco',
'device_type': 'cisco_ios',
}

print ('connecting to device ' + IP)

net_connect = ConnectHandler(**Switches)

with open('config') as CONFIG_LINES:
CONFIG = CONFIG_LINES.read()
output =
print(output)

output = ip int brief')
print(output)

Error:-

connecting to device 192.168.122.51

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/paramiko/channel.py", line 699, in recv
out = self.in_buffer.read(nbytes, self.timeout)
File "/usr/local/lib/python3.8/dist-packages/paramiko/buffered_pipe.py", line 164, in read
raise PipeTimeout()


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 569, in _read_channel_expect
new_data =
File "/usr/local/lib/python3.8/dist-packages/paramiko/channel.py", line 701, in recv
raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "abc.py", line 18, in <module>
output =
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 1875, in send_config_set
new_output =
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 651, in read_until_pattern
return self._read_channel_expect(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/netmiko/base_connection.py", line 579, in _read_channel_expect
raise NetmikoTimeoutException(
Timed-out reading channel, data not available.

mukeshyadav