for loop is not running after ssh connection - bash

I am trying to write a script which will do ssh to few hosts (via for loop) and run few commands on remote host with help of for another for loop.
I have written something like below but second for loop is not returning anything after doing ssh.
PS: ssh set-up has been done so no issues while doing ssh. Issue is with second for loop.
for ((i=11; i <= 16; i++))
do
echo $i
ssh abc$i.uk.com << EOF
cd /home
for j in `ls -1`
do
ls -lrt $j/*
done
EOF
done

Related

Remote SSH commands with for loop

I'm running running ssh commands from client machine on a remote machine:
ssh -o "StrictHostKeyChecking no" -i $key 'user'#$public_ip "
i=1;
workerips: "ip1 ip2";
for ip in $worker_ips; do
echo \"ipis: ${ip} and i is ${i}\"
done;"
My problem is that '$ip' and '$i' are empty inside the 'for' loop. '$i' keeps its value outside the for loop. What am I doing wrong?
It's a lot easier to do this sort of thing with a heredoc:
$ ssh -o "StrictHostKeyChecking no" -i "$key" user#$public_ip << \EOF
> i=1
> workerips="ip1 ip2"
> for ip in $workerips; do echo "ipis: ${ip} and i is ${i}"
> done
> EOF
Depending on what you are trying to do, parallel ssh program (pssh) could work for you.
https://linux.die.net/man/1/pssh
The idea is you can execute the same command across the nodes you define (either across the board or in batches). Basic workloads might be updating a config file across several nodes, more complex cases include things like snapshots across a Cassandra cluster to make sure you have these run at nearly the same time.

for loop in remotely executed bash script does not work

I have a remote bash script, which goes through files using a for loop:
#!/bin/bash
for f in *.pdf;
do
echo $f
done
When executed locally:
server$./test.sh
1508.01585.pdf
1605.07683.pdf
When executed through ssh from a remote client:
client$ ssh user#server 'bash -c ~/papers/test.sh'
*.pdf
I have tried using ssh -T, bash -s, and nothing seems to work.
Please help!

Shell Script - While Loop / File Reading not working

I have a requirement which should address following points.
I have a file which contains list of IP addresses,I want to read line by line.
For each IP I need to push following commands using SSH (all are Mikrotik devices)
/ radius add service=login address=172.16.0.1 secret=aaaa
/ user aaa set use-radius=yes
Following is my code.
#!/bin/bash
filename="branch"
while IFS= read line; do
echo ${line//}
line1=${line//}
ok='#'
line3=$ok$line1
sshpass -p abc123 ssh -o StrictHostKeyChecking=no admin$line3 / radius add service=login address=172.16.0.1 secret=aaaa
sleep 3
sshpass -p abc123 ssh -o StrictHostKeyChecking=no admin$line3 / user aaa set use-radius=yes
sleep 3
echo $line3
echo $line
done <"$filename"
Branch text file:
192.168.100.1
192.168.101.2
192.168.200.1
Issue: What ever the changes I am doing While loop is only run once.
Troubleshooting/Observations:
Without the SSH command if I run the While loop to read the file " branch " it work fine.
The problem is that a program in the loop also reads data on standard input. This will consume the 2nd and subsequent lines of what's in "$filename".
On the next iteration of the loop, there's nothing left to read and the loop terminates.
The solution is to identify the command reading stdin, probably sshpass and change it to leave stdin alone. The answer by Cyrus shows one way to do that for ssh. If that doesn't work, try
sshpass [options and arguments here] < /dev/null
Another solution is to replace the while with a for loop. This works as long as the branch file only contains IP addresses:
for ip in $(cat branch); do
echo $ip
...
sshpass ...
done

how to ssh a loop over several commands

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.

How can I trigger multiple remote shell scripts in bash to run concurrently and then wait for them to complete?

I have a bash script which runs a number of tasks on remote machines via SSH. Each script is quite long so I'd like to run them concurrently as background tasks. I also need for them all to complete before moving on. I know that I can use the wait command for the latter but when I stick & at the end to make it background task, it all stops working.
By stop working, I mean that the scripts don't seem to have run, but otherwise the main script still completes.
ssh root#machine1 'bash -s' < script1 my_parameter
ssh root#machine2 'bash -s' < script2 my_parameter
ssh root#machine3 'bash -s' < script3 my_parameter
wait
some_other_task
This works fine for me with Ubuntu 11.04:
#!/bin/bash
ssh user#server1 <command.sh &
ssh user#server2 <command.sh &
ssh user#server3 <command.sh &
wait
echo end
Content of command.sh:
sleep 5
hostname
Update:
With commandline arguments it gets more complicated. You can use a "here-document" and set to set $1 $2 $3.
#!/bin/bash
ssh -T root#server1 << EOF
#!/bin/bash
set -- $my_parameter1 $my_parameter2 $my_parameter3
$(cat script1)
EOF
ssh -T root#server2 << EOF
#!/bin/bash
set -- $my_parameter1 $my_parameter2 $my_parameter3
$(cat script2)
EOF
ssh -T root#server3 << EOF
#!/bin/bash
set -- $my_parameter1 $my_parameter2 $my_parameter3
$(cat script3)
EOF
wait
echo end

Resources