Automation Network : 2. Membuat Script Python pada Cisco Melalui SSH

1 min read

setelah sukses setting konfigurasi network,kita akan membuat automation menambah konfigurasi jaringan menggunakan python pada router cisco

sebelumnya , saya aktifkan fitur ssh pada router cisco

R1#conf t
R1(config)#username cisco password cisco
R1(config)#username cisco privilege 15  
R1(config)#ip domain-name domaincisco.local
R1(config)#crypto key generate rsa
R1(config)#line vty 0 15
R1(config-line)#login local
R1(config-line)#exit
R1(config)#exit
R1#copy run start

setelah configurasi service ssh cisco diaktifkan, buatlah script sebagai berikut

import paramiko
import time

ip_adddress = '192.168.5.3'
username = 'cisco'
password = 'cisco'

paramiko.Transport._preferred_ciphers = ('aes256-cbc', )
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_adddress, username=username, password=password, allow_agent=False, look_for_keys=False)

print("Succes login to {}".format(ip_adddress))
conn = ssh_client.invoke_shell()

conn.send("conf t\n")
conn.send("int lo0\n")
conn.send("ip add 10.1.1.1 255.255.255.255\n")
time.sleep(1)

output = conn.recv(65535)
print(output.decode())
ssh_client.close()

jalankan script diatas, maka hasilnya

R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#do show ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.5.3     YES manual up                    up      
FastEthernet0/1            unassigned      YES NVRAM  administratively down down    
Loopback0                  10.1.1.1        YES manual up                    up      
R1(config)#

lalu saya akan menambahkan ip address 10.1.1.2 ke interface lo1

import paramiko
import time

ip_adddress = '192.168.5.3'
username = 'cisco'
password = 'cisco'

paramiko.Transport._preferred_ciphers = ('aes256-cbc', )
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_adddress, username=username, password=password, allow_agent=False, look_for_keys=False)

print("Succes login to {}".format(ip_adddress))
conn = ssh_client.invoke_shell()

conn.send("conf t\n")
conn.send("int lo1\n")
conn.send("ip add 10.1.1.2 255.255.255.255\n")
time.sleep(1)

output = conn.recv(65535)
print(output.decode())
ssh_client.close()

coba kita lihat configurasi ip terbaru

R1(config)#do show ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.5.3     YES manual up                    up      
FastEthernet0/1            unassigned      YES NVRAM  administratively down down    
Loopback0                  10.1.1.1        YES manual up                    up      
Loopback1                  10.1.1.2        YES manual up                    up  

How to Run Django on Jupyter Notebook Visual Code

previously I have struggled to debug my app when running Python app directly; I need it to run on my Jupyter Workspace easily to...
admin
27 sec read

How to secure important data with environments in Python

python file .env
admin
19 sec read

Custome Setting Database Django

berikut ini macam cara settting database django MAMP Server 2. MYSQL Server (XAMPP) 3. Default
admin
13 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *