Python: Script para implementar TACACS+ en Cisco IOS
1. Crear archivo de textos donde introduciremos las IPs de los dispositivos Cisco que nos conectaremos via SSH -> nano /home/angel/scripts/networkdevices.txt
192.168.1.10
192.168.1.11
2. Crear un script invocando a los modulos de paramiko, time y get pass. Como consejo el script lo guardaremos en la misma carpeta que el archivo de las IPs (networkdevices.txt) ->
import paramiko
import time
import getpass
username = "usuario"
password = "contraseña"
f = open ("/home/angel/scripts/networkdevices.txt")
for line in f:
ip_address = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print ("Successful connection", ip_address)
remote_connection = ssh_client.invoke_shell()
remote_connection.send("configure terminal\n")
remote_connection.send("aaa new-model\n")
remote_connection.send("\n")
remote_connection.send("tacacs server tacacs1\n")
remote_connection.send("address ipv4 192.168.100.10\n")
remote_connection.send("key <presharedkey>\n")
remote_connection.send("exit\n")
remote_connection.send("tacacs server tacacs2\n")
remote_connection.send("address ipv4 192.168.110.10\n")
remote_connection.send("key <presharedkey>\n")
remote_connection.send("aaa group server tacacs+ tacacs-grupo\n")
remote_connection.send("server name tacacs1\n")
remote_connection.send("server name tacacs2\n")
remote_connection.send("exit\n")
remote_connection.send("aaa authentication login default group tacacs-grupo local\n")
remote_connection.send("aaa authentication enable default group tacacs-grupo enable\n")
remote_connection.send("aaa authorization exec default group tacacs-grupo local if-authenticated\n")
remote_connection.send("aaa authorization commands 1 default group tacacs-grupo local if-authenticated\n")
remote_connection.send("aaa authorization commands 15 default group tacacs-grupos local if-authenticated\n")
remote_connection.send("aaa authorization config-commands\n")
remote_connection.send("aaa authorization console\n")
remote_connection.send("aaa accounting system default start-stop group tacacs-grupo\n")
remote_connection.send("end\n")
remote_connection.send("copy run start\n")
remote_connection.send("\n")
time.sleep(10)
output = remote_connection.recv(65535)
print (output)
ssh_client.close
Comentarios
Publicar un comentario