---
# System prepended metadata

title: src_cs_na_01_tmp

---

# src_cs_na_01_tmp

```

import requests
import datetime
import base64
import re
def main():
    try:
        with open("D:\KPI_List\FW_API_TK.txt", "r") as f:
            tokens={}
            lines = f.readlines()
            for line in lines:
                (key,value)= line.replace("\n", "").split(',')
                tokens[key] = value
        #print(tokens)
        requests.packages.urllib3.disable_warnings()
        fortigates=['inter-fw-01','sf-fw-02','extra-fw-03','inter-fw-05','nh_inter-fw-05','qp_inter-fw-05']
        for fortigate in fortigates:
            token = 'Bearer '+ tokens.get(fortigate,'')     
            headers = {
            'User-Agent':'Mozilla/5.0 (Windows NI 6.1;WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/56.0.2924.76 Safari/537.36',
            'Authorization':token
            }
            #
            nowTime = f"{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}"
            if fortigate == 'inter-fw-01':
                interface_name = ['ha','port5','x1','x3','x2','x4','port1','port2','port3','port4']
                for interface in interface_name:
                    print(interface,token)
                    url = 'https://172.18.15.1/api/v2/monitor/system/interface/select/?interface_name='+ interface
                    #print (url)
                    #response = requests.get(url, headers=headers,verify=False)
                    #fData = response.json()
                    #print (f"{nowTime},{fortigate},{interface},{fData['result'][interface]['id']},{fData[interface]['tx_bytes']},{fData[interface]['rx_bytes']}")
            elif fortigate == 'sf-fw-02':
                interface_name = ['ha','port5','x1','x2']
                include_vlan = ['vlan502','vlan1','vlan8','vlan60','vlan190','vlan201','vlan202','vlan203','vlan204','vlan206','vlan207','vlan208']
                for interface in interface_name:
                    print(interface,token)
                    url = 'https://172.18.15.9/api/v2/monitor/system/interface/select/?interface_name='+ interface
                    #print (url)
                    #response = requests.get(url, headers=headers,verify=False)
                    #fData = response.json()
                    #print (f"{nowTime},{fortigate},{interface},{fData['result'][interface]['id']},{fData[interface]['tx_bytes']},{fData[interface]['rx_bytes']}")
                url='https://172.18.15.9/api/v2/monitor/system/interface/select/?include_vlan=1'
                response = requests.get(url, headers=headers,verify=False)
                vlanData = response.json()
                for vlan in include_vlan:
                    print(vlan)
                    print (f"{nowTime},{fortigate},{vlan},{vlanData['result'][vlan]['id']},{vlanData[vlan]['tx_bytes']},{vlanData[vlan]['rx_bytes']}")
            elif fortigate == 'extra-fw-03':
                interface_name = ['ha1','ha2','port1','port3','port2','port4']
                include_vlan = ['vlan621','vlan622','vlan623','vlan624','vlan625','vlan626']
                for interface in interface_name:
                    print(interface,token)
                    url = 'https://172.18.15.17/api/v2/monitor/system/interface/select/?interface_name='+ interface
                    #print (url)
                    #response = requests.get(url, headers=headers,verify=False)
                    #fData = response.json()
                    #print (f"{nowTime},{fortigate},{interface},{fData['result'][interface]['id']},{fData[interface]['tx_bytes']},{fData[interface]['rx_bytes']}")
                url='https://172.18.15.17/api/v2/monitor/system/interface/select/?include_vlan=1'
                response = requests.get(url, headers=headers,verify=False)
                vlanData = response.json()
                for vlan in include_vlan:
                    print(vlan)
                    print (f"{nowTime},{fortigate},{vlan},{vlanData['result'][vlan]['id']},{vlanData[vlan]['tx_bytes']},{vlanData[vlan]['rx_bytes']}")
            elif fortigate == 'inter-fw-05':
                interface_name = ['dmz','wan1','wan2']
                include_vlan = ['internal3','internal4','internal1','internal2','Vlan250','Vlan249']
                for interface in interface_name:
                    print(interface,token)
                    url = 'https://10.198.250.254/api/v2/monitor/system/interface/select/?interface_name='+ interface
                    #print (url)
                    #response = requests.get(url, headers=headers,verify=False)
                    #fData = response.json()
                    #print (f"{nowTime},{fortigate},{interface},{fData['result'][interface]['id']},{fData[interface]['tx_bytes']},{fData[interface]['rx_bytes']}")
                url='https://10.198.250.254/api/v2/monitor/system/interface/select/?include_vlan=1'
                response = requests.get(url, headers=headers,verify=False)
                vlanData = response.json()
                for vlan in include_vlan:
                    print(vlan)
                    print (f"{nowTime},{fortigate},{vlan},{vlanData['result'][vlan]['id']},{vlanData[vlan]['tx_bytes']},{vlanData[vlan]['rx_bytes']}")
    except Exception as a:
        print(a)
        print(',')
        
    
if __name__ == '__main__':
    main()


```