Skip to content

Instantly share code, notes, and snippets.

@hiccupzhu
Created May 31, 2013 09:26
Show Gist options
  • Save hiccupzhu/5683864 to your computer and use it in GitHub Desktop.
Save hiccupzhu/5683864 to your computer and use it in GitHub Desktop.
parse some confs with python script.
#!/usr/bin/python
import os
import sys
import getopt
import re
channels = dict();
workmode=""
chname=""
streamaddr=""
manageaddr=""
sourceaddr=""
vpid=""
apid=""
class Channel:
def __init__(self, name):
self.name = name;
self.streamaddr={"addr":"", "port":-1, "listening":False};
self.manageaddr={"addr":"", "port":-1, "listening":False};
self.sourceaddr={"addr":"", "port":-1, "listening":False};
self.vpid = 0;
self.apid = 0;
def usage():
print "Release time:xxxx-xx-xx"
print " -v|--version : version"
print " -h|--help : help"
print " -a|--add : add a channel"
print " -d|--del <channel-name> : delete channel"
print " -l|--list : list all channels"
print " --name : channel name"
print " --streamaddr <...> : stream address like[0.0.0.0:21000]"
print " --manageaddr <...> : manage address like[0.0.0.0:11000]"
print " --sourceaddr <...> : source address like[udp://192.168.xxx.xxx:xxxx]"
print " --vpid <...> : source video pid like[18b0]"
print " --apid <...> : source audio pid like[18b1]"
print "e.g.: pytool --add --name example --streamaddr 0.0.0.0:20130 --manageaddr 0.0.0.0:10130 --sourceaddr udp://239.100.194.2:1234 --vpid 18b0 --apid 18b1"
return;
def check_root():
if (os.getuid() != 0):
print "Permission denied! Please use root try again.";
exit(0);
def channel_add():
if chname == "":
print "chname must to set!";
exit(0);
if streamaddr == "":
print "chname must to set!";
exit(0);
if manageaddr == "":
print "chname must to set!";
exit(0);
if sourceaddr == "":
print "chname must to set!";
exit(0);
if vpid == "":
print "chname must to set!";
exit(0);
if apid == "":
print "chname must to set!";
exit(0);
check_root();
cmds = """
chname="%s"
streamaddr="%s"
manageaddr="%s"
sourceaddr="%s"
vpid="%s"
apid="%s"
target="/etc/itvencoder/$chname.conf"
cp /etc/template.conf $target
perl -pi -e "s/STREAM_ADDR/$streamaddr/g" $target
perl -pi -e "s/MANAGE_ADDR/$manageaddr/g" $target
perl -pi -e "s#SOURCE_ADDR#$sourceaddr#g" $target
perl -pi -e "s/VPID/$vpid/g" $target
perl -pi -e "s/APID/$apid/g" $target
target="/etc/supervisord.d/$chname.ini"
mkdir -p /var/log/itvencoder/$chname
cp /etc/template.ini $target
perl -pi -e "s/CHNAME/$chname/g" $target
""" %(chname, streamaddr, manageaddr, sourceaddr, vpid, apid);
os.system(cmds);
def channel_del():
if chname == "":
print "chname must to set!";
exit(0);
get_channel_list();
try:
channels[chname].name;
except KeyError:
print "channel[%s] NOT existing, please check it!" %(chname);
exit(0);
check_root();
cmds = """
chname="%s"
rm -f "/etc/itvencoder/$chname.conf"
rm -f "/etc/supervisord.d/$chname.ini"
rm -rf /var/log/itvencoder/$chname
""" %(chname);
os.system(cmds);
def channel_get_name(line):
line = re.sub("^/etc/itvencoder/", "", line);
line = re.sub(".conf:.*", "", line);
return line;
def get_network_addr(line):
addrs = "";
match = re.search("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}", line);
if match:
addrs = match.group(0);
else:
return {"addr":"", "port":-1};
addr = addrs.split(":");
return {"addr":addr[0], "port":int(addr[1])};
def get_channel_list():
lines = os.popen(
"""
ls /etc/itvencoder/*.conf | xargs grep -E "^httpstreaming|^httpmgmt|^ *uri" | perl -pe "s/#.*$//g" | perl -pe "s/<\/var>//g" | perl -pe "s/<.*>//g" | perl -pe "s/ //g"
""").readlines();
for line in lines:
line = line.strip();
name = channel_get_name(line);
try:
channels[name].name;
except KeyError:
channels[name] = Channel(name);
if line.find("httpstreaming=") >= 0:
channels[name].streamaddr = get_network_addr(line);
elif line.find("httpmgmt=") >= 0:
channels[name].manageaddr = get_network_addr(line);
elif line.find("uri=") >= 0:
channels[name].sourceaddr=get_network_addr(line);
cmd = '''netstat -nn -tul | grep "LISTEN" | awk {'print $4'} ''';
for key in channels:
lines = os.popen(cmd + " | grep " + str(channels[key].streamaddr["port"])).readlines();
if lines:
channels[key].streamaddr["listening"] = True;
lines = os.popen(cmd + " | grep " + str(channels[key].manageaddr["port"])).readlines();
if lines:
channels[key].manageaddr["listening"] = True;
def channel_list():
get_channel_list();
i = 0;
for key in channels:
print "Channel[%d]-Name:%s" %(i, channels[key].name);
print " streamaddr:%s" %(channels[key].streamaddr);
print " manageaddr:%s" %(channels[key].manageaddr);
print " sourceaddr:udp://%s:%d" %(channels[key].sourceaddr["addr"], channels[key].sourceaddr["port"]);
i += 1;
if __name__ == "__main__":
opts, args = getopt.getopt(sys.argv[1:],'vhad:l', ['help', 'version', 'add', 'del=', 'list', 'name=', 'streamaddr=', 'manageaddr=', 'sourceaddr=', 'vpid=', 'apid=']);
# print "opts=" + str(opts)
# print "args=" + str(args)
for key,value in opts:
if key in ("-v", "--version"):
print "iTVEncoder 0.3.0";
exit(0);
elif key in ("-h", "--help" ):
usage();
exit(0);
elif key in ("-a", "--add"):
workmode = "add";
elif key in ("-d", "--del"):
workmode = "del";
chname = value;
elif key in ("-l", "--list"):
workmode = "list";
elif (key == "--name"):
chname = value;
elif (key == "--streamaddr"):
streamaddr = value;
elif (key == "--manageaddr"):
manageaddr = value;
elif (key == "--sourceaddr"):
sourceaddr = value;
elif (key == "--vpid"):
vpid = value;
elif (key == "--apid"):
apid = value;
else:
print "UNKONOWN args:" + str(key);
if(workmode == "add"):
channel_add();
elif(workmode == "list"):
channel_list();
elif(workmode == "del"):
channel_del();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment