use of inverted commas (" ") in sh command in jenkins file - jenkins

Hi i am using scp command in jenkinsfile via sh ' '.
My command is:
sh 'sshpass -p "my-password" scp /home/jenkinshome.........'
but it fails and in console output i find that inverted commas (" ") from the command is gone.
I am not sure what is happening there. Is there any othe way to pass my password.

The solution that always works for me is I first create password less authentication between 2 servers where I want to scp. Works like a charm. No need to pass password.

Related

How to excape '&' in a sh()-call in jenkins

I want to call curl on a webpage with get parameters from the jenkins-groovy-script via sh:
sh("curl http://example.com/page.cgi?param1=a&param2=b&param3=c")
But the command is split at the '&' and in the output log of jenkins there is something like:
+ param3=c
+ curl http://example.com/page.cgi?param1=a
+ param2=b
Of course the call to the web server only contains the first parameter.
I tried already with different kind of escaping but without success.
How can I make the call including all the parameters?
Just like in the command line, you have to quote it or escape it. Research quoting and escaping in sh shell.
Try:
sh("curl 'http://example.com/page.cgi?param1=a&param2=b&param3=c'")
sh('curl "http://example.com/page.cgi?param1=a&param2=b&param3=c"')
sh("curl http://example.com/page.cgi?param1=a\\&param2=b&param3=c")
sh("curl \"http://example.com/page.cgi?param1=a&param2=b&param3=c\"")
& in shell means to run something in the background, like sleep 10 & wait.

special characters in password in curl command

In my password, there are two special characters are there > and :, when i run curl command it's not working with error:
The system cannot find the file, even i tried escape characters in my password as \> and \:.
Also i did double quotes e.g. curl -u "abc\\sys_account:Tabc>fg:Eh" abc.org
Still I haven't get any luck.
Any help will be much appreciated.
Also it's not working with WithCredentials Pipeline syntax in Jenkins.

Trying to store ssh command as variable - 'bad variable name'

I'm writing a shell script in Jenkins.
I'm trying to store the output from this ssh command into a variable so I can perform some edits(grep) to the output.
export OUTPUT=$(ssh -q -o StrictHostKeyChecking=no ${USER_AND_SERVER} "sudo hab svc status")
I keep getting a "bad variable name" error when I try to run this. I'm not sure if it's the fact I'm passing another variable in the ssh command 'USER_AND_SERVER'.
Try with the below syntax. Assign the variable first without the export, and THEN do the export:
OUTPUT=$(ssh … )
export OUTPUT

How to escape $ in a travis ci encrypted environment variable?

Travis CI's user documentation has a section on how to escape secure environment variables. It seems not to work for the '$' symbol. Is there anything special that needs done for the '$' symbol?
I setup this example. In .travis.yml:
travis encrypt "FAKE_PASSWORD=H3llo\\#Worl\\$d" -a
In my script I echo the variable and get:
fake password is H3llo#Worl
It appears that $d is being replaced with nothing. How can I fix this?
The problem is when running travis encrypt the $ symbol needs to be escaped for the command as well as when the variable is used. With two backslashes \\ it only creates one backslash in the variable and $d is still expanded by bash. Using three backslashes fixes the issue.
travis encrypt "FAKE_PASSWORD=H3llo\\#Worl\\\$d" -a
\\ creates a single backslash and \$ creates a $ symbol that is not expanded by bash. When travis runs the bash command to create that variable looks like
FAKE_PASSWORD=H3llo\\#World\$d
This is what bash expects when using a $ in a variable.

"su - user" not working the same when scripted

I have a program that was installed under "/home/user" and whose commands ONLY work if I execute them from the command line like so:
root#server:~# su - user
user#server:~# command
However, I am trying to script these commands like so:
su - user -c 'command'
Although the script executes and command is 'run", parts of the program do not work correctly.
I made sure the $PATH variable was the same between "root" and "user", and added a couple environmental variables that were in /home/user/.profile to the root one. Still, something is missing.
What is special about "su - user" from command line vs in a script, and how can I account for what's missing?
I cannot say for sure what is different in the hidden parts of your command. But I would like to point out things that may help:
Consider the difference between single and double quotas: 'command' and "command". The latter will expand content: http://www.howtogeek.com/howto/29980/whats-the-difference-between-single-and-double-quotes-in-the-bash-shell/ So if I write in my shell su - myname -c 'echo $PATH', I see a very different path than with su - myname -c "echo $PATH".
Try to find out more by "debugging" like su - myname -c "echo SHOW_ME_SOMETHING". Maybe you can observe subparts of your command working differently.
Handling of the path is tricky, meaning there is login emulating with different reset states on different systems. Have a look here: https://superuser.com/questions/193277/what-happens-to-the-environment-when-you-run-su-c
Hope this helps.

Resources