cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3121
Views
10
Helpful
5
Replies

Script for telnet backup configuration

theerapongpomp
Level 1
Level 1

i guys,

 

I'm quite new for automation but learning. I have a script for backup cisco configuration but my problem is I can't find the way to save the backup for each command.

 

The ideal is when typing show run, show ip int br, show inventory it will be saved as separated files not in one file.

 

Below is my code that I separated the functions but only function "show run" is working.

For the function "Show interface" it has a new file stored in but no such information in the file.

 

If I reorder the function "show interface" has info. but "show run"

 

import getpass
import sys
import telnetlib

 

user = input('Enter your telnet username: ')
password = getpass.getpass()
file = open('hostIP')

 

def show_run():
#show running config
tel.write(b"terminal length 0\n")
tel.write(b"show run\n")
tel.write(b"exit\n")
readoutput = tel.read_all()
saveoutput = open("configuration/running_config_" + HOST + ".txt", "w")
saveoutput.write(readoutput.decode('ascii'))
saveoutput.write("\n")

 

def show_interface():
#show running config
tel.write(b"terminal length 0\n")
tel.write(b"show ip int br\n")
tel.write(b"exit\n")
readoutput = tel.read_all()
saveoutput = open("/configuration/interface_" + HOST + ".txt", "w")
saveoutput.write(readoutput.decode('ascii'))
saveoutput.write("\n")


for IP in file:
IP=IP.strip()
print ('Get config from Switch ' + (IP))
HOST = IP
tel = telnetlib.Telnet(HOST,23, timeout=1)
tel.read_until(b'Username: ')
tel.write(user.encode('ascii') + b'\n')
if password:
tel.read_until(b'Password: ')
tel.write(password.encode('ascii') + b'\n')
else:
print("Wrong Pass!!")
tel.write(b"enable\n")
tel.write(password.encode('ascii') + b'\n')
show_run()
show_interface()

 

5 Replies 5

Dan Frey
Cisco Employee
Cisco Employee

The saveoutput has a relative path in "show run" and absolute path in the other def.  Not sure this is intentional?

Hi Daniel,

 

I put them wrong. Actually the code has the same absolute path "/root/Python/configuration/".

Both can create the new file storing in the path, but only first function has information in it and second function just leaves a blank file.

 

 

If you look at the file stored for "show run" the last line will have "exit" as input to the CLI.   This is terminating the telnet process and tel.write(b"exit\n") is responsible.    tel_readall() requires the connection to be closed to read the buffer.     read_until() can use a timeout to read the buffer and does not require the connection to be closed.   The last definition can use read_all() while all previous definitions can use read_until() with a timeout specified.  

 

 

 

 

import getpass
import sys
import telnetlib

user = raw_input("Username: ")
password = getpass.getpass()
file = open('hostIP')

def show_run():
  tel.write(b"terminal length 0\n")
  tel.write(b"show run\n")
#  tel.write(b"exit\n")
  readoutput = tel.read_until(b">", timeout=5)
  saveoutput = open("/home/dafrey/running_config_" + HOST + ".txt", "w")
  saveoutput.write(readoutput.decode('ascii'))
  saveoutput.write("\n")

def show_interface():
  tel.write(b"terminal length 0\n")
  tel.write(b"show ip int br\n")
  tel.write(b"exit\n")
  readoutput = tel.read_all()
  saveoutput = open("/home/dafrey/interface_" + HOST + ".txt", "w")
  saveoutput.write(readoutput.decode('ascii'))
  saveoutput.write("\n")

for IP in file:
  IP=IP.strip()
  print ('Get config from Switch ' + (IP))
  HOST = IP
  tel = telnetlib.Telnet(HOST,23, timeout=1)
  tel.read_until(b'Username: ')
  tel.write(user.encode('ascii') + b'\n')
  if password:
    tel.read_until(b'Password: ')
    tel.write(password.encode('ascii') + b'\n')
  else:
    print("Wrong Pass!!")
    tel.write(b"enable\n")
    tel.write(password.encode('ascii') + b'\n')
  show_run()
  show_interface()



 

 

 

 

Hi Daniel,

 

Appreciate your suggestion.

But once running I got a type of error below. I tried many way to put argument info for read_until() it has the same error.

 

 

 

Traceback (most recent call last):
File "Backup.py", line 42, in <module>
show_run()
File "Backup.py", line 9, in show_run
readoutput = tel.read_until (b'SW1' , timeout=5)
File "/usr/lib/python3.8/telnetlib.py", line 329, in read_until
return self.read_very_lazy()
File "/usr/lib/python3.8/telnetlib.py", line 405, in read_very_lazy
raise EOFError('telnet connection closed')
EOFError: telnet connection closed

 

------------------------------------------------------

Traceback (most recent call last):
File "Backup.py", line 42, in <module>
show_run()
File "Backup.py", line 9, in show_run
readoutput = tel.read_until (b'SW1>' , timeout=5)
File "/usr/lib/python3.8/telnetlib.py", line 329, in read_until
return self.read_very_lazy()
File "/usr/lib/python3.8/telnetlib.py", line 405, in read_very_lazy
raise EOFError('telnet connection closed')

 

-------------------------------------------------------

 

Traceback (most recent call last):
File "Backup.py", line 42, in <module>
show_run()
File "Backup.py", line 9, in show_run
readoutput = tel.read_until (b'SW1#' , timeout=5)
File "/usr/lib/python3.8/telnetlib.py", line 329, in read_until
return self.read_very_lazy()
File "/usr/lib/python3.8/telnetlib.py", line 405, in read_very_lazy
raise EOFError('telnet connection closed')
EOFError: telnet connection closed

 

 

Thanks.

This is working for me on python 2 and 3.   Can you post a copy of the entire script that is generating the error?

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: