Skip to content

Commit

Permalink
Imporved login method/Removed sys.exit() from login()
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaymane920 committed Dec 12, 2021
1 parent 9c168ef commit 0794442
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup(
name='pyFortiManagerAPI',
description='A Python wrapper for the FortiManager REST API',
version='0.1.5',
version='0.1.6',
py_modules=["pyFortiManagerAPI"],
package_dir={'': 'src'},
keywords=['Fortimanager', 'RestAPI', 'API', 'Fortigate', 'Fortinet', "python", "Fortimanager API",
Expand Down
21 changes: 13 additions & 8 deletions src/pyFortiManagerAPI.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
__author__ = "Akshay Mane"

import json
import os
import sys
from functools import wraps

import requests
import urllib3
import logging
Expand Down Expand Up @@ -37,6 +41,7 @@ def login(self):
Log in to FortiManager with the details provided during object creation of this class
:return: Session
"""

if self.sessionid is None or self.session is None:
self.session = requests.session()
# check for explicit proxy handling
Expand Down Expand Up @@ -68,12 +73,12 @@ def login(self):
}
login = self.session.post(
url=self.base_url, json=payload, verify=self.verify)
try:
self.sessionid = login.json()['session']
if login.json()["result"][0]["status"]["message"] == "No permission for the resource":
return self.session
except KeyError:
logging.error(login.json())
exit()
elif "session" in login.json():
self.sessionid = login.json()["session"]
return self.session

else:
return self.session

Expand Down Expand Up @@ -989,17 +994,17 @@ def run_script_on_single_device(self, script_name: str, device_name: str, vdom:
url=self.base_url, json=payload, verify=self.verify)
return run_script.json()["result"]

def backup_config_of_fortiGate_to_tftp(self, tftp_ip, path, filename, device_name, vdom="root"):
def backup_config_of_fortiGate_to_tftp(self, tftp_ip, path, script_name, filename, device_name, vdom="root"):
"""
A small function to backup configuration on FortiGates from FortiManager and store it in TFTP Server.
:param tftp_ip: Specify TFTP Server IP
:param path: Specify the path to store the config
:param script_name: Specify the Script name
:param filename: Specify the name of the backup file
:param device_name: Specify the name of the device
:param vdom: Specify the Vdom
"""
result = []
script_name = "backup_config_script"
full_path = normpath(join(path, filename)).replace("\\", "/")
cli_command = f"execute backup config tftp {full_path} {tftp_ip}"
logging.info("Creating a Script Template in FortiManager")
Expand All @@ -1011,4 +1016,4 @@ def backup_config_of_fortiGate_to_tftp(self, tftp_ip, path, filename, device_nam
vdom=vdom
),
"device": device_name, "vdom": vdom})
return json.dumps(result, indent=4)
return result

0 comments on commit 0794442

Please sign in to comment.