Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # importing the libraries
- import sys, getopt
- import requests
- import re
- from urllib3.exceptions import InsecureRequestWarning
- from bs4 import BeautifulSoup
- # Suppress only the single warning from urllib3 needed.
- requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
- #def main (argv):
- url = ''
- user = ''
- passwd = ''
- hostname = ''
- prd = ''
- # Get full command-line arguments
- full_cmd_arguments = sys.argv
- # Keep all but the first
- argument_list = full_cmd_arguments[1:]
- short_options = "hu:p:H:P:"
- long_options = ["username=","password=","hostname=","publicregistrationdomain"]
- def usage():
- print('check-sip-trunk.py -u <username> -p <password> -H <hostname> -P <publicregistrationdomain>')
- try:
- arguments, values = getopt.getopt(argument_list, short_options, long_options)
- except getopt.GetoptError as err:
- print (str(err))
- usage()
- sys.exit(2)
- if not arguments:
- usage()
- sys.exit(2)
- for opt, arg in arguments:
- if opt == '-h':
- usage()
- sys.exit()
- elif opt in ("-u", "--username"):
- user = arg
- elif opt in ("-p", "--password"):
- passwd = arg
- elif opt in ("-H", "--hostname"):
- hostname = arg
- url = f"https://{hostname}/services/webapp/monitor/sip_i.php?sip_i=regstatus"
- elif opt in ("-P", "--publicregistrationdomain"):
- prd = arg
- # Make a GET request to fetch the raw HTML content
- response = requests.get(url, auth=(user, passwd), verify=False)
- # Parse the html content
- soup = BeautifulSoup(response.text, "lxml")
- counter=0
- PRD=""
- PDDI=""
- STATUS=""
- rows=[]
- for td in soup.find_all("td")[:-1]:
- if counter==0:
- PRD=td.find('b').text.split('-',1)[1].strip()
- counter+=1
- elif counter==1:
- if not "Public DDI" in td.text:
- raise IndexError('Expected value "Public DDI" but got '+td.text+'')
- counter+=1
- elif counter==2:
- if not "STATUS" in td.text:
- raise IndexError('Expected value "STATUS" but got '+td.text+'')
- counter+=1
- elif counter==3:
- PDDI=td.text
- counter+=1
- elif counter==4:
- STATUS=td.text.strip()
- counter=0
- if STATUS=="REGISTRATION SUCCESSFUL":
- trunkstatus = "0 SIP-TRUNK - OK: Public Registration Domain - "+PRD+", Public DDI - "+PDDI+", Status - "+STATUS
- rows.append(trunkstatus)
- elif STATUS=="REGISTRATION FAILED":
- trunkstatus = "2 SIP-TRUNK - CRITICAL: Public Registration Domain - "+PRD+", Public DDI - "+PDDI+", Status - "+STATUS
- rows.append(trunkstatus)
- else:
- trunkstatus = "3 SIP-TRUNK - UNKNOWN: Public Registration Domain - "+PRD+", Public DDI - "+PDDI+", Status - "+STATUS
- rows.append(trunkstatus)
- else:
- raise IndexError('Critical Error. Index is not within interval 0-4')
- for item in rows:
- if re.search(rf"\b{prd}\b",item):
- print(item)
- sys.exit()
- else:
- print("Public Registration Domain \""+prd+"\" does not exist.")
- sys.exit(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement