Ssh script to validate ssh connection to multiple serves with status

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Ssh script to validate ssh connection to multiple serves with status
# 1  
Old 06-03-2018
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
Code:
#/bin/bash
for host in $(cat servers.txt); do
        ssh "$host" "$HOSTNAME" > "test.log";
done

its coming as error below...

Code:
./ssh.sh: line 5: syntax error near unexpected token `do'
./ssh.sh: line 5: `do ssh "$host" "$HOSTNAME" > "output.$host";'

---------- Post updated at 06:25 PM ---------- Previous update was at 06:13 PM ----------

I don;t have knowledge on scripting much... so just checking if anyone come across same and have code ..

Last edited by rbatte1; 06-04-2018 at 08:28 AM.. Reason: added code tags
# 2  
Old 06-04-2018
I do not see a syntax error, but you should correct the shebang (1st line) to
Code:
#!/bin/bash

Maybe you are on Solaris and failing to run /bin/bash falls back to /bin/sh that does not understand $( )
There is a second problem,
> "test.log"
overwrites the file each time.
Either change to
>> "test.log"
Or move it to the "done" as follows
Code:
...
done > "test.log"

that is more efficient because it opens+creates the file once (when the loop starts).
Or
Code:
...
done >> "test.log"

That opens+appends it once.
# 3  
Old 06-04-2018
Welcome to the forum.

That error message for line 5 doesn't match your posted code that has four lines only, "test.log" is not "output.$host", and are you sure what the HOSTNAME variable means? So please post the entire code along with a few lines of servers.txt
# 4  
Old 06-04-2018
Depending how many you are poking, you might consider these adjustments too:-
Code:
#!/bin/bash
for host in $(cat servers.txt)
do
        ssh -q -o ConnectTimeout=3 "$host" "hostname"
done > test.log

The blue -q is to suppress some of the normal output to keep your log file a little clearer, however the log file will probably still collect anything that is displaed by the server login process e.g. from /etc/profile or ~/.bashrc

I've added the green to speed up the timeout if the server does not respond. You might want to adjust it depending on what your network will allow.

The red hostname is a command to run on the server you are connecting to. You have $HOSTNAME which may be undefined or set to anything in the shell's environment prior to this script.


What output do you actually want? Is it a list of hosts you could not connect to? If you can open the IP connection, but if fails to sign in how would you handle that? You script would probably just hang. Other conditions may cause your script to carry on to the next host immediately, e.g. the server's public key is different and you get an alert about a possible man-in-the-middle attack. The ssh exits with a non-zero return code, but you don't check. Perhaps you just want a list of successes to then target those that don't let you connect. Knowing the goal would be useful for us.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. BSD

Connection SSH to remote by ssh

Hello guys! I am setting up a script to access a unix remote server. My problem is that when I put the ssh line "my host", the script does not wait for the server response asking for the password to execute the line in which I put the password, that is, I need to put a form in which script has a... (1 Reply)
Discussion started by: aroucasp
1 Replies

2. Shell Programming and Scripting

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: abc@1.2.342 22 abc@1.2.343 22 abc@1.2.344 22 abc@1.2.345 22... (9 Replies)
Discussion started by: sadique.manzar
9 Replies

3. UNIX for Dummies Questions & Answers

Script that tries to connect via SSH till connection is up

Hey, I need a script that tries to connect via SSH to a remote server and that remote server might not be up yet, so retry until succeed the error message I get if the server is not up yet is: ssh: connect to host 127.0.0.1 port 40001: Connection refused any idea of a good way to do it ? ... (5 Replies)
Discussion started by: OdedOvdat
5 Replies

4. Shell Programming and Scripting

How to test for the ssh exit status in script?

Hello; I regularly run monitoring scripts over ssh to monitoring scripts But whenever a server is hung or in maintenance mode, my script hangs.. Are there anyways to trap exit status and be on my way ?? Looked at the ssh manpage and all I can see is a "-q" option for quiet mode .. Thank... (2 Replies)
Discussion started by: delphys
2 Replies

5. Shell Programming and Scripting

Mysql command after a SSH connection (Script)

Hi all, Im new at scripting and i need to run a few commands at work every hours so i decide to make a script but on 1 of the steps i have a the follwoing problem: The command i do is this: #!/bin/bash ssh root@asdasd001 'mysql -h A-db-1 -uroot -password --execute "show slave status"'... (3 Replies)
Discussion started by: Aparicio
3 Replies

6. Shell Programming and Scripting

SSH Connection drops - but does my script keep running?

Hello there. I'm fairly new to Linux, but I am connecting via SSH and PuTTY to a remote server, and I am running a fairly heavy MySQL script in a PHP page. Our connection here is dodgy to say the least and I get continuous disconnections. My question is, when I get disconnected, does my... (4 Replies)
Discussion started by: christatedavies
4 Replies

7. Shell Programming and Scripting

ssh connection script

Hi all, I'm writing a script that chooses the best computer available in an open lab. The script works great except every now and then there is a dead computer in the lab that begins the ssh handshaking, but freezes after the following: debug1: Offering public key: When the script happens... (2 Replies)
Discussion started by: x-375HK-x
2 Replies

8. 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

9. Shell Programming and Scripting

Testing ssh connection from KSH script

Hi. I have a kornshell script that runs on a daily basis as a cron job. Part of what the script does is copy the folder contents from another server to the current server (server where KSH script is running). I have a scp command, as follows: scp $REMOTE_HOST:$REMOTE_FILE_DIR/* $TMP_DIR ... (8 Replies)
Discussion started by: dmilks
8 Replies
Login or Register to Ask a Question