Skip to content

Commit a2f81e2

Browse files
committed
[pfsensible-generate-module] Some more error handling around connecting/logging in
1 parent 44651a8 commit a2f81e2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

GENERATING_MODULES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
The process of writing basic pfsensible modules is hopefully greatly simplified by using
44
the pfsensible-generate-module script. The basic workflow is as follows:
55

6+
* You need a test pfSense instance with ssh access enabled.
67
* Navigate in the pfSense web interface to the area you want to write a module for. This should be a page where you can edit
78
settings or one where you are adding an item.
89
* Copy the URL of the page - you will pass it to the `--url` option of the script.

misc/pfsensible-generate-module

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,30 @@ if args.url is not None:
105105
client = requests.Session()
106106

107107
# Retrieve the CSRF token first
108-
r = client.get(login_url, verify=False)
108+
try:
109+
r = client.get(login_url, verify=False)
110+
except requests.exceptions.ConnectionError as e:
111+
print(f'Failed to connect to {login_url}: {e}', file=sys.stderr)
112+
sys.exit(1)
113+
109114
csrf = re.search(".*name='__csrf_magic' value=\"([^\"]+)\".*", r.text, flags=re.MULTILINE).group(1)
110115

111116
# Login to the web interface
112117
login_data = dict(login='Login', usernamefld=args.user, passwordfld=args.password, __csrf_magic=csrf)
113118
r = client.post(login_url, data=login_data, verify=False)
114-
csrf = re.search(".*name='__csrf_magic' value=\"([^\"]+)\".*", r.text, flags=re.MULTILINE).group(1)
119+
if (args.verbose >= 4):
120+
print(f'Login URL returned {r} {r.text}')
121+
html = lxml.html.fromstring(r.text)
122+
# <div class="col-sm-4 nowarning msgbox text-center text-danger"><h4>Username or Password incorrect</h4></div>
123+
alert = html.xpath('//div[contains(@class,"text-danger")]/*[1]/text()')
124+
if len(alert) > 0:
125+
print(f'Login failed with "{alert[0]}"', file=sys.stderr)
126+
sys.exit(1)
115127

116128
# Retrieve the configuration web page and parse it
117129
r = client.get(args.url, verify=False)
130+
if (args.verbose >= 4):
131+
print(f'{args.url} returned {r} {r.text}')
118132
html = lxml.html.fromstring(r.text)
119133

120134
elif args.urlfile is not None:

0 commit comments

Comments
 (0)