C
C
Ciscoridze2015-08-27 16:02:12
WordPress
Ciscoridze, 2015-08-27 16:02:12

How to telnet to Cisco using Python?

Good afternoon.
The task is as follows: using python, make telnet on the Cisco Switch and remove the config from it.
I do it like this:

import getpass
import sys
import telnetlib

HOST = '172.17.0.42'
#user = raw_input("Enter your remote account: ")
password = "mypass"
user = "mylogin"

tn = telnetlib.Telnet(HOST,23,5)
tn.read_until("Username: ", 5)
tn.write(user + "\n")
if password:
    tn.read_until("Password: ", 5)
    tn.write(password + "\n")

tn.write('sh ver ')

data = '' 
while data.find('#') == -1:
    data = tn.read_very_eager()
print data

Display only SW2960-4floor-2#

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Arman, 2019-03-08
@Cloud47

in condition try get_field use

O
Orkhan Hasanli, 2019-03-08
@azerphoenix

It could be even simpler...
1) As you were told to use get_field() +
2)
replace:
on the:

get_field('slider-txt-1') || get_field('slider-txt-2')

T
tgz, 2015-08-27
@tgz

https://github.com/nickpegg/ciscolib

V
Valentine, 2015-08-27
@vvpoloskin

1) Read everything from the output after entering the password.
2) type \n after show ver

E
erast_petrovi4, 2018-01-12
@erast_petrovi4

there is a ciscotelnet module based on telnetlib, specifically for working with cisco via telnet:
pip install git+ https://github.com/sergeyzelyukin/cisco-telnet.git
there is a readme and an example of work on github. quite simple and comfortable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question