Created
February 25, 2020 02:23
-
-
Save iyre/bd9a708fdb016ff7374544568c14a9ad to your computer and use it in GitHub Desktop.
python telnetlib to configure cisco ios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getpass | |
import sys | |
import telnetlib | |
import os | |
# Variables | |
tacacs = '.tacacslogin' | |
commandsfile = 'commands.txt' | |
hostsfile = 'hosts.txt' | |
verbose = "yes" | |
def userlogin(): | |
# Getting login credentials | |
global username, password, enable | |
if os.path.isfile(tacacs): | |
login = open(tacacs, "r") | |
username = login.readline() | |
username = username.replace("\n", "") | |
password = login.readline() | |
password = password.replace("\n", "") | |
enable = login.readline() | |
enable = enable.replace("\n", "") | |
login.close() | |
else: | |
print(tacacs, "not found.\n") | |
#username = input("Username: ") | |
#password = getpass.getpass("User Password: ") | |
#enable = getpass.getpass("Enable Password: ") | |
print('Using default login.\n') | |
username = 'Cisco' | |
password = 'Cisco' | |
enable = 'Cisco' | |
return username, password, enable | |
def login(): | |
# Logging in with username, password, and eable password | |
global username, password, enable | |
telnet.read_until(b"Username: ") | |
telnet.write(username.encode('ascii') + b"\n") | |
if password: | |
telnet.read_until(b"Password: ") | |
telnet.write(password.encode('ascii') + b"\n") | |
if enable: | |
telnet.read_until(b'>') | |
telnet.write(b"enable\n") | |
telnet.read_until(b"Password: ") | |
telnet.write(enable.encode('ascii') + b"\n") | |
def sessioncommands(): | |
# Executing commands on the host | |
global commands | |
print("Executing Commands on", host2login) | |
if os.path.isfile(commandsfile): | |
commands = open(commandsfile, "r") | |
try: | |
for cmd2exe in commands: | |
telnet.write(cmd2exe.encode('ascii')) | |
finally: | |
commands.close() | |
else: | |
print(commandsfile, " doesn't exist") | |
telnet.write(b"exit\n") | |
# Displaying the results | |
if verbose == "yes": | |
telnet.write(b"exit\nexit\nexit\nexit\n") | |
output = telnet.read_all().decode('ascii') | |
if "% " in output: | |
print("Error: ", output) | |
sys.exit() | |
else: | |
print(output) | |
print("Logging out of", host2login) | |
# Doing work | |
userlogin() | |
if os.path.isfile(hostsfile): | |
hosts = open(hostsfile, "r") | |
while 1: | |
host2login = hosts.readline() | |
host2login = host2login.replace("\n", "") | |
if not host2login: | |
break | |
else: | |
print("Logging into", host2login) | |
telnet = telnetlib.Telnet(host2login) | |
login() | |
sessioncommands() | |
hosts.close() | |
else: | |
host2login = input("Host: ") | |
print("Logging into", host2login) | |
telnet = telnetlib.Telnet(host2login,timeout=1) | |
login() | |
sessioncommands() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config t | |
dot11 ssid ##SSID## | |
authentication open | |
authentication key-management wpa version 2 | |
guest-mode | |
wpa-psk ascii ##PASSWORD## | |
exit | |
interface Dot11Radio0 | |
encryption mode ciphers aes-ccm | |
ssid ##SSID## | |
no shutdown | |
exit | |
exit | |
copy running-config startup-config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192.168.1.167 | |
192.168.1.168 | |
192.168.1.169 | |
192.168.1.170 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can modify function userlogin() with try-except for Hosts are not available during bulk update =