Network Automation menggunakan Python – Paramiko

Network Automation Python

Selain menggunakan Telnetlib seperti yang dibahas pada artikel sebelumnya, tools lain atau library python yang dapat digunakan adalah Paramiko. Berikut topologi yang digunakan untuk praktek:

Sebelum menuliskan kode paramiko, jangan lupa install library

Contoh 1

import paramiko
import time

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname="192.168.124.139",username="cisco",password="cisco")

conn = ssh_client.invoke_shell()

conn.send("enable\n")
conn.send("cisco\n")
conn.send("conf t\n")
conn.send("int lo 1\n")
conn.send("ip add 11.11.11.1 255.255.255.0\n")
#time.sleep(1)   ---> hilangkan pagar / komentar nya

output = conn.recv(65535)
print(output.decode("ascii"))

ssh_client.close()
(Env) zakky.muhammad@mbp netauto % python3 code/paramiko-1.py

R1>enable
Password: 
R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#int lo 1
R1(config-if)#ip add 11.11.11.1 255.255.255.0
R1(config-if)#

conn.send() : digunakan untuk memasukkan perintah.
time.sleep() : memberikan jeda pada perintah untuk ditampilakn output-nya secara sempurna

Contoh 2

import paramiko
import time

host_list = ["192.168.124.138","192.168.124.139"]

n = 1

for host in host_list:
	ssh_client = paramiko.SSHClient()
	ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	ssh_client.connect(hostname=host,username="cisco",password="cisco")

	conn = ssh_client.invoke_shell()

	conn.send("enable\n")
	conn.send("cisco\n")
	conn.send("conf t\n")
	conn.send("int lo 1\n")
	conn.send("ip add 10.10.10.{} 255.255.255.0\n".format(n))
	conn.send("no sh\n")

	#time.sleep(2)   ---> hilangkan pagar / komentar nya

	output = conn.recv(65535)
	print(output.decode("ascii"))

	ssh_client.close()

	n = n + 1
(Env) zakky.muhammad@mbp netauto % python3 code/paramiko-2.py

R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#int lo 1
R1(config-if)#ip add 10.10.10.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#

R2>enable
Password: 
R2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R2(config)#int lo 1
R2(config-if)#ip add 10.10.10.2 255.255.255.0
R2(config-if)#no sh
R2(config-if)#

Pada contoh ke-2 ini perbedaannya hanya di host. Jika di contoh ke-1 hanya menggunakan 1 host, yang di contoh ke-2 ini menggunakan lebih dari 1 host yang dimasukkan ke dalam list.

Nilai n hanya digunakan sebagai increment untuk alamat IP, dimana alamat IP masing-masing host akan berawalan 10.10.10.n. Nilai n akan bernilai awal 1. Seiring pengulangan berjalan, makan nilai n akan bertambah.

Contoh 3

import paramiko
import time

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname="192.168.124.138",username="cisco",password="cisco",port="2121")

conn = ssh_client.invoke_shell()

conn.send("enable")
conn.send("cisco")
conn.send("conf t")
conn.send("int lo 1")
conn.send("ip add 10.10.10.5 255.255.255.0")
conn.send("no sh")

#time.sleep(2)   ---> hilangkan pagar / komentar nya

output = conn.recv(65535)
print(output)

ssh_client.close()
(Env) zakky.muhammad@mbp netauto % python3 code/paramiko-3.py

R1>enable
Password: 
R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#int lo 1
R1(config-if)#ip add 10.10.10.5 255.255.255.0
R1(config-if)#no sh
R1(config-if)#

LANJUTKAN BACA MATERI LENGKAP


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.