Skip to content

Commit 1345a9d

Browse files
authored
Added try except block to catch ssl errors. (#74)
If IGNORE_SSL_ERRORS is true, then silence SSL verification & continue. If IGNORE_SSL_ERRORS is false, then raise the error with custom message. Added verbose option to assist with reducing output and allowing for verbose logging if desired. If IGNORE_SSL_ERRORS is false, and --verbose is specified, then print the exception.
1 parent 3726326 commit 1345a9d

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ celerybeat.pid
102102
*.sage.py
103103

104104
# Environments
105-
.env
105+
.env*
106+
!.env.example
106107
.venv
107108
env/
108109
venv/

nb-dt-import.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import settings
1111
import sys
1212
import re
13+
import requests
1314

1415

1516
counter = Counter(
@@ -768,21 +769,7 @@ def main():
768769

769770
cwd = os.getcwd()
770771
startTime = datetime.now()
771-
772-
nbUrl = settings.NETBOX_URL
773-
nbToken = settings.NETBOX_TOKEN
774-
nb = pynetbox.api(nbUrl, token=nbToken)
775-
776-
determine_features(nb)
777-
778-
if settings.IGNORE_SSL_ERRORS:
779-
import requests
780-
requests.packages.urllib3.disable_warnings()
781-
session = requests.Session()
782-
session.verify = False
783-
nb.http_session = session
784-
785-
772+
786773
VENDORS = settings.VENDORS
787774
REPO_URL = settings.REPO_URL
788775

@@ -798,8 +785,27 @@ def main():
798785
help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l")
799786
parser.add_argument('--branch', default=REPO_BRANCH,
800787
help="Git branch to use from repo")
788+
parser.add_argument('--verbose', action='store_true',
789+
help="Print verbose output")
801790
args = parser.parse_args()
802791

792+
nbUrl = settings.NETBOX_URL
793+
nbToken = settings.NETBOX_TOKEN
794+
nb = pynetbox.api(nbUrl, token=nbToken)
795+
796+
try:
797+
determine_features(nb)
798+
except requests.exceptions.SSLError as e:
799+
if args.verbose:
800+
print(e)
801+
if not settings.IGNORE_SSL_ERRORS:
802+
print("IGNORE_SSL_ERRORS is False. SSL verification failed, exiting.")
803+
sys.exit(1)
804+
print("IGNORE_SSL_ERRORS is True, catching exception and disabling SSL verification.")
805+
requests.packages.urllib3.disable_warnings()
806+
nb.http_session.verify = False
807+
determine_features(nb)
808+
803809
try:
804810
if os.path.isdir('./repo'):
805811
print(f"Package devicetype-library is already installed, "

0 commit comments

Comments
 (0)