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.
# 1  
Old 02-18-2018
Ssh to validate multiple remote hosts connection validation.

Dear Folks,


I am trying to read a config file contains ip and port numbers.
i want to read each line of the config file and check ssh connection is happening or not.

Kindly guide.

Config file:
Code:
abc@1.2.342        22
abc@1.2.343        22
abc@1.2.344        22
abc@1.2.345        22
abc@1.2.3.116       22
abc@1.2.3.117       22
abc@1.2.3.118       22
abc@1.2.3.119       22
abc@1.2.342        2222
abc@1.2.343        2222
abc@1.2.344        2222
abc@1.2.345        2222
abc@1.2.3.116       2222
abc@1.2.3.117       2222
abc@1.2.3.118       2222
abc@1.2.3.119       2222

Code:
#!/bin/sh
while read -r lines || [[ -n $lines ]] ;
do
        hosts=`echo $lines | awk '{print $1}'`
        port=`echo $lines | awk '{print $2}'`
        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

Kindly help, it is checking only for one ip, rest is not passing as an argument.
Regards,
sadique

Last edited by Don Cragun; 02-18-2018 at 05:36 AM.. Reason: Remove "CODE : " in CODE tags.
# 2  
Old 02-18-2018
What happens if you try the following script instead?
Code:
#!/bin/sh
set -xv
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

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-18-2018
For half of your configuration entries you don't need the ssh test as they don't comply to the "dotted quad" notation. And, as already implied by Don Cragun's proposal, the || [[ -n $lines ]] is not necessary because it would be executed only if the read failed anyhow (e.g. at enf of file), terminating the while loop.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-19-2018
It is not working,
not reading all the ips and ports.
Kindly guide.
# 5  
Old 02-19-2018
What is not working?
We need the output to understand...
also Looking at don's code I cant believe it failed reading a line, to convince yourself, add a counter in the loop as you know what is expected based on the config file given in input
# 6  
Old 02-19-2018
Show us the exact output my script did produce (in CODE tags).

I turned on tracing in that script so we can see what is going on.

If you're not willing to show us the output it produced, there is no reason for us to keep this thread open.
# 7  
Old 02-19-2018
Anything special about the input file? Post a hexdump of a small but representative part of it.
This User Gave Thanks to RudiC For This Post:
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