i have following script executes shell command java's java.lang.processbuilder
works ok if don't use arguments:
<cfif isdefined("form.cmd")> <cfif server.os.name contains "mac" or server.os.name contains "linux" or server.os.name "unix"> <cfset cmd = "sh -c #form.cmd#"> <cfelseif server.os.name contains "windows"> <cfset cmd = "cmd.exe /c #form.cmd#"> <cfelse> <cfset cmd = "sh -c #form.cmd#"> </cfif> <cfscript> str = htmleditformat(rereplace(cmd,"""","'","all")); exec = str.split(" "); pb = createobject("java","java.lang.processbuilder").init(exec); pb.redirecterrorstream(true); proc = pb.start(); proc.waitfor(); isr = createobject("java","java.io.inputstreamreader").init(proc.getinputstream()); br = createobject("java","java.io.bufferedreader").init(isr); line = br.readline(); while (isdefined("line")) { writeoutput(line & '<br>'); line = br.readline(); } br.close(); isr.close(); </cfscript> </cfif>
if #form.cmd#
uname
, output expect:
linux
but if #form.cmd#
uname -a
scripts executes uname
leaving outside -a
argument. thought use java array add arguments:
<cfscript> args = createobject("java", "java.util.arraylist").init(); args.add("sh"); args.add("-c"); args.add("uname"); args.add("-a"); pb = createobject("java","java.lang.processbuilder").init(args); pb.redirecterrorstream(true); proc = pb.start(); proc.waitfor(); isr = createobject("java","java.io.inputstreamreader").init(proc.getinputstream()); br = createobject("java","java.io.bufferedreader").init(isr); line = br.readline(); while (isdefined("line")) { writeoutput(line & '<br>'); line = br.readline(); } br.close(); isr.close(); </cfscript>
same problem. doing wrong?
"-a" being passed in argument sh, not uname. need put "uname -a" within same arg, ie:
args.add("sh"); args.add("-c"); args.add("uname -a");
if helps clarify, directly command line, want type:
sh -c "uname -a"
Comments
Post a Comment