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

Viết chương trình Python tương tác với Meraki Dashboard API (P.2 )

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

  • Viết chương trình Python tương tác với Meraki Dashboard API (P.2 )

    2/ Lập trình python sử dụng thư viện Request để thực hiện:


    + Get the Organization ID

    + Get the networks in the organization

    + Get the devices in a network

    + Get device information

    2.1/ Hàm List_the_organizations() - Get the Organization ID

    # Hàm thực hiện lấy danh sách các organization
    def List_the_organizations(API_KEY_Reader,list_id_or):
    url = 'https://api.meraki.com/api/v1/organizations'
    header = {
    'X-Cisco-Meraki-API-Key': API_KEY_Reader
    }
    response = requests.get(url, headers=header)
    data = response.json()

    # Lưu thông tin gồm id và name của các organization vào list_id_or[]
    for i in range(0,len(data)):
    list_id_or.append({'id':data[i]['id'],
    'name':data[i]['name']})

    2.2/ Hàm List_the_networks_in_an_organization() - Get the networks in the organization

    # Hàm thực hiện lấy danh sách các network từ 1 organization được chỉ định
    def List_the_networks_in_an_organization(API_KEY_Reade r,list_id_or,list_id_netw):

    # Hiển thị thông tin của các organization
    for i in range(0,len(list_id_or)):
    print(f"{i+1:2}. Name: {list_id_or[i]['name']:30} ID: {list_id_or[i]['id']}")
    id_in = input("Moi chon organization: ")

    # Lấy ID của organization được chọn
    id = list_id_or[int(id_in)-1]['id']

    # Lấy thông tin các network từ organization trên
    url = f'https://api.meraki.com/api/v1/organizations/{id}/networks'
    header = {
    'X-Cisco-Meraki-API-Key': API_KEY_Reader
    }
    response = requests.get(url, headers=header)
    data = response.json()

    # Lưu thông tin gồm id và name của các network vào list_id_netw[]
    for i in range(0,len(data)):
    list_id_netw.append({'id':data[i]['id'],
    'name':data[i]['name']})

    2.3/ Hàm List_the_devices_in_a_network() - Get the devices in a network

    # Hàm thực hiện lấy danh sách các thiết bị từ 1 network được chỉ định
    def List_the_devices_in_a_network(API_KEY_Reader,list_ id_netw,list_serial_device):

    # Hiển thị thông tin của các network
    for i in range(0,len(list_id_netw)):
    print(f"{i+1:2}. Name: {list_id_netw[i]['name']:30} ID: {list_id_netw[i]['id']}")
    id_in = input("Moi chon network: ")

    # Lấy ID của network được chọn
    id_netw = list_id_netw[int(id_in)-1]['id']

    # Lấy thông tin các device từ network trên
    url = f'https://api.meraki.com/api/v1/networks/{id_netw}/devices'
    header = {
    'X-Cisco-Meraki-API-Key': API_KEY_Reader
    }
    response = requests.get(url, headers=header)
    data = response.json()

    # Lưu thông tin gồm name, serial, mac, model của các device vào list_serial_device[]
    for i in range(0,len(data)):
    list_serial_device.append({'name':data[i]['name'],
    'serial':data[i]['serial'],
    'mac':data[i]['mac'],
    'model':data[i]['model']})

    # Trả về ID của network được chọn
    return id_netw

  • #2
    2.4/ Hàm Get_device_information() - Get device information

    # Hàm thực hiện lấy thông tin cảu thiết bị được chỉ định
    def Get_device_information(API_KEY_Reader,id_netw,list _serial_device):

    # Hiển thị thông tin của các device
    for i in range(0,len(list_serial_device)):
    print(f"{i+1:2}. Name: {list_serial_device[i]['name']:30} Serial: {list_serial_device[i]['serial']:25} Mac: {list_serial_device[i]['mac']:25} Model: {list_serial_device[i]['model']}")
    id_in = input("Moi chon device: ")

    # Lấy serial của device được chọn
    serial_device = list_serial_device[int(id_in)-1]['serial']

    # Lấy thông tin device từ serial trên
    url = f'https://api.meraki.com/api/v1/networks/{id_netw}/devices/{serial_device}'
    header = {
    'X-Cisco-Meraki-API-Key': API_KEY_Reader
    }
    response = requests.get(url, headers=header)
    data = response.json()
    # Đinh dạng và hiển thị thông tin của device
    print(json.dumps(data,indent=4))

    Comment

    Working...
    X