Basic Paramiko

paramiko adalah module yang mengimplementasikan SSHv2 yang bekerja dibalik ansible. berikut instalasi paramiko

sudo pip install paramiko

tutorial kali ini menjalankan paramiko di perangkat cisco router. yang pertama harus dilakukan adalah mengeset cisco router untuk SSH

R1#
R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#hostname R1
R1(config)#ip domain-name btech.id  
R1(config)#crypto key generate rsa 
The name for the keys will be: R1.btech.id
Choose the size of the key modulus in the range of 360 to 2048 for your
  General Purpose Keys. Choosing a key modulus greater than 512 may take
  a few minutes.

How many bits in the modulus [512]: 1024
% Generating 512 bit RSA keys, keys will be non-exportable...[OK]

*Mar  1 00:14:27.519:  RSA key size needs to be atleast 768 bits for ssh version 2
*Mar  1 00:14:27.523: %SSH-5-ENABLED: SSH 1.5 has been enabled
R1(config)#username btech privilege 15 secret passbtech
R1(config)#line vty 0 4
R1(config-line)#login local
R1(config-line)#transport input ssh 
R1(config)#ip ssh version 2

sekarang kita buat program untuk menunjukan interface :

import sys
import time
import paramiko

host = str(sys.argv[1])
port = int(sys.argv[2])
user = str(sys.argv[3])
passwd = str(sys.argv[4])

# Create Session
session = paramiko.SSHClient()

# set policy when connecting to server with unknown host key and add hostname & key to known_hosts
session.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect to Cisco Router
# disable looking for private key in .ssh/
# disable connecting to SSH Agent
session.connect(host, username=user, password=passwd,port=port, look_for_keys=False, allow_agent=False)

# invoke a shell
shell = session.invoke_shell()

# send command to device
shell.send("show ip int br\n")

# add time to be able to load all data
time.sleep(1)

# receive data in maximal 5000 bytes
output = shell.recv(5000)

# print the output
print(output)

# close the session
shell.close()

jalankan program tersebut

ubuntu@ubuntu:~/paramiko$ python lab1.py "192.168.122.250" 22 "btech" "passbtech"

R1#show ip int br
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.122.250 YES DHCP   up                    up      
FastEthernet0/1            unassigned      YES unset  administratively down down    
R1#
ubuntu@ubuntu:~/paramiko$ 

 

Comments are closed.