• 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.

ISE and Python (Phần 2)

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

  • ISE and Python (Phần 2)

    Sử dụng RADIUS Simulator

    Trình mô phỏng RADIUS có sẵn dưới dạng tập lệnh Python (python pez.py -c ise_dot1x_{username}.cfg) trên máy chủ CentOS Linux để thực hiện các yêu cầu Xác thực RADIUS thực đối với ISE. Điều này cho phép bạn kiểm tra cấu hình chính sách của mình với cả người dùng nội bộ ISE và người dùng Active Directory.

    1. SSH đến RADIUS Simulator VM bằng mật khẩu C1sco12345, chấp nhận mọi key fingerprints:
    ssh developer@10.10.20.32
    developer@10.10.20.32's password: C1sco12345

    2. Lệnh đơn giản để xác thực một user là:
    ��� Nhấn phím ↑ để quay trở lại câu lệnh trước để tiết kiệm công sức, khỏi cần phải gõ lại.
    python pez.py -c ise_dot1x_{username}.cfg

    3. Output của script sẽ cho biết việc xác thực đã nhận được phản hồi Access-Accept hay Access-Reject: [developer@hcllinux pez]$ python pez.py -c ise_dot1x_tme.cfg
    ('acc_enable:', True)
    host ipv4 address detected
    10.10.20.77
    Total time: 1.001
    Average TPS: 1
    Current TPS: 1
    Access-Accept: 1
    Access-Reject: 0
    Errors: 0
    Timeouts: 0
    End: False

    Total time: 1.001
    Average TPS: 1
    Current TPS: 1
    Access-Accept: 1
    Access-Reject: 0
    Errors: 0
    Timeouts: 0
    End: True

    [developer@hcllinux pez]$
    Nếu bạn không nhận được output như trên, bạn phải xem lại cấu hình ISE cho policy, internal users, network devices, và Active Directory.

    4.Những file ise_*.cfg có sẵn trong RADIUS simulator cho users trong Active Directory: ise_dot1x_analyst.cfg

    ise_dot1x_cio.cfg
    ise_dot1x_contractor.cfg
    ise_dot1x_cxo.cfg
    ise_dot1x_devops.cfg
    ise_dot1x_engineer.cfg
    ise_dot1x_helpdesk.cfg
    ise_dot1x_netadmin.cfg
    ise_dot1x_netops.cfg
    ise_dot1x_pm.cfg
    ise_dot1x_secadmin.cfg
    ise_dot1x_se.cfg
    ise_dot1x_secops.cfg
    ise_dot1x_tme.cfg
    ise_dot1x_vendor.cfg
    ise_dot1x_webops.cfg

    5.Hai file configs này dành cho users không nằm trong Active Directory nên bạn có thể thử tài khoản internal user trong ISE: ise_dot1x_probe.cfg # probe:C1sco12345
    ise_vpn.cfg # employee:C1sco12345

    6.Bạn có thể xem thử xem có xác thực và ủy quyền nào từ simulator trong ISE LiveLogs ở GUI theo Operations RADIUS Live Logs.

    7. Bạn cũng có thể sử dụng Cisco ISE API - MNT để xem cùng một phiên làm việc (sessions). Trong Postman Collections pane, chọn Cisco ISE API - MNT GET ActiveList (all).��� Tất cả MNT API output đều ở dưới dạng XML.
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <activeList noOfActiveSession="1">
    <activeSession>
    <user_name>tme</user_name>
    <calling_station_id>95:70:2B:E4:00:00</calling_station_id>
    <nas_ip_address>10.10.20.32</nas_ip_address>
    <acct_session_id>95:70:2b:e4:00:00</acct_session_id>
    <server>ise-1</server>
    <framed_ip_address>21.0.0.2</framed_ip_address>
    <framed_ipv6_address/>
    </activeSession>
    </activeList>
    Sử dụng ciscoisesdk Python Package

    Những gì bạn đã thấy với tập lệnh Postman Python là các URL thô được sử dụng để gửi các đoạn JSON tĩnh để cấu hình ISE. Nó không linh hoạt và không cung cấp các đối tượng và thuộc tính truyền thống có thể được vận hành theo cách lập trình. Sẽ thật tuyệt nếu bạn có thể đọc, tạo, cập nhật và xóa tài nguyên ISE của mình hoàn toàn bằng Python phải không?
    Đó chính xác là những gì ciscoisesdk làm. Chúng tôi đã xây dựng gói Python ciscoisesdk để cung cấp các mô-đun Python để tương tác theo chương trình với các tài nguyên ISE. Cuối cùng không chỉ để bạn nghịch mà nó còn là nền tảng của bộ sưu tập cisco.ise Ansible. Nếu bạn muốn biết thêm về ciscoisesdk:
    Bạn đã cài đặt gói requests bằng pipenv trong DevBox, quá trình này giống hệt đối với ciscoisesdk.
    ��� Đừng quên bật Python virtual environment bằng câu lệnh pipenv shell nếu bạn không thấy chữ (developer)ở prompt terminal.
    pipenv install ciscoisesdk

    Tạo một Network Device

    Hãy tạo một network device khác nhưng lần này sử dụng ciscoisesdk.
    1. Trong DevBox, tạo một file mới, đặt là isesdk_post_networkdevice.py.
    2. Copy script dưới vào file mới tạo trong DevBox.
    #!/usr/bin/env python3
    import json
    from ciscoisesdk import api, ApiError
    api_ = api.IdentityServicesEngineAPI(
    username='admin',
    password='C1sco12345',
    base_url='https://ise-1.lab.devnetsandbox.local',
    verify=False)
    try:
    response = api_.network_device.create_network_device(
    name="test_device",
    description="",
    profile_name="Cisco",
    authentication_settings={
    "networkProtocol": "RADIUS",
    "radiusSharedSecret": "C1sco12345"
    },
    tacacs_settings={
    "sharedSecret": "C1sco12345",
    "connectModeOptions": "OFF"
    },
    network_device_iplist=[
    {
    "ipaddress": "10.10.20.33",
    "mask": 32
    }
    ],
    network_device_group_list=[
    "Location#All Locations",
    "IPSEC#Is IPSEC Device#No",
    "Device Type#All Device Types"
    ]
    )
    if (response.headers["Location"]) :
    print(response.headers["Location"])
    else :
    print(json.dumps(response.response, indent=2))
    except ApiError as e:
    print(e)

    3.Đổi quyền cho file để có thể chạy được:
    chmod 755 isesdk_post_networkdevice.py

    4. Chạy script và bạn có thể thấy id của network device ở sau cùng của URL. ./isesdk_post_networkdevice.py
    ��� Nếu bạn thấy cảnh báo như là InsecureRequestWarning: Unverified HTTPS request is being made to host, bỏ qua nó bằng các thêm vào biến môi trường (environment) giá trị sau:
    export PYTHONWARNINGS="ignore:Unverified HTTPS request"
    Lấy thông tin Network Device

    Chúng ta đã tạo một network device với script trước đó. Với script đầu tiên của bạn với ciscoisesdk, hãy thử lấy thông tin chi tiết hơn.
    1. Trong DevBox, tạo file với tên là isesdk_get_networkdevice.py.
    2. Copy script sau và dán nó vào file vừa tạo trong DevBox.
    #!/usr/bin/env python3
    import json
    from ciscoisesdk import api
    api_ = api.IdentityServicesEngineAPI(username='admin',
    password='C1sco12345',
    base_url='https://ise-1.lab.devnetsandbox.local',
    verify=False)
    try:
    response = api_.network_device.get_network_device_by_name('te st_device').response
    print(json.dumps(response, indent=2))
    except ApiError as e:
    print(e)
    Lưu script.
    Cho script quyền được thực thi: chmod 755 isesdk_get_networkdevice.py.
    Chạy script với câu lệnh ./isesdk_get_networkdevice.py và bạn sẽ thấy tất cả các thuộc tính như ở trong Postman.
    {
    "NetworkDevice": {
    "id": "3d3c90b0-d253-11eb-a22c-723e1f0dfb0b",
    "name": "test_device",
    "description": "",
    "authenticationSettings": {
    "networkProtocol": "RADIUS",
    "radiusSharedSecret": "C1sco12345",
    "enableKeyWrap": false,
    "dtlsRequired": false,
    "keyInputFormat": "ASCII",
    "enableMultiSecret": "false"
    },
    "tacacsSettings": {
    "sharedSecret": "C1sco12345",
    "connectModeOptions": "OFF"
    },
    "profileName": "Cisco",
    "coaPort": 0,
    "link": {
    "rel": "self",
    "href": "https://ise-1.lab.devnetsandbox.local/ers/config/networkdevice/name/test_device",
    "type": "application/json"
    },
    "NetworkDeviceIPList": [
    {
    "ipaddress": "10.10.20.33",
    "mask": 32
    }
    ],
    "NetworkDeviceGroupList": [
    "Location#All Locations",
    "IPSEC#Is IPSEC Device#No",
    "Device Type#All Device Types"
    ]
    }
    }
    Đó là phần cuối của chuyến tham quan nhanh ciscoisesdk để chỉ cho bạn những điều cơ bản về cách sử dụng nó. Tham khảo tài liệu nếu bạn có bất kỳ câu hỏi nào về cách sử dụng bất kỳ đối tượng cụ thể nào trong các tập lệnh của riêng bạn.
    Last edited by nguyen6110@; 15-03-2022, 09:26 AM.
Working...
X