#!/usr/bin/env python3
import os
import sys 
import pwd


from optparse import OptionParser
parser = OptionParser()
parser.add_option("--id",  dest="id",
                  help="set netns id = ip")

parser.add_option("--cmd", dest="cmd",
                  help="cmd to execute in network-namespace")
parser.add_option("--user", dest="user",
                  help="username")
(options, args) = parser.parse_args()
print("option",options)

if options.id is None:
    print("please set network-namespace --id=xx")
    sys.exit()

if options.cmd is None:
    print("exec: please set --cmd=xxxx")
    sys.exit()

cmd="sudo ip netns exec \"green_1{id}\" su \"{user}\" -c '{cmd}' "  # ok
#cmd="sudo /opt/netns/exec \"green_{id}\" su \"{user}\" -c 'screen -d -m -S \"green_{id}\" \"{cmd}\"' "  # ok
print(cmd)
cmd2=cmd.format(user=options.user,id=options.id,cmd=options.cmd)
print(cmd2)
os.system(cmd2)

