Here's the code:
inputDomain = subprocess.Popen("cat /etc/localdomains", shell=True, stdout=subprocess.PIPE)
domains = inputDomain.stdout.read().splitlines()
for domain in domains:
cmd = "whmapi1 domainuserdata domain " + domain
output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
jsonS = json.dumps(output.communicate())
print json.loads(jsonS)['data']
here's there error
root@server [~/testP]# python copie.py Traceback (most recent call last): File "copie.py", line 18, in print json.loads(jsonS)['data'] TypeError: list indices must be integers, not str
and this is an example of the json i need to parse:
{
"data":{
"userdata":{
"phpopenbasedirprotect":1,
"options":"ExecCGI Includes",
"ip":"10.0.0.1",
"hascgi":"1",
"group":"user",
"usecanonicalname":"Off",
"scriptalias":[
{
"url":"/cgi-bin/",
"path":"/home/user/public_html/cgi-bin"
},
{
"url":"/cgi-bin/",
"path":"/home/user/public_html/cgi-bin/"
}
],
"user":"user",
"ifmodulemodsuphpc":{
"group":"user"
},
"owner":"root",
"documentroot":"/home/user/public_html",
"userdirprotect":"",
"serveralias":"parkeddomain.com www.parkeddomain.com www.example.com",
"port":"80",
"homedir":"/home/user",
"ifmoduleconcurrentphpc":{
},
"customlog":[
{
"target":"/usr/local/apache/domlogs/example.com",
"format":"combined"
},
{
"target":"/usr/local/apache/domlogs/example.com-bytes_log",
"format":"\"%{%s}t %I .\\n%{%s}t %O .\""
}
],
"servername":"example.com",
"serveradmin":"[email protected]"
}
}
So i need the user and the domaine, but python always answer that i need a int. Thanks for the help guys.
I had problems running the above under Python3, so here is a way to achieve what OP is asking in Python3
import subprocess
import json
def getProcessOutput(cmd):
process = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE)
process.wait()
data, err = process.communicate()
if process.returncode is 0:
return data.decode('utf-8')
else:
print("Error:", err)
return ""
for domain in getProcessOutput("cat /etc/localdomains").splitlines():
cmd = "whmapi1 domainuserdata domain " + domain
print(json.loads(getProcessOutput(cmd))['data'])
It outputs Error: None
because my machine doesn't have /etc/localdomains
but otherwise it seems to work just fine.
since your process returns a json
string, there's no need to dump
it to load it again.
# stdout, stderr
jsonS,_ = output.communicate()
now you have a string, that you can load using json
d = json.loads(jsonS)
now d['data']
yields the info you want
Aside: as I said:
inputDomain = subprocess.Popen("cat /etc/localdomains", shell=True, stdout=subprocess.PIPE)
domains = inputDomain.stdout.read().splitlines()
could be replaced by native python:
with open("/etc/localdomains") as f: domains = f.read.splitlines()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With