Echo multi-line variable content over ssh - variables

I'm trying to echo multiline variable over ssh, but the second line gets executed as a command.
msg="Hello World; Hello World2"; echo $msg; ssh -q newhost "echo "$msg""
Output:
Hello World; Hello World2
Hello World
bash: Hello: command not found

Is there a reason it needs to be a multi-line variable? why not send two distinct messages?
msg="Hello World"; msg2="Hello World2"; echo $msg; ssh -q localhost "echo "$msg""$msg2""
works fine
echo -e will let you interpret escaped characters like \n which you should use instead of semicolon ;

Related

Pass arguments for SQL statement in a shell file from another shell file through ssh command

I am passing command line arguments to a shell file i.e assignRole.sh which contains an SQL command which will use these arguments like below
ssh -o StrictHostKeyChecking=no -T $key < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt
This gives me error and does not accept arguments of name and open_mode and gives error, but if I execute the statement outside of ssh command like:
/oracle/oracle_user/makhshif/./assignRole.sh name open_mode
This runs fine
What is the problem with ssh command and how should I adjust these parameters so these can be accepted for the shell script assignRole.sh
< /oracle/oracle_user/makhshif/./assignRole.sh
This commands sends a content of that file to stdin. So obviously it can't process variables that you haven't send to remote machine. Just preprocess your script or create a script on remote machine and call it with arguments
Though it's even easier to pass variables like this:
ssh -o StrictHostKeyChecking=no -T $key "var1=$var1 var2=$var2" < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt
For example my function for executing update scripts on all cluster nodes:
# functions:
ssh_exec(){
local DESCR="$1"; shift
local SCRIPT="$1"; shift
local hosts=("$#")
echo =================================================
echo = $DESCR
echo = Going to execute $SCRIPT...
read -a res -p "Enter 'skip' to skip this step or press Enter to execute: "
if [[ $res = "skip" ]]
then
echo Skipping $SCRIPT...
else
echo Executing $SCRIPT...
for host in "${hosts[#]}"
do
local cur=${!host}
echo Executing $SCRIPT on $host - $cur...
sshpass -p "$rootpass" ssh -o "StrictHostKeyChecking no" root#${cur} \
"ns1=$ns1 ns2=$ns2 search=$search zoo1=$zoo1 zoo2=$zoo2 zoo3=$zoo3 node0=$node0 pass=$pass CURIP=$cur CURHOST=$host bash -s" \
<$SCRIPT >log-$SCRIPT-$cur.log 2>&1
echo Done.
done
echo =================================================
fi
}
Then I use it like this:
read -p "Please check that Solr started successfully and Press [Enter] key to continue..."
#Solr configset and collections:
ssh_exec "Solr configset and collections" script06.sh zoo1 zoo2 zoo3
This command executes script06.sh on 3 servers (zoo1,zoo2,zoo3)
As Sayan said, using < redirects the output of running the assignRole.sh script locally, but you want to execute that script on the remote host, with the arguments.
Pass the whole command as the final argument to ssh, in quotes:
ssh -o StrictHostKeyChecking=no -T $key "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" >> /oracle/oracle_user/dftest.txt
or split into multiple lines for readability:
ssh -o StrictHostKeyChecking=no -T $key \
"/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" \
>> /oracle/oracle_user/dftest.txt

Arguments of sh command ignored when using with ssh

In the following command, the first argument of sh command echo hey is ignored:
$ ssh localhost sh -c 'echo hey; echo ho'
ho
Why?
Your commandline is:
ssh localhost sh -c 'echo hey; echo ho'
ssh starts a shell on localhost and passes it the comandline:
sh -c echo hey; echo ho
The shell on localhost sees two commands. Both run fine.
The problem is that the first command is: sh -c echo hey
The option -c tells sh to execute the next argument. The next argument is echo. The extraneous hey argument is ignored.
To fix your problem, either change your quoting or just don't run the redundant shell:
ssh localhost "sh -c 'echo hey; echo ho'"
ssh localhost 'echo hey; echo ho'
The main confusion is probably that ssh concatenates all the non-option arguments it receives into a single string that it passes to the remote shell to execute.

SSH - Using grep with special chars

I'm trying to search some string on some files but I didn't find the good combination.
I want to use this command but I have an error in my syntax
grep -r -H "<?php $GLOBALS[" /var/www/vhosts/
I want to search (via SSH) the string <?php $GLOBALS[ on the files under /var/www/vhosts
Use this:
ssh user#host "grep -r -H '<?php \$GLOBALS\[' /var/www/vhosts/"

using awk in expect send getting can't read "1": no such variable error

I am sending awk command through expect send, when i am sending i am getting error but i can't read 1 no such variable
I did use {{}} mechansim but i did work,
expect "$prompt" {
send "awk {{print $1}} /mytest/test.log\r"
}
I tried with eascapse sequence \, but i didnot find any response expect_out(buffer),..etc
expect "$prompt" {
send "awk '{print \$1}' /mytest/test.log\r"
}
I tried with exec command also
expect "$prompt" {
send "exec awk {{print $1}} /mytest/test.log\r"
}
You have to use the curly braces to avoid substitution. Alternatively you have to escape the dollar sign and the curly braces too.
A couple of examples:
1. interacting with a program on your local machine:
#!/usr/bin/expect -d
spawn "/bin/bash"
set cmd "awk '\{print \$1\}' /mytest/test.log\r"
send $cmd
expect eof
puts $expect_out(buffer)
2. interacting with a remote program over ssh:
#!/usr/bin/expect -d
append cmd {awk '{print $1}' /mytest/test.log} "\r"
spawn ssh user#hostname
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "mypassword\r"
interact -o -nobuffer -re $prompt return
send $cmd
send "exit\r"
expect eof
puts $expect_out(buffer)

escape character with ssh

I'm trying to write several commands trought ssh connection bue I got problem with escape characters. Below an example of what I'd like to do:
/usr/bin/ssh mrtg#172.20.29.40 echo -e "ciao\nprova"
I got this result:
ciaonprova
instead of:
ciao
prova
if I use -e option for ssh:
/usr/bin/ssh -e mrtg#172.20.29.40 echo -e 'ciao\nprova'
I receive this error:
Bad escape character 'mrtg#172.20.29.40'.
Can someone give me a suggestion to let remote server interpret escape characters?
The -e option has nothing to do with your command (these are SSH escape characters, not shell).
You can just put your command in quotes:
/usr/bin/ssh mrtg#172.20.29.40 'echo -e "ciao\nprova"'