Python eval is not working as a string how to use with shell code - python-3.x

I have a function like svn_check which basically check brnach in svn function is given below
def svn_check(request):
url ='eval `/volume/tools/bin/ssh-agent.sh`;printenv;ssh-add -l;/volume/tools/bin/svn info svn+ssh://cng-svn.tema.net/my-2009/branches/NEHA_NEGI'
output, message = subprocess.getstatusoutput(url)
print("CMD : " + str(url))
print("Message : " + str(message))
print("Return Code : " + str(output_br))
return HttpResponse(output_br)
i also have a branch with name NEHA_NEGI. but when i am executing this function it is returning me the error i dont know why it is returning me the error.
CMD : 'eval /volume/tools/bin/ssh-agent.sh;printenv;ssh-add -l;/volume/tools/bin/svn info svn+ssh://cng-svn.tema.net/my-2009/branches/NEHA_NEGI', referer: http://my-site.com:81/
Message : NOTE: ssh-agent is empty; use ssh-add to load your key(s)!, referer: http://my-site.com:81/
Return Code : 1, referer: http://my-site.com:81/
I am getting this in the error log. can anyone please help me relate this why I am getting this error? thanks in advance I am a newbie in shell language.
Or maybe it is happening because eval function is treated as a string. so there s any other way to type so eval can run properly.

Related

terraform 11 and "concat" function: parse error

TF v.0.11 (I know it is old but I need this one)
I try to configure output this way:
value = "${concat(aws_lambda_function.lambda.*.arn, [""])}"
The error message is
Error reading config for output FUNCTION_ARN: parse error at 1:46 :
expected expression but found "["
What am I doing wrong, how to fix that?
Reason: [""] don't work for v11.
Solution:
value = "${element(concat(aws_lambda_function.lambda.*.arn, list("")), 0)}"

Escaping double-backslash in groovy

I have a Groovy script that should read a value from a registry key, on a remote machine. When I run reg query command on the local machine, or from another machine on the network, I get back the correct value. I also get the correct value when I run the Groovy script against the local machine (removing "\\' + hostname + '\").
When I run the code listed below, I get the following error:
java.io.IOException: Cannot run program "\HKEY_LOCAL_MACHINE\SOFTWARE\Application\": CreateProcess error=2, The system cannot find the file specified
This leads me to believe that I am failing to escape the path correctly. If that's right, how should I escape a double-backslash?
Here is the script:
def hostname = '10.1.1.2'
def outVal = ''
try {
output = 'reg query \\\\' + hostname + '\\HKEY_LOCAL_MACHINE\\SOFTWARE\\SynAEM\\UDF1 -v PatchGroup'.execute().text
outVal = output.tokenize(' ')[-1]
}
catch(Exception e) {
outVal = 'NotSpecified'
println e
}
println 'PatchGroup=' + outVal
return 0
Your problem has nothing to do with backslashes. It's to do with precedence. What you wrote is equivalent to:
output = 'reg query \\\\' + hostname +
('\\HKEY_LOCAL_MACHINE\\SOFTWARE\\SynAEM\\UDF1 -v PatchGroup'.execute().text)
The execute() method was trying to run the last string, ie the registry name. What you need is:
output = ('reg query \\\\' + hostname + '\\HKEY_LOCAL_MACHINE\\SOFTWARE\\SynAEM\\UDF1 -v PatchGroup').execute().text
or, possibly a bit clearer:
output = "reg query \\\\$hostname\\HKEY_LOCAL_MACHINE\\SOFTWARE\\SynAEM\\UDF1 -v PatchGroup".execute().text

Jenkins: ${BUILD_LOG, maxLines, escapeHtml} not working

I am trying to use "${BUILD_LOG, maxLines, escapeHtml}" like discribed in:
How can I take last 20 lines from the $BUILD_LOG variable?
Unfortunately it doesn't work for me.
I get this error:
Script1.groovy: 114: expecting anything but ''\n''; got it anyway # line 114, column 301.
arted by user MYUSERNAME
My code in this line is:
msg.setText("This build (" + build.getFullDisplayName()
+ " ) contains the following tasks:\n\nTASK\t\t\t IMPLEMENTER:\n"
+ taskList + "\n\n\nLink to this
build: ${BUILD_URL} \n ${BUILD_LOG, maxLines=9999, escapeHtml=false}" );
If I take this out the following, it works. Thats why my guess is, that "BUILD_LOG" is not working anymore?
${BUILD_LOG, maxLines=9999, escapeHtml=false}
EDIT:
Maybe as addition: I am trying to do this withing the PreSend groovy script.
Since I am building the Email text dynamically.
${BUILD_URL} works fine, ${BUILD_LOG, maxLines=9999, escapeHtml=false} doesn't (for me) i am looking for a solution for this...
the msg object is a java MimeMessage.
Thanks,
Daniel
That error message is usually related to not closed quotes, comments started with / instead of //, etc. In your code the only thing I can see is that your third line is not finished properly, i.e., after "\n\n\nLink to this you are not closing double quotes and instead you are starting a new line (thereby the expecting anything but ''\n''.
Try to write the whole line:
msg.setText("This build (" + build.getFullDisplayName()
+ " ) contains the following tasks:\n\nTASK\t\t\t IMPLEMENTER:\n"
+ taskList + "\n\n\nLink to this build: ${BUILD_URL} \n ${BUILD_LOG, maxLines=9999, escapeHtml=false}" );
or close the quotes instead:
msg.setText("This build (" + build.getFullDisplayName()
+ " ) contains the following tasks:\n\nTASK\t\t\t IMPLEMENTER:\n"
+ taskList + "\n\n\nLink to this "
+ "build: ${BUILD_URL} \n ${BUILD_LOG, maxLines=9999, escapeHtml=false}" );
I used the below and it's working fine for me.
${BUILD_LOG, maxLines=10, escapeHtml=false}
I tried with Jenkins version 1.617
Have you tried to set escapeHtml=true? It may happen that this token expanded as is and then string in " " becomes not valid.
In latest version variable ${BUILD_LOG} wasn't available for me - only solution to get log in email content was for me setting:
msg.setText(build.getLog())
as Default Pre-send Script in Jenkins global configuration...

Lua script unable to detect/catch error while executing invalid linux command

I have the following function that works fine as long as I give it a valid command to execute. As soon as I give it a non-existent command, the script is interrupted with an error message.
#!/usr/bin/lua
function exec_com(com)
local ok,res=pcall(function() return io.popen(com) end)
if ok then
local tmp=res:read('*a')
res:close()
return ok,tmp
else
return ok,res
end
end
local st,val=exec_com('uptime')
print('Executed "uptime" with status:'..tostring(st)..' and value:'..val)
st,val=exec_com('zzzz')
print('Executed "zzzz" with status:'..tostring(st)..' and value:'..val)
When I run the script above I get the following output:
Executed "uptime" with status:true and value: 18:07:38 up 1 day, 23:00, 3 users, load average: 0.37, 0.20, 0.20
sh: zzzz: command not found
Executed "zzzz" with status:true and value:
You can clearly see above that pcall() function still reported success when executing "zzzz" which is odd.
Can someone help me devise a way to catch an exception when executing a non-existent or ill-formed Linux command using Lua script? Thanks.
Edit: Restated my request after getting the clarification that pcall() works as expected, and the problem is due to popen() failing to throw an error.
I use a method which is similar to your "temporary workaround" but which gives you more information:
local cmd = "uptime"
local f = io.popen(cmd .. " 2>&1 || echo ::ERROR::", "r")
local text = f:read "*a"
if text:find "::ERROR::" then
-- something went wrong
print("error: " .. text)
else
-- all is fine!!
print(text)
end
If you look at io.popen(), you'll see that it'll always return a file handle.
Starts program prog in a separated process and returns a file handle
that you can use to read data from this program (if mode is "r", the
default) or to write data to this program (if mode is "w").
Since, a file handle returned is still a valid value for lua, the pcall(), your local function inside the pcall is returning a true value (and an error is not being propagated); thereby, giving you a true status and no output.
I have come up with my own temporary workaround that pipes the error to /dev/null and determines the success/failure of executed command based on the text received from io.popen():read('*a') command.
Here is my new code:
#!/usr/bin/lua
function exec_com(com)
local res=io.popen(com..' 2>/dev/null')
local tmp=res:read('*a')
res:close()
if string.len(tmp)>0 then
return true,tmp
else
return false,'Error executing command: '..com
end
end
local st,val=exec_com('uptime')
print('Executed "uptime" with status:'..tostring(st)..' and value:'..val)
st,val=exec_com('cat /etc/shadow')
print('Executed "cat /etc/shadow" with status:'..tostring(st)..' and value:'..val)
And the corresponding output is now correct:
Executed "uptime" with status:true and value: 00:10:11 up 2 days, 5:02, 3 users, load average: 0.01, 0.05, 0.19
Executed "cat /etc/shadow" with status:false and value:Error executing command: cat /etc/shadow
In my example above I am creating a "generic" error description. This is an intermediate fix and I am still interested in seeing alternative solutions that can return a more meaningful error message describing why the command failed to execute.
Rather than taking the time reading the whole file into a variable, why not just check if the file is empty with f:read(0)?
Local f = io.popen("NotExist")
if not f:read(0) Then
for l in st:lines() do
print(l)
end
else
error("Command Does Not Exist")
end
From the lua Manual:
As a special case, io.read(0) works as a test for end of file: It returns an empty string if there is more to be read or nil otherwise.

how to check if a colmmande return is true or false selenium-ide

I'm trying to get a command return with selenium ide by doing it :
storeTextPresent|myText|title
gotoIf|storedVars.tite|true
echo|${"true"}
but it doesnt work...i have : [error] Unexpected Exception: fileName -> chrome://flowcontrol/content/extensions/goto-sel-ide.js?1347871647495, lineNumber -> 120.
Does anybody know how to get the return?
Thank you
I didn't check for boolean support on gotoIf, but if gotoIf wants to go to a label, I don't see a label defined in the above script.
Also, the "storedVars.tite" reference looks to be misspelled.

Resources