I'm bit new with Python and trying to use Python script to run across all network devices from csv list. device which is successfully authenticate gives me output without issue but few of devices are throwing some invalid cred error ,how we can handle this type of error...?
file_csv = open('device_csv_test.txt','r')
lines = file_csv.readlines()
file_csv.close()
lines_ = [line.strip() for line in lines[1:]]
# create list of IPs that we need to pull uptime
device_ip_list =[]
for line in lines_:
device_data = line.split("\t")
device_name = device_data[0]
device_ip = device_data[1]
device_ip_list.append(device_ip)
# function that gets system_info details
def system_info(ip):
urllib3.disable_warnings()
try:
fw = firewall.Firewall(ip, username, password)
except ValueError:
print(f"{device_name} is no longer accessible OR might have invalid cred")
else:
system_info = fw.op ("show system info", xml=True ) # without xml=True, it is going to return object only , but with xml=True arg, it is return a string with xml data
system_info_json = xmltodict.parse(system_info)
uptime = system_info_json["response"]["result"]["system"]["uptime"]
device_name = system_info_json["response"]["result"]["system"]["hostname"]
dict_ = [{'device_name':device_name ,'uptime': uptime}]
writer.writerows(dict_)
# writer.writerows([elm.values() for elm in dict_]) # parse value of dict
# write parsed data (hostname , uptime) into CSV file
start_time = timeit.default_timer()
with open ("new_csv.csv","w") as f:
writer = csv.DictWriter(f, fieldnames= ['device_name','uptime']) # create header
writer.writeheader() # write header in csv
# writer = csv.writer(f) t
# f.write('hostname,uptime\n')
for ips in device_ip_list:
system_info(ips)
end_time = timeit.default_timer()
print(f"total time = {end_time - start_time}")
it is generating error below :
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 3878, in method
super_method(self, *args, **kwargs)
File "/home/admin/palo_nat/lib/python3.11/site-packages/pan/xapi.py", line 637, in keygen
raise PanXapiError(self.status_detail)
pan.xapi.PanXapiError: URLError: code: 403 reason: Invalid Credential
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/admin/palo_uptime_test.py", line 70, in <module>
system_info(ips)
File "/home/admin/palo_uptime_test.py", line 53, in system_info
system_info = fw.op ("show system info", xml=True ) # without xml=True, it is going to return object only , but with xml=True arg, it is return a string with xml data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/firewall.py", line 242, in op
return super(Firewall, self).op(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 4047, in op
element = self.xapi.op(cmd, vsys, False, extra_qs, retry_on_peer=retry_on_peer)
^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 4000, in xapi
self._xapi_private = self.generate_xapi()
^^^^^^^^^^^^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/firewall.py", line 255, in generate_xapi
return super(Firewall, self).generate_xapi()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 4072, in generate_xapi
"api_key": self.api_key,
^^^^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 3994, in api_key
self._api_key = self._retrieve_api_key()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 4159, in _retrieve_api_key
xapi.keygen(retry_on_peer=False)
File "/home/admin/palo_nat/lib/python3.11/site-packages/panos/base.py", line 3897, in method
raise the_exception
panos.errors.PanURLError: URLError: code: 403 reason: Invalid Credential
thanks for help...!!
looking for correct exception to handle error as posted.
From your error output, you are getting a
403error:That means that some of your entries for IP+username+password combinations (lines in your csv) are incorrect.
As you mentioned, your script does work for some devices you are logging in to, but not others. Given that information, it seems clear that the script is likely not the issue, but instead your IP+username+password combination is incorrect for the ones you are receiving this 403 error.
Debugging tips:
tab(\t) delimiter, make sure you don't have other whitespace characters in your values that are being parsed from the csv. Note that callingline.strip()as you do at the beginning only removes the leading and trailing spaces, i.e., spaces at the beginning and end of the line. You can easily check with a text editor that can reveal whitespace and show symbols in their place. e.g., if your csv row looks likex.x.x.x\tusername\s\tpassword\n, then the space (\s) character afterusernameand before the tab (\t) that separates it frompasswordwould make your parsed username:username\s