Esempio n. 1
0
def main():
    # Parse for command line argument for fgt ip
    if len(sys.argv) < 2:
        # Requires fgt ip and password
        print("Please specify fgt ip address")
        exit()

    # Initilize fgt connection
    ip = sys.argv[1]
    try:
        passwd = sys.argv[2]
    except:
        passwd = ''
    # fgt = FGT(ip)

    # Hard coded vdom value for all requests
    vdom = "root"

    # Login to the FGT ip

    fgt = FortiOSAPI()

    fgt.login(ip, 'admin', passwd, verify=False)
    yamldata = '''
        antivirus:
            profile:
                apiset:
                    'name': "apiset"
                    "scan-mode": "quick"
                    'http': {"options": "scan avmonitor",}
                    "emulator": "enable"
        firewall:
            policy:
                66:
                  'name': "Testfortiosapi"
                  'action': "accept"
                  'srcintf': [{"name": "internal"}]
                  'dstintf': [{"name": "virtual-wan-link"}]
                  'srcaddr': [{"name": "all"}]
                  'dstaddr': [{"name": "all"}]
                  'schedule': "always"
                  'service': [{"name": "HTTPS"}]
                  "utm-status": "enable"
                  "profile-type": "single"
                  'av-profile': "apiset"
                  'profile-protocol-options': "default"
                  'ssl-ssh-profile': "certificate-inspection"
                  'logtraffic': "all"
                '''

    yamldict = yaml.load(yamldata, Loader=yaml.FullLoader)
    fgt.setoverlayconfig(yamltree=yamldict)
    fgt.logout()
def _send_request(call):
    logger.info(
        '_send_request request_props:{}'.format(call))
    host = call.get('host')
    username = call.get('username')
    password = call.get('password')
    use_ssl = call.get('use_ssl', False)
    verify_ssl = call.get('verify_ssl', False)
    vdom = call.get('vdom', 'root')

    path = call.get('path')
    name = call.get('name')
    data = call.get('data', {})
    method = call.get('method')
    # TODO# add api key method option and client certificate verifications
    # TODO# upload license call / check license when connection is on

    fgt_instance = FortiOSAPI()
    if use_ssl == True:
        fgt_instance.https('on')
    else:
        fgt_instance.https(status='off')

    fgt_instance.login(host,
                       username,
                       password,
                       verify=verify_ssl)

    if method == "LICENSE":
        code, response = fgt_instance.license()
        logger.debug('---> Method: {} \n code: {} \n response: \n {}'.format(method, code, response))

    if method == "GET":
        code, response = fgt_instance.get(path, name, vdom=None, mkey=None, parameters=None)
        logger.debug('---> Method: {} \n code: {} \n response: \n {}'.format(method, code, response))

        # if method == "UPDATE":
        # if method == "SET":
        # if method == "REPLACE":
        # if method == "CLONE":
    if method == "SETOVERLAY":
        code, response = fgt_instance.setoverlayconfig(data, vdom=vdom)
        logger.debug('---> Method: {} \n code: {} \n response: \n {}'.format(method, code, response))

    fgt_instance.logout()
    return code, response