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 &'
Related
Can anyone explain, please, why this command echoes an empty line instead of "abc"? I'm stuck with this. I know there are multiple ways of reaching the same goal but please also explain why this command does not print "abc" and why it's wrong.
ssh 127.0.0.1 bash -c "echo abc"
You effectively lose a level of quoting when you execute commands via ssh. You would need to write instead:
ssh 127.0.0.1 'bash -c "echo abc"'
Without those outside quotes, the command you're running on the remote system is:
bash -c echo abc
Here you're running the command echo, with $0 set to abc.
My expect script
#!/usr/bin/expect -f
#I tried replacing sh - with bash -s still no positive results
spawn ssh xxxx#yyyy "sh -" < test.sh
expect "password: "
send "zzzzz\r"
expect "$ "
This command works well if executed in the terminal
ssh xxxx#yyyy "sh -" < test.sh
But if I execute it via expect script; it fails.
This is the output if I execute it via the expect script. May I know where I am going wrong
bash: test.sh: No such file or directory
P.S : Yes, the file exists and the credentials are right.
Expect script was unable to read the contents of the file, that was the issue. Solved it by reading the contents of the file and passing that variable instead of the file name,
set fh [open test.sh r]
set contents [read $fh]
close $fh
and replacing the sh- with bash -c '$contents'
Thank you everyone for the valuable comments.
I am new to ssh so forgive me if my questions are trivial..i need to make a a remote computer execute a set of commands several times so i was thinking about making a loop using ssh ..the problem is i don't know do i save those commands in a file and loop on that file or can i like save them in ssh and just call them ..i am really troubled..also if i make a loop like this
i= 10
while i!= 0
execute command.text file ???
i--
How to i tell it to execute the file ?
Just try first on the shell in the remote machine to run the command you want.
You will find plenty of info over the internet about loops in shell/bash/csh/whatevershell:
For instance assuming bash run in the remote host (from: http://www.bashoneliners.com/ )
$ for ((i=1; i<=10; ++i)); do echo $i; done
Once you learn that, simply then take that statement to the ssh command from the machine you want to trigger the action:
$ ssh user#remotehost 'for ((i=1; i<=10; ++i)); do echo $i; done'
You can write a simple script that will execute needed commands, and path it to ssh.
For example:
script.sh, it will iterate over your bunch of commands 10 times:
for i in $(seq 10)
do
command1
command2
command3
done
and path it to remote server for execution:
$ ssh $SERVERNAME < script.sh
If you have this command.text file in which you have written all the commands in column (you can modify them with vi or vim and put them in column), you don't even need to do a loop, you can simply do:
cat command.text | awk '{print "ssh user#remotehost "$0" "}' | sh -x
For example if command.text contains:
ls -lart
cd /tmp
uname -a
This will let you do all commands written in the command.text by doing ssh user#remotehost.
I'm trying to use a ssh command like :
ssh user#host command -m MYFILE
MYFILE is the content of a file on my local directory.
I'm using Bash. I've tried to use printf "%q", but i'd not working. MYFILE contains spaces, new lines, single and doublequotes...
Is there a way my command gets the file content ? I can't actually run anything else than command on the remote host.
How about first transferring the file to the remote machine
scp MYFILE user#host:myfile &&
ssh user#host 'command -m "$(< myfile)" && rm myfile'
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.