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

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

Related

How to write bash commands inside SFTP [duplicate]

This question already has answers here:
Using variables inside a bash heredoc
(3 answers)
Closed last year.
I have the below script to download today's file from the server
#!/bin/sh
IFS='
'
SYS_DT=$(date '+%d%h%Y')
SYS_FILE='BOMExtract_'$SYS_DT'.xlsx'
sshpass -p "123" sftp "admin#XXXX" << 'EOF'
cd /u01/admin/Oracle
lcd /u01/usr
get $SYS_FILE
But it is not taking the value of SYS_FILE in getting command. Can anyone please help to write bash inside sftp commands?
I removed the quotes around EOF and that fixed the issue.Thanks Barmar

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?

Shell program behaves differently when read from a file vs. pipe [duplicate]

This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 2 years ago.
I made a bash script for my personal usage which sets up selenium webdriver with appropriate options . Here is its raw link - https://del.dog/raw/edivamubos
If i execute this script using curl after writing it to a file first like..
curl https://del.dog/raw/edivamubos -o test.sh && \
chmod u+x test.sh && \
bash test.sh
The script works perfectly as its intended to work
But usually i like to execute scripts directly using curl , so when i do..
curl https://del.dog/raw/edivamubos | bash
The script works very weirdly , it keeps repeating line 22,23 and 29 infinitely on loop. i couldnt beleive it as first so i tested this 3,4 times and can confirm it.
Now
what is the reason for same script acting differently in both cases ?
How do i fix it ( ie make it work correctly even after executing it directly without writing to a file )
Edit -
If someone want they can quickly test this in google colab ( in case someone intending to test but don't want to install any packages locally ) . I am mentioning this thing because you won't be able to reproduce this properly in any bash IDE.
When you pipe the script to bash, this command (line 24):
read -p "Enter your input : " input
reads the next line (i.e. line 25, case $input in) because bash's stdin is connected to curl's stdout, and read reads from the same descriptor as bash.
To avoid that, the developer can change the script so that all input is read from /dev/tty (i.e. the controlling terminal). E.g.:
read -p 'prompt' input </dev/tty
Or the user can use one of the below, so that read reads from the terminal, not the descriptor it was read from.
bash -c "$(curl link)"
bash <(curl link)

Insert data in bash prompt with script [duplicate]

This question already has answers here:
Read user input inside a loop
(6 answers)
Kerberos kinit enter password without prompt [closed]
(6 answers)
Closed 5 years ago.
I created a bash script that perform some operations inside a while loop.
In one command, the console asks for an input and I have no idea how to provide this input in order to continue with my script.
while read line;
do
string_array=($line)
username=${string_array[0]}
password=${string_array[1]}
kinit $username
===> here I need to enter the $password and press "ENTER" to continue
done <myfile
Any suggestion?
Change your main while-loop to read from a different file descriptor than stdin and use read to read from stdin and use -s to suppress text from showing up in console.
while read -u 3 line; do
# Your rest of the code
read -s -p "Enter password: " password
done 3<myfile

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

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.

Resources