Skip to content

Instantly share code, notes, and snippets.

@thuma
Created July 8, 2020 21:08
Show Gist options
  • Save thuma/8d27bb740c01f2d75a4d0a47fd25222f to your computer and use it in GitHub Desktop.
Save thuma/8d27bb740c01f2d75a4d0a47fd25222f to your computer and use it in GitHub Desktop.
import subprocess
import configparser
config = configparser.ConfigParser()
config.read('/etc/asterisk/sip.conf')
sipusers = config.sections()
sipusers = sipusers[2:]
def execute(cmd):
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
iptablesdata = str(subprocess.check_output(["iptables-save"]))
pack = ""
for path in execute(["ngrep","-W","byline","-d","ens1","port","5060"]):
if path.startswith("U "):
pack = path
if path.startswith(("U ","From:")):
pack = pack + path
for user in sipusers:
if "From: <sip:"+user+"@domain" in pack:
ipnummer = pack.split(" ")[1].split(":")[0]
todo = str("-A INPUT -s "+ipnummer+"/32 -p udp -m udp --dport 5060 -j ACCEPT")
if not todo in iptablesdata:
subprocess.call("iptables " + todo, shell=True)
iptablesdata += todo
print(pack.split(" ")[1].split(":")[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment