I am trying to execute the following code that searches for a mac address in a text file and if found it asks if the user wants to retrieve device information by calling API based on the found IP and password. also, it gives the user the option to start a new search or to quit.
This part works perfectly if a search is matched. the problem here when the search does not match! it executes " No match found! " correctly in the first trial and then it goes over the function again but if the user is re-entered a mac address that does not have a match again, the code does not re-execute again and ends the program.
below is the code
import http.client
from base64 import b64encode
import ssl
import json
def search_mac():
with open(r"C:\Users\afahmy\Desktop\final.txt", "r") as output_file:
search = input("Enter mac-address: ")
search = search.lower().replace(':', '')
for line in output_file:
ip = line.split(',')[0]
# print(ip)
password = line.split(',')[1]
# print(password)
mac = line.split(',')[2].rstrip()
# print(mac)
if search == mac:
print(format("IP:" + ip + "\n" + "password:" + password))
if input("Do you want to retrieve device information?[y/n]") == 'y':
ssl._create_default_https_context = ssl._create_unverified_context
auth_string = "Polycom:"
admin_password = auth_string + password
conn = http.client.HTTPSConnection(ip)
userandpass = b64encode(admin_password.encode('UTF-8')).decode('ascii')
headers = {'Authorization': 'Basic %s' % userandpass}
conn.request("GET", "/api/v1/mgmt/device/info", headers=headers)
res = conn.getresponse()
data = res.read()
json_format = json.loads(data)
dictionary = json_format["data"]
dictionary_new = {k: dictionary[k] for k in dictionary.keys() - {'IPV6Address', 'DeviceType', 'DeviceVendor', 'AttachedHardware'}}
for item in dictionary_new.items():
print(item)
if input("Do you want to make another search?[y/n]:") == 'y':
search_mac()
else:
print("Thank you!")
exit()
search_mac()
print("No Match!")
if input("Do you really want to make another search?[y/n]:") == 'y':
search_mac()
else:
print("Thank you for your time!")
Here is the output when I enter the incorrect mac address the first time (works well) and the second time (here is the problem).
Enter mac-address: 64167f185bf30000 <<<< i added 4 zeros to be a wrong one
No Match!
Do you really want to make another search?[y/n]:y
Enter mac-address: 64167f185bf3 <<<< entered a correct one
IP:10.10.10.4
password:Password!
Do you want to retrieve device information?[y/n]n
Do you want to make another search?[y/n]:y
Enter mac-address: 64167f185bf300 <<<< again a mac address that does not have a match
Process finished with exit code 0
Aucun commentaire:
Enregistrer un commentaire