Network Automation | AnsibleParamiko
Jakukn scipt ini di Ansible Linux, ketik
pico paramiko1.py
python paramiko1.py
----------------------------------------
1. Kita akan melakukan konfig IP interface loopback device (single device)
import paramiko
import time
ip_address = "10.0.1.1"
username = "admin"
password = "cisco"
ssh = paramiko.SSHClient()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print "Success Login to {0}".format(ip_address)
conn = ssh_client.invoke_shell()
#conn = ssh_client.invoke_shell()
conn.send("conf t\n")
conn.send("int lo9\n")
conn.send("ip add 10.0.0.10 255.255.255.255\n")
time.sleep(1)
output = conn.recv(65535)
print output
ssh_client.close()
2. Kita akan melakukan save config pada satu device (single device)
import paramiko
import time
ip_address = '10.0.1.1'
username = 'admin'
password = 'cisco'
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username, password=password)
print ('Success login to {0}').format(ip_address)
conn = ssh_client.invoke_shell()
conn.send('terminal length 0\n')
conn.send('show run\n')
conn.send('write\n')
time.sleep(1)
output = conn.recv(65535)
import time
import getpass
ip_list = ['10.0.1.1','10.0.1.2']
username = raw_input('Username: ')
password = getpass.getpass()
for ip_address in ip_list:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print 'Success login to {}'.format(ip_address)
conn = ssh_client.invoke_shell()
conn.send('terminal length 0\n')
conn.send('show run\n')
conn.send('write\n')
time.sleep(1)
output = conn.recv(65535)
output_file=open('{0}.cfg'.format(ip_address),'wr')
output_file.write(output)
output_file.close()
print 'Config in {0} saved!!'.format(ip_address)
ssh_client.close()
import time
import getpass
ip_address = raw_input('IP Adress')
username = raw_input('Username: ')
password = getpass.getpass()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print 'Success Login to {0}'.format(ip_address)
conn = ssh_client.invoke_shell()
conn.send('conf t\n')
conn.send('int lo16\n')
conn.send('ip add 10.0.0.16 255.255.255.255\n')
time.sleep(1)
5. Paramiko-Multi Device-Backup Konfig to Server External
import paramiko
import time
import getpass
ip_list = ['10.0.1.1','10.0.1.2']
username = raw_input('Username: ')
password = getpass.getpass()
for ip_address in ip_list:
ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print 'Success login to {}'.format(ip_address)
conn = ssh_client.invoke_shell()
conn.send('terminal length 0\n')
conn.send('show run\n')
conn.send('copy running-config tftp:\n')
conn.send('10.0.1.100\n\n')
time.sleep(1)
output = conn.recv(65535)
output_file=open('{0}.cfg'.format(ip_address),'wr')
output_file.write(output)
output_file.close()
print 'Backup Config {0} Success!!!'.format(ip_address)
ssh_client.close()
Link Buat Refrensi: https://zakkymuhammad.com/blog/network-automation/
Posting Komentar untuk "Network Automation | AnsibleParamiko"