Ssh to validate multiple remote hosts connection validation.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ssh to validate multiple remote hosts connection validation.
# 8  
Old 02-19-2018
Hmmm - we overlooked the obvious - the first ssh is eating up the rest of the input file. Try adding the -n option...
This User Gave Thanks to RudiC For This Post:
# 9  
Old 02-19-2018
Here is the output.

Code:
while read -r hosts port
do
        ssh -q -o "BatchMode=yes" -p "$port" "$hosts" "echo 2>&1"
        [ $? -eq 0 ] && echo "Connected to ${hosts} for port ${port}" || echo "Connection refused to ${hosts} for port ${port}, Please check"
done < /home/ssh_configfile
+ read -r hosts port
+ ssh -q -o BatchMode=yes -p 22 abc@1.2.3.42 'echo 2>&1'

+ '[' 0 -eq 0 ']'
+ echo 'Connected to abc@1.2.3.42 for port 22'
Connected to abc@1.2.3.42 for port 22
+ read -r hosts port


i have multiple server and its reading only 1st ip for rest it is not reading and checking the connection.

If anyone who can be humble and kind enough to guide me on the right direction.


Regards,
Sadique

---------- Post updated at 06:06 AM ---------- Previous update was at 06:01 AM ----------

Thanks Rudic your observation worked for me now with my original code i am able to connect to all the ip with their host and validate the connection.
Also as u suggested i have removed the
Code:
|| [[ -n $lines ]] ;

Thank you for the help everyone.
# 10  
Old 02-19-2018
It would seem that the ssh inside your loop may be consuming all the other input lines.

Perhaps a small adjustment like this might help:-

Code:
exec 99</home/ssh_configfile

while read -u 99 -r host port
do
        ssh -q -o "BatchMode=yes" -p "$port" "$hosts" "echo 2>&1"
        [ $? -eq 0 ] && echo "Connected to ${hosts} for port ${port}" || echo "Connection refused to ${hosts} for port ${port}, Please check"
done


Does that help? It is forcing the loop to read from a nominated file descriptor leaving ssh to read STDIN if it wishes.


Can you show us the output (with a set -x before this bit) if it is still stuck? Perhaps just a two line input file would suffice to keep the output small.


Regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk remote multiple hosts print remote hostname and output

Hi all, i'm trying to gether multiple pattern on remote hosts, and trying to print hostname and the pattern, ssh remoteserver1 -C 'hostname 2>&1;cat /var/log/server1.log | awk -F ";" '"'"'{ print " "$2" "$5}'"'"'| sort | uniq -c | sort -g -r ' The output is the following, remoteserver1 ... (8 Replies)
Discussion started by: charli1
8 Replies

2. Shell Programming and Scripting

Grep remote multiple hosts output to local server

Hello all, i'm trying to create a report by greping a pattern on multiple remote hosts and creta a simple report, actually i did this, is ther any better way to do this. #!/bin/bash for host in `cat RemoteHosts` do ssh $host -C 'hostname 2>&1; grep ERROR /var/log/WebServer.log.2019-09-21... (0 Replies)
Discussion started by: charli1
0 Replies

3. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

4. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

5. UNIX for Dummies Questions & Answers

How to search using ssh on multiple hosts?

Hi guys - I am having a hard time trying to figure how to search for a certain string on config files hosted on multiple hosts. This is an example: Hostnames: myhost1.mycompany.com|myhost2.mycompany.com|myhost3.mycompany.com String to search for: myipaddress.somehost.com Directory... (9 Replies)
Discussion started by: DallasT
9 Replies

6. Shell Programming and Scripting

SSH to remote hosts in shell scripting

Hi There, I have a file contaning some 100 servers names one by one the file called redhat_servers.txt I want to prepare a script where it should give me the host name and kernal version. I wrote like this, #!/bin/bash while read line do ssh $line "uname -nr" done <... (3 Replies)
Discussion started by: kumar85shiv
3 Replies

7. Shell Programming and Scripting

Running a script on multiple remote hosts at once

I have a script on about 15 hosts that I need to run for each host whenever I want (not crontab). Problem is, this script takes 5-10 mins to run for each host. Is there a way I can run the script in parallel for all the hosts instead of 1 at a time? Also, I'm remotely running the script on the... (3 Replies)
Discussion started by: mrskittles99
3 Replies

8. Shell Programming and Scripting

Logon to multiple hosts using ssh hardcode password

Hi im trying to write a script to logon to list of servers with same userID. I have no option/plan to implement ssh-keygen sharing between the systems, so i have written script creating 2 files, file1 holds list of hosts host1 host2 host3 file2 has following script for i in `cat file1`... (1 Reply)
Discussion started by: dreamaix
1 Replies

9. Shell Programming and Scripting

Remote SSH Connection Using Script

Hi, I am new to Shell Scripting. Can anybody help me in writing a Script Which Could Login from a Unix box to a Remote Unix box which accepts the user credentials automatically and display the result for checking the Disk Space Utilisation (Without running any SSH agent). (1 Reply)
Discussion started by: ajith_tg
1 Replies

10. Shell Programming and Scripting

Remote Connection (SSH)

Hello all, I connect usually to one enviornment "dev" daily and then ftp some files to some other enviorment "uat" and then login to "uat" and run some scripts to process these files. I was thinking to automate the process, where running one script from "dev" will complete all task required... (11 Replies)
Discussion started by: RishiPahuja
11 Replies
Login or Register to Ask a Question