Strange bash errors when passing script to ssh - bash

I'm trying to run a local script remotely that I'm calling with some other code. The basic formate is
ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}
I get the error line 24: ${Command}: ambiguous redirect
Command is a string with the name of the script I want to run and its arguments. If I change the script to just print the command as
echo "ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}"
and then run the command myself it works just fine.
I've tried putting the command in a temp variable and then call it that way, like:
TEMP="ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}"
$TEMP
echo $TEMP
This results in No such file or directory. Again the echoed version of the command runs just fine at the command line.
Anyone know what I'm doing wrong here?

It seems that executing $TEMP doesn't work correctly, as the whole string 'bash -s' -- < ${Command} is given in argument to ssh. And in fact if you create a file called ${Command} on you remote host you will get an error bash: bash -s: command not found.
A solution is to uses eval like this :
eval $TEMP
This really does what it should.

Related

Connecting to SSH using a bash script

I am trying to write a bash script to make things faster. Is it not possible to connect to the server with the code below in a bash script? I can't make it work, even though it works in the terminal.
#!/bin/bash -x
echo "Starting connection script"
sh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
What I get when I run is a not found output for each line in the pem file,
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: -----BEGIN: not found
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: adsnaleAFemasdsdsdnds: not foundMadfdasfdasfnda;vonraada
...
Some debug is needed.
Please change:
ssh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
to:
#!/bin/bash -x
echo "Starting connection script"
ssh -vi /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
does it produce an idea about the reason ?
you are calling sh which is kind of shell change it to ssh

Execute a statement after logging in to SSH

I am writing a script in which i have to execute a set of statments after logging in via SSH. So i am using this command
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND "&& exec bash -l"
Now this works if i give COMMAND as any single line statement such as ls or mkdir, but its not working if i put the following in COMMAND
COMMAND= "if [ $B==$A ]
then
echo 'ERROR'
fi "
I tried putting \ at the end of each line,changed the indentation,put it into a single line but its throwing error
fi : command not found
syntax error near unexpected token `&&'
The first problem is the ssh statement itself. ssh command should be run like:
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND && exec bash -l
Second one is that COMMAND variable should be assigned as one line like:
COMMAND="if [ $B==$A ]; then echo 'ERROR'; fi;"
I hope this helps.

is it possible to use variables in remote ssh command?

I'd like to execute several commands in sequence on a remote machine, and some of the later commands depend on earlier ones. In the simplest possible example I get this:
ssh my_server "echo this is my_server; abc=2;"
this is my_server
abc=2: Command not found.
ssh my_server "echo this is my_server; abc=2; echo abc is $abc"
abc: undefined variable
For a bit of background info, what I actually want to do is piece together a path and launch a java application:
ssh my_server 'nohup sh -c "( ( echo this is my_server; jabref_exe=`which jabref`; jabref_dir=`dirname $jabref_exe`; java -jar $jabref_dir/../jabref.jar` $1 &/dev/null ) & )"' &
jabref_dir: Undefined variable.
That way, whenever jabref gets updated to a new version on the server, I won't have to manually update the path to the jar file. The jabref executable doesn't take arguments, but launching it with java -jar does, which is why I have to juggle the path a bit.
At the moment I have the list of commands in a separate script file and call
ssh my_server 'nohup sh -c "( ( my_script.sh &/dev/null ) & )"' &
which works, but since the ssh call is already inside one script file it would be nice to have everything together.
In this example
ssh my_server "echo this is my_server; abc=2;"
abc is set on the remote side, so it should be clear why it is not set on your local machine.
In the next example,
ssh my_server "echo this is my_server; abc=2; echo abc is $abc"
your local shell tries to expand $abc in the argument before it is ever sent to the remote host. A slight modification would work as you expected:
ssh my_server 'echo this is my_server; abc=2; echo abc is $abc'
The single quotes prevent your local shell from trying to expand $abc, and so the literal text makes it to the remote host.
To finally address your real question, try this:
jabref_dir=$( ssh my_server 'jabref_exe=$(which jabref); jabref_dir=$(dirname $jabref_exe);
java -jar $jabref_dir/../jabref.jar > /dev/null; echo $jabref_dir' )
This will run the quoted string as a command on your remote server, and output exactly one string: $jabref_dir. That string is captured and stored in a variable on your local host.
With some inspiration from chepner, I now have a solution that works, but only when called from a bash shell or bash script. It doesn't work from tcsh.
ssh my_server "bash -c 'echo this is \$HOSTNAME; abc=2; echo abc is \$abc;'"
Based on this, the code below is a local script which runs jabref on a remote server (although with X-forwarding by default and passwordless authentication the user can't tell it's remote):
#!/bin/bash
if [ -f "$1" ]
then
fname_start=$(echo ${1:0:4})
if [ "$fname_start" = "/tmp" ]
then
scp $1 my_server:$1
ssh my_server "bash -c 'source load_module jdk; source load_module jabref; java_exe=\$(which java); jabref_exe=\$(which jabref); jabref_dir=\$(echo \${jabref_exe%/bin/jabref});eval \$(java -jar \$jabref_dir/jabref.jar $1)'" &
else
echo input argument must be a file in /tmp.
else
echo this function requires 1 argument
fi
and this is the 1-line script load_module, since modulecmd sets environment variables and I couldn't figure out how to do that without sourcing a script.
eval `/path/to/modulecmd bash load $1`;
I also looked at heredocs, inspired by How to use SSH to run a shell script on a remote machine? and http://tldp.org/LDP/abs/html/here-docs.html. The nice part is that it works even from tcsh. I got this working from the command line, but not inside a script. That's probably easy enough to fix, but I've got a solution now so I'm happy :-)
ssh my_server 'bash -s' << EOF
echo this is \$HOSTNAME; abc=2; echo abc is \$abc;
EOF

executing script using ssh inside a loop

I am trying to write a script, where I have a loop to login to multiple remote machines and execute a script inside each machine. Here is an example:
for ((j=1; j < 2; j++)); do
mchname="n"$j
ssh -T $mchname <<'ENDSSH'
./run_script < input > output &
ENDSSH
done
Whenever I try to execute the above script I get:
"warning: here-document at line 37 delimited by end-of-file (wanted `ENDSSH')"
I am new to ssh, so I am sure I am making a silly mistake. Can anyone suggest me a solution?
Thanks.
There's a problem in your bash script. The heredoc end tag (ENDSSH in your script) cannot be indented.
Try this instead:
ssh -T $mchname <<'ENDSSH'
./run_script < input > output &
ENDSSH
# ^ no indentation for that line
Edit:
Also, you can run a command on the remote system by passing it as an argument to ssh, rather than providing it as standard input. The command will be executed by the user's remote shell:
ssh -T $mchname './run_script < input > output &'

using ssh with stat command bash [duplicate]

I'd like to execute several commands in sequence on a remote machine, and some of the later commands depend on earlier ones. In the simplest possible example I get this:
ssh my_server "echo this is my_server; abc=2;"
this is my_server
abc=2: Command not found.
ssh my_server "echo this is my_server; abc=2; echo abc is $abc"
abc: undefined variable
For a bit of background info, what I actually want to do is piece together a path and launch a java application:
ssh my_server 'nohup sh -c "( ( echo this is my_server; jabref_exe=`which jabref`; jabref_dir=`dirname $jabref_exe`; java -jar $jabref_dir/../jabref.jar` $1 &/dev/null ) & )"' &
jabref_dir: Undefined variable.
That way, whenever jabref gets updated to a new version on the server, I won't have to manually update the path to the jar file. The jabref executable doesn't take arguments, but launching it with java -jar does, which is why I have to juggle the path a bit.
At the moment I have the list of commands in a separate script file and call
ssh my_server 'nohup sh -c "( ( my_script.sh &/dev/null ) & )"' &
which works, but since the ssh call is already inside one script file it would be nice to have everything together.
In this example
ssh my_server "echo this is my_server; abc=2;"
abc is set on the remote side, so it should be clear why it is not set on your local machine.
In the next example,
ssh my_server "echo this is my_server; abc=2; echo abc is $abc"
your local shell tries to expand $abc in the argument before it is ever sent to the remote host. A slight modification would work as you expected:
ssh my_server 'echo this is my_server; abc=2; echo abc is $abc'
The single quotes prevent your local shell from trying to expand $abc, and so the literal text makes it to the remote host.
To finally address your real question, try this:
jabref_dir=$( ssh my_server 'jabref_exe=$(which jabref); jabref_dir=$(dirname $jabref_exe);
java -jar $jabref_dir/../jabref.jar > /dev/null; echo $jabref_dir' )
This will run the quoted string as a command on your remote server, and output exactly one string: $jabref_dir. That string is captured and stored in a variable on your local host.
With some inspiration from chepner, I now have a solution that works, but only when called from a bash shell or bash script. It doesn't work from tcsh.
ssh my_server "bash -c 'echo this is \$HOSTNAME; abc=2; echo abc is \$abc;'"
Based on this, the code below is a local script which runs jabref on a remote server (although with X-forwarding by default and passwordless authentication the user can't tell it's remote):
#!/bin/bash
if [ -f "$1" ]
then
fname_start=$(echo ${1:0:4})
if [ "$fname_start" = "/tmp" ]
then
scp $1 my_server:$1
ssh my_server "bash -c 'source load_module jdk; source load_module jabref; java_exe=\$(which java); jabref_exe=\$(which jabref); jabref_dir=\$(echo \${jabref_exe%/bin/jabref});eval \$(java -jar \$jabref_dir/jabref.jar $1)'" &
else
echo input argument must be a file in /tmp.
else
echo this function requires 1 argument
fi
and this is the 1-line script load_module, since modulecmd sets environment variables and I couldn't figure out how to do that without sourcing a script.
eval `/path/to/modulecmd bash load $1`;
I also looked at heredocs, inspired by How to use SSH to run a shell script on a remote machine? and http://tldp.org/LDP/abs/html/here-docs.html. The nice part is that it works even from tcsh. I got this working from the command line, but not inside a script. That's probably easy enough to fix, but I've got a solution now so I'm happy :-)
ssh my_server 'bash -s' << EOF
echo this is \$HOSTNAME; abc=2; echo abc is \$abc;
EOF

Resources