Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # importing the libraries
  4. import sys, getopt
  5. import requests
  6. import re
  7. from urllib3.exceptions import InsecureRequestWarning
  8. from bs4 import BeautifulSoup
  9.  
  10. # Suppress only the single warning from urllib3 needed.
  11. requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
  12.  
  13. #def main (argv):
  14. url = ''
  15. user = ''
  16. passwd = ''
  17. hostname = ''
  18. prd = ''
  19.  
  20. # Get full command-line arguments
  21. full_cmd_arguments = sys.argv
  22.  
  23. # Keep all but the first
  24. argument_list = full_cmd_arguments[1:]
  25.  
  26. short_options = "hu:p:H:P:"
  27. long_options = ["username=","password=","hostname=","publicregistrationdomain"]
  28.  
  29. def usage():
  30.     print('check-sip-trunk.py -u <username> -p <password> -H <hostname> -P <publicregistrationdomain>')
  31.  
  32. try:
  33.     arguments, values = getopt.getopt(argument_list, short_options, long_options)
  34. except getopt.GetoptError as err:
  35.     print (str(err))
  36.     usage()
  37.     sys.exit(2)
  38.  
  39. if not arguments:
  40.   usage()
  41.   sys.exit(2)
  42.  
  43. for opt, arg in arguments:
  44.     if opt == '-h':
  45.         usage()
  46.         sys.exit()
  47.     elif opt in ("-u", "--username"):
  48.         user = arg
  49.     elif opt in ("-p", "--password"):
  50.         passwd = arg
  51.     elif opt in ("-H", "--hostname"):
  52.         hostname = arg
  53.         url = f"https://{hostname}/services/webapp/monitor/sip_i.php?sip_i=regstatus"
  54.     elif opt in ("-P", "--publicregistrationdomain"):
  55.         prd = arg
  56.  
  57. # Make a GET request to fetch the raw HTML content
  58. response = requests.get(url, auth=(user, passwd), verify=False)
  59.  
  60. # Parse the html content
  61. soup = BeautifulSoup(response.text, "lxml")
  62.  
  63. counter=0
  64. PRD=""
  65. PDDI=""
  66. STATUS=""
  67.  
  68. rows=[]
  69.  
  70. for td in soup.find_all("td")[:-1]:
  71.     if counter==0:
  72.         PRD=td.find('b').text.split('-',1)[1].strip()
  73.         counter+=1
  74.     elif counter==1:
  75.         if not "Public DDI" in td.text:
  76.             raise IndexError('Expected value "Public DDI" but got '+td.text+'')
  77.         counter+=1
  78.     elif counter==2:
  79.         if not "STATUS" in td.text:
  80.             raise IndexError('Expected value "STATUS" but got '+td.text+'')
  81.         counter+=1
  82.     elif counter==3:
  83.         PDDI=td.text
  84.         counter+=1
  85.     elif counter==4:
  86.         STATUS=td.text.strip()
  87.  
  88.         counter=0
  89.         if STATUS=="REGISTRATION SUCCESSFUL":
  90.             trunkstatus = "0 SIP-TRUNK - OK: Public Registration Domain - "+PRD+", Public DDI - "+PDDI+", Status - "+STATUS
  91.             rows.append(trunkstatus)
  92.         elif STATUS=="REGISTRATION FAILED":
  93.             trunkstatus = "2 SIP-TRUNK - CRITICAL: Public Registration Domain - "+PRD+", Public DDI - "+PDDI+", Status - "+STATUS
  94.             rows.append(trunkstatus)
  95.         else:
  96.             trunkstatus = "3 SIP-TRUNK - UNKNOWN: Public Registration Domain - "+PRD+", Public DDI - "+PDDI+", Status - "+STATUS
  97.             rows.append(trunkstatus)
  98.     else:
  99.         raise IndexError('Critical Error. Index is not within interval 0-4')
  100.  
  101. for item in rows:
  102.     if re.search(rf"\b{prd}\b",item):
  103.         print(item)
  104.         sys.exit()
  105. else:
  106.     print("Public Registration Domain \""+prd+"\" does not exist.")
  107.     sys.exit(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement