I am writing command in bash script as:-
${SSH} ${USER}#${HOST} mkdir -p ${DEST_LOG_DIR};
However on Debug flow I get as:
+ 'ssh -o StrictHostKeyChecking=no' -i xxxxx.pem user#host 'mkdir -p test"
./test.sh: line 454: ssh -o StrictHostKeyChecking=no: command not found
The varibable SSH is defined as
SSH="ssh -o StrictHostKeyChecking=no"
Why it is giving me this error?
"I'm trying to put a command in a variable, but the complex cases always fail!"
SSH=(ssh -o StrictHostKeyChecking=no)
...
"${SSH[#]}" ...
Related
I'm trying to run a complete script while the ssh session is live instead of single commands.
Here is my current code:
sh "ssh -tt -o StrictHostKeyChecking=no ubuntu#IPV4_DNS uptime"
sh "ssh -v ubuntu#IPV4_DNS docker pull X:${BUILD_NUMBER}"
sh "ssh -v ubuntu#IPV4_DNS docker rm -f test"
sh "ssh -v ubuntu#IPV4_DNS docker run --name=test -d -p 3000:3000X:${BUILD_NUMBER}"
The desired code is something like this, but the following doesn't work:*
sh "ssh -tt -o StrictHostKeyChecking=no ubuntu#IPV4_DNS uptime"
sh ''' ssh -v ubuntu#IPV4_DNS docker pull X:${BUILD_NUMBER}
&& docker rm -f test && docker run --name=test -d -p 3000:3000X:${BUILD_NUMBER}
'''
ssh something here && something else && another one
runs something here in the ssh session, and something else and another one locally. You want to add quotes to pass the entire command line to ssh.
sh "ssh -tt -o StrictHostKeyChecking=no ubuntu#IPV4_DNS uptime"
sh """ssh -v ubuntu#IPV4_DNS 'docker pull X:${BUILD_NUMBER} &&
docker rm -f test &&
docker run --name=test -d -p "3000:3000X:${BUILD_NUMBER}"'
"""
I switched to triple double quotes instead of triple single quotes, assuming you want Jenkins to expand ${BUILD_NUMBER} for you.
The original question asked about Bash, but for the record, you are running sh here, not Bash. If you wanted to use Bash features in a Jenkinsfile, you can add a shebang #!/usr/bin/env bash or similar as the very first line of the command. But that's not necessary here; all these commands are simple and completely POSIX. (Maybe see also Difference between sh and bash)
but needed execute $() in server, it is running before ssh
ssh -t -o StrictHostKeyChecking=no -i "${SSH_KEY}" "${HOST}" "$(command)"
this is a script that I need to run that code on the server
If you run that, it will execute whats inside of the $() before connecting to the server. Then it will send the result to be executed.
I think what you want is do this :
ssh -t -o StrictHostKeyChecking=no -i "${SSH_KEY}" "${HOST}" 'command'
Where command might be something like :
ls -lh | grep someword
Let me know if I am correct.
Error Msg:
command-line: line 0: Bad configuration option:
sh '''ssh -i ${rundeck_rsa_key} -o StrictHostKeyChecking=no -o centos#xxxx.net "sudo su -c "sh ./home/centos/releases/xx.sh" rundeck"'''
Broken Down command (I just made the above command for your convenience)
sh '''ssh -i ${rundeck_rsa_key} -o StrictHostKeyChecking=no
-o centos#xxxx.net "sudo su -c "sh ./home/centos/releases/xx.sh" servc"'''
I'm trying to
ssh into the server
change user to "servc"
execute xx.sh shell
I think there is a syntax error on "sudo su -c "sh ./home/centos/releases/xx.sh" servc"
Do you have any clue?? :D
You can't nest a double quoted string inside another without escaping the inner ones.
Try this:
sh '''ssh -i ${rundeck_rsa_key} -o StrictHostKeyChecking=no -o centos#xxxx.net "sudo su -c \"sh ./home/centos/releases/xx.sh\" rundeck"'''
I am trying to create directory with sudo user permission over SSH.
Here is the command i formed
some_command "ssh -t userA#host bash -c \"\'sudo -u userB bash -c \" mkdir -p /home/userB/dir_to_create \" \'\" "
here some_command is part of expect script.
I am getting this error :-
[sudo] password for userB:
mkdir: missing operand
Try `mkdir --help' for more information.
Connection to host closed.
If i run
sudo -u userB bash -c "mkdir /home/userB/dir_to_create"
it works.
ssh -t user#host "bash -c \"sudo -u otherUser bash -c 'mkdir -p /home/userB/dir_to_create'\""
should work
Based on comment from Mark Plotnick , i figured out the answer .
Here is the solution to it
spawn bash -c "ssh -t userA#host \"sudo -u userB bash -c 'mkdir -p /home/userB/perf_tools' \" "
Trick is that , single quote and double quote placements make all the magic happen in this script.
I need to ssh into a machine and execute a bunch of commands under sudo bash. Here is what I've tried:
sshpass -p "vagrant" ssh vagrant#33.33.33.100 "sudo bash -i -c <<EOF
echo
ls
echo
EOF"
But it throws me 'bash: -c: option requires an argument\n'. How can I fix this?
You need to remove -c from your command line to make it accept heredoc:
sshpass -p "vagrant" ssh vagrant#33.33.33.100 "sudo bash <<EOF
echo
ls
echo
EOF"
Also you may remove -i (interactive) option too.
bash -c expects you to provide all commands on command line so this may work too:
sshpass -p "vagrant" ssh vagrant#33.33.33.100 "sudo bash -c 'echo; ls; echo'"