Executing ssh command in a bash shell script within a loop [duplicate] - bash

This question already has answers here:
in my bash loop over a list of some servers, if the ssh connects the bash script exits
(2 answers)
Closed 8 years ago.
I'm trying to execute an ssh command within a a bash shell script that should perform the following:
1) ssh to host
2) execute command
3) print value of command
4) repeat steps 1 -3
5) exit bash shell script
I have set up password less entry to the remote host, added host key to remote host
I want to test the various states of the httpd process running on the remote host
Within a text file, httpd_process.txt, I have:
/etc/init.d/httpd status (stop, start, restart)
I do the following in the script:
while read LINE
do
echo "Httpd Request: $LINE"
status=`$LINE`
echo "Status: $status"
sleep 5 # sleep so that next
done < /path_name/httpd_process.txt
exit 0
I assumed that each time through the loop another input string is read from the input text file and the request is made to the remote host.
However, what I experience is that after the first request the script terminates.
Am I correct to assume that as the first request is sent it creates a child process and once that process completes my script completes and the next turn through the loop is not executed?

ssh is consuming stdin. Pass it -n to prevent this.

Related

How to wait for a command to get success in bash even when we have set -e in the bash script [duplicate]

This question already has answers here:
How to undo the effect of "set -e" which makes bash exit immediately if any command fails?
(3 answers)
Running a bash function with set -e without exiting the shell
(3 answers)
Closed 6 months ago.
Below is my shell script, I am trying to connect to 9801 port and I am waiting using while loop until this got connected. But due to "set -e" line, when it is trying to connect to port 9801 and got connection refused it is coming out of the shell script even when it is inside while loop. When I remove the "set -e" line, the shell script is not exiting and waiting inside the loop till the port gets connected. But I need to have "set -e" for remaining part of the same script.
#!/bin/bash
set -e
while [ true ]
do
nc localhost 9801 -zv
if [ $? -eq 0 ]; then
break
fi
done
Behavior I am seeing with the above script:
root#user ~# ./test.sh
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connection to ::1 failed: Connection refused.
Ncat: Trying next address...
Ncat: Connection refused.
I need this shell script need not exit and should wait until it gets connected and also I need to have "set -e" for remaining part of the same script. Hoping that anyone helps me with this. Thanks in advance!

How to automate OPENVPN login [duplicate]

This question already has answers here:
Send string to stdin
(6 answers)
Closed 2 years ago.
I created a very simple bash script. The first line of the script after #!/bin/bash accesses my vpn service using an OPENVPN file provided by the VPN Vendor. It works as expected and then waits for data entry of my username and once entered expects my password.
I used the echo command to provide the required responses but they never occur. Open VPN just sits there waiting for my username. If I hit enter twice then OPENVPN terminates and my script completes by echoing the username and password.
#!/bin/bash
sudo openvpn /etc/openvpn/us-dtw.prod.xxxxxxx.com_udp.ovpn
echo $'\r'
printf " xf3Z3ZY6xxxxxxxxEuRDmh"
echo $'\r'
echo " nvn7B5kxxxxhxxJstRU"
What could be causing my script to hang and not execute the echo commands until OPENVPN terminates?
Maybe there is a way to pipe the text but I am really new to this.
Found an example that works correctly to automate the VPN logon process: https://help.anonine.com/support/solutions/articles/5000613671-how-do-i-save-my-username-password-in-openvpn-for-automatic-login-
openvpn will by default be using the TTY to get input, not standard input.
Have you looked at the --askpass option to openvpn?

Log in to another server and run commands - Using a script [duplicate]

This question already has answers here:
What is the cleanest way to ssh and run multiple commands in Bash?
(14 answers)
bash script execute commands after ssh
(1 answer)
How to use SSH to run a local shell script on a remote machine?
(23 answers)
When I run a bash script that ssh's into a remote server and run a command like wget it saves on the source not the destination server?
(3 answers)
Closed 3 years ago.
I want to run a script a.sh that will do the following steps -
Run few commands
Call another shell script b.sh in the same server - server01
b.sh will login to another server - server02 and run the commands that follow.
I am able to do till step 2 correctly. In step 3, I am able to loin to another server but it stops there. It does not run the steps that follow.
Have a look at the two scripts.
a.sh
cd ~/sample/home && python helloworld.py
#!/bin/bash
cd ~/sample/ && ./b.sh
b.sh
ssh username#server02.com
echo "In server02"
Both a.sh and b.sh are in the same server, that is server01.com. Here, I want a.sh to run in server01 and then b.sh to run in server01. Once b.sh runs, it should do ssh to server02 and print "In server02".
I am able to do till ssh to server02. After that it is not printing "In server02" in server02.
Is there a way to do it?
Use a heredoc.
ssh username#server02.com << EOF
echo "In server02"
EOF

Bash Script Quits After Exiting SSH

I'm trying to write a Bash script that logs into 2 different linux based power-strips (Ubiquiti Mpower Pros) and turns 2 different lights off (one on each strip). To do this I login to the 1st strip, change the appropriate file to 0 (thus turning off the light), and exit, repeating the same process on the next power-strip. However, after I exit the first SSH connection, the script stops working. Could someone please suggest a fix? My only idea would be to encase this script in a python program. Here's my code:
#!/bin/bash
ssh User#192.168.0.100
echo "0" > /proc/power/relay1
exit
# hits the enter key
cat <(echo "") | <command>
ssh User#192.168.0.103
echo "logged in"
echo "0" > /proc/power/relay1
exit
cat <(echo "") | <command>
ssh as an app BLOCKS while it's running, the echo and exit are executed by the local shell, not by the remote machine. so you are doing:
ssh to remote machine
exit remote shell
echo locally
exit locally
and boom, your script is dead. If that echo/exit is supposed to be run on the remote system, then you should be doing:
ssh user#host command
^^^^^---executed on the remote machine
e.g.
ssh foo#bar 'echo ... ; exit'
The commands you're apparently trying to run through ssh are actually being executed locally. You can just pass the command you want to run to ssh and it will do it (without needing an explicit exit)
ssh User#192.168.0.110 'echo "0" > /proc/power/relay1'
will do that, and similar for the other ssh command

SSH while read line loop without using remote commands [duplicate]

This question already has answers here:
Looping through lines in a file in bash, without using stdin
(3 answers)
Execute a command on remote hosts via ssh from inside a bash script
(4 answers)
Closed 8 years ago.
I have a list of IP addresses I need to ssh into, making unique changes to each one. I tried doing
while read -r line; do
ssh -n $line; done < file
but I need to manually run commands and checks on each device. The second I login it kicks me out to the next one. Are there any read line or ssh options that can allow me to do this?
Tell read to use a different FD, then you can remove the -n from ssh.
while read -u 3 ...
do
...
done 3< file

Resources