• If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
Xin chào ! Nếu đây là lần đầu tiên bạn đến với diễn đàn, xin vui lòng danh ra một phút bấm vào đây để đăng kí và tham gia thảo luận cùng VnPro.

Announcement

Collapse
No announcement yet.

Dùng Python thực hiện SSH & cấu hình trên thiết bị mạng bằng Netmiko

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dùng Python thực hiện SSH & cấu hình trên thiết bị mạng bằng Netmiko



    Mô hình mạng :



    Gồm 1 máy ảo kali linux , 1 router cisco giả lập trên gns3.

    1. cấu hình trên Router R1

    1.1 cấu hình SSH version 2

    #config terminal
    (config)#enable password password
    (config)#line vty 0 4
    (config-line)#password 123
    (config-line)#login local
    (config-line)#exit

    (config)#username vnpro password 123
    (config)#ip domain-name vnpro
    (config)#crypto key generate rsa
    The name for the keys will be: R1.vnpro .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
    (config)#ip ssh version 2

    R1(config)#interface fastEthernet 0/0

    R1(config-if)#ip address dhcp

    R1(config-if)#no shutdown

    R1(config-if)#exit
    • Thực hiện kiểm tra địa chỉ IP của interface fastEthenet 0/0

    FastEthernet0/0 is up, line protocol is up

    Internet address is 192.168.186.131/24

    Broadcast address is 255.255.255.255

    Address determined by DHCP

    2. trên kali

    Yêu cầu cần : đã cài ssh và python3

    2.1 thực hiện kiểm tra kết nối SSH vào R1

    ssh vnpro@192.168.186.131

    + Nếu có lỗi

    "

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    " , thực hiện lệnh sau:

    ssh-keygen -f "/root/.ssh/known_hosts" -R "192.168.186.131"

    + Nếu có lỗi "no matching key exchange method found. Their offer: diffie-hellman-group1-sha1" :

    ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 vnpro@192.168.186.131

    + Nếu có lỗi "no matching cipher found. Their offer: aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc":

    Truy cập vào file :

    vi /etc/ssh/ssh_config

    Thêm vào cuối file :

    Ciphers aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc

    => Thực hiện kiểm tra lại kết nối ssh

    ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 vnpro@192.168.186.131

    The authenticity of host '192.168.186.131 (192.168.186.131)' can't be established.

    RSA key fingerprint is SHA256:HSxD9Pak+WFl1W2NxJpJdhfTkhyu2Thi2MiiPk4SWBs .

    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

    Warning: Permanently added '192.168.186.131' (RSA) to the list of known hosts.

    vnpro@192.168.186.131's password: [Nhập 123] (username vnpro password 123)

    R1>enable

    Password: [Nhập password] (enable password password)
    • Kết nối SSH vào Router R1 thành công

    2.2 cài đặt thư viện netmiko

    Yêu cầu : máy ảo kali truy cập được Internet .

    Thực hiện lệnh : sudo apt-get install python3-netmiko

    (Nhập Y để tiếp tục quá trình cài đặt)

    2.3 lập trình python sử dụng thư viện netmiko để thực hiện ssh và cấu hình địa chỉ IP cho interface

    2.3.1 tạo file python

    vi addIPInterface.py

    2.3.2 thiết lập SSH

    from netmiko import ConnectHandler

    R1 = { 'device_type':'cisco_ios',

    'host': '192.168.186.131',

    'username':'vnpro',

    'password':'123',

    'secret':'password', }

    net_connect = ConnectHandler(**R1)

    Gọi hàm ConnectHandler trong thư viện netmiko bằng dòng lệnh :
    From netmiko import ConnectHandler


    ,trong đó hàm ConnectHandler bao gồm các giá trị:
    + device_type: các thiết bị của cisco thuờng là ‘cisco_ios’
    + host: IP của thiết bị
    + username: tên đăng nhập
    + password: mật khẩu đăng nhập
    + secret : mật khẩu enable
    gán giá trị khi thực hiện hàm ConnectHandler cho biến net_connect .
    Tới đây chúng ta đã thiết lập thành công SSH bằng netmiko .

    2.2.3 sử dụng một số hàm trong thư viện netmiko

    - Để gửi 1 câu lệnh đến Router/Switch qua SSH ở Privileged EXEC mode , ta dùng hàm send_commnad(), VD:

    output = net_connect.send_command('sh ip int bri')

    print(output)

    - Để vào Privileged EXEC mode , ta dùng hàm enable() , VD :

    net_connect.enable()

    - Để gửi 1 câu lệnh đến Router/Switch ở config mode ta dùng hàm send_config_set(), VD:

    net_connect.enable()

    addIPInterface = ['int f1/0','ip add 192.168.186.133 255.255.255.0','no shut']

    net_connect.send_config_set(addIPInterface)

    Một số lệnh khác thường dùng :
    net_connect.save_config() - lưu cấu hình
    net_connect.find_prompt() - trả về tên host R/Sw
    net_connect.disconnect() - Đóng kết nối

    2.3.4 File cấu hình về kết quả thực thi

    File addIPInterface.py như sau :



    Thực hiện chạy file addIPInterface.py :

    #python3 addIPInterface.py

    Kết quả :

    • Ta đã thực hiện thành công cấu hình IP cho 1 interface trên router R1 thông qua sử dụng thư viện netmiko trong python.

Working...
X