Published 10月 09, 2018 by with 0 comment

06 - Using SSH in Python - v1



我試著用Python在Cisco設備上做些事.
這個練習是用Python去SSH登入Cisco設備.
下面程式能做到:
1. SSH(可選port).
2. 登入單台設備
3. 自動輸入SSH的帳密.
4. 在python中手動要輸入進Cisco設備的指令

範例檔我是參考:
https://github.com/ktbyers/netmiko
https://pynet.twb-tech.com/blog/automation/netmiko.html

用Notepad++打下列的程式碼,
另存為PY_SSH_SW_VLANs.py. 我附上中文注釋方便好讀.
# -*- coding: UTF-8 -*-
# http://juilin77.blogspot.com/
# v20181009
# 下面程式能做到:
# 1. SSH(可選port).
# 2. 登入單台設備
# 3. 自動輸入SSH的帳密.
# 4. 在python中手動要輸入進Cisco設備的指令

# 首先導入下面這個模組
from netmiko import ConnectHandler

# 命令cisco設備, 並做登入的參數設定
mgmt_sw01 = {
    "device_type": "cisco_ios",
    "ip":   "192.168.80.191",
    "username": "cisco",
    "password": "cisco123",
    "port" : 22,          # optional, defaults to 22
    "secret": "secret",     # optional, defaults to ""
    "verbose": False,       # optional, defaults to False
}

net_connect = ConnectHandler(**mgmt_sw01)

# 這邊可以寫上我將要在登入設備後去執行的"Show"指令(show的做法)
# 並輸出其指令效果
output = net_connect.send_command("show cdp neighbors")
print(output)

# 這邊可以寫上我將要在登入設備後去執行"config"指令(config的做法)
# 並輸出其指令效果
config_commands = [ "logging buffered 20000",
                    "logging buffered 20010",
                    "no logging console" ]
output = net_connect.send_config_set(config_commands)
print (output)

# 我用loop的方式, 建立多個VLAN, 從VLAN2開始, 建立到VLAN4.
# VALN name 就是 PY_VLAN_開頭, 後面接上VLAN ID.
# 並輸出其指令效果
for n in range(2,5):
    print ("Creating VLAN " + str(n))
    config_commands = ["vlan " + str(n),
                       "name PY_VLAN_" + str(n)]
    output = net_connect.send_config_set(config_commands)
    print (output)

然後我用Web-IOU去建一個測試環境.

以下是預先需要設定的指令:
hostname MGMT_SW01
!
username cisco privilege 15 password cisco123
!
interface vlan 1
 ip address 192.168.80.201 255.255.255.0
 no shutdown
!
ip domain-name peter.net
crypto key generate rsa
1024
!
line vty 0 4
 login local 
 transport input all
!

然後用Windows的cmd, 執行python.
D:\Tech\Python\PY_Cisco>python PY_SSH_SW_VLANs.py

 Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
                  S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
                  D - Remote, C - CVTA, M - Two-port Mac Relay

Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID
R01              Eth 0/1           177               R    Linux Uni Eth 0/0
config term
Enter configuration commands, one per line.  End with CNTL/Z.
MGMT_SW01(config)#logging buffered 20000
MGMT_SW01(config)#logging buffered 20010
MGMT_SW01(config)#no logging console
MGMT_SW01(config)#end
MGMT_SW01#
Creating VLAN 2
config term
Enter configuration commands, one per line.  End with CNTL/Z.
MGMT_SW01(config)#vlan 2
MGMT_SW01(config-vlan)#name PY_VLAN_2
MGMT_SW01(config-vlan)#end
MGMT_SW01#
Creating VLAN 3
config term
Enter configuration commands, one per line.  End with CNTL/Z.
MGMT_SW01(config)#vlan 3
MGMT_SW01(config-vlan)#name PY_VLAN_3
MGMT_SW01(config-vlan)#end
MGMT_SW01#
Creating VLAN 4
config term
Enter configuration commands, one per line.  End with CNTL/Z.
MGMT_SW01(config)#vlan 4
MGMT_SW01(config-vlan)#name PY_VLAN_4
MGMT_SW01(config-vlan)#end
MGMT_SW01#

D:\Tech\Python\PY_Cisco>

完成

Check:


參考資料:
1. 05 - Install Paramiko and Netmiko on Windows
http://juilin77.blogspot.com/2018/10/05-install-paramiko-and-netmiko-on.html

2. 12 - Using SSH in Python - v4
https://juilin77.blogspot.com/2019/05/12-using-ssh-in-python-v4.html

3. 08 - Using SSH in Python - v3
https://juilin77.blogspot.com/2018/10/08-using-ssh-in-python-v3.html

4. 07 - Using SSH in Python - v2
https://juilin77.blogspot.com/2018/10/07-using-ssh-in-python-v2.html

最初發表 / 最後更新: 2018.10.09 / 2019.05.16

0 comments:

張貼留言