Ssh -q -o BatchMode=yes -o StrictHostKe圜hecking=no -o ConnectTimeout=$connect_timeout $server 'exit 0' We can use this command in our existing shell script for automation purpose: # cat /tmp/check_connectivity.sh Here we have defined a connection timeout period of 5 second which you can change based on your environmentĪLSO READ: Linux sftp restrict user to specific directory | setup sftp chroot jail Shell Script Example To check if nmap-ncat is installed on your server # rpm -q nmap-ncat nc and ncat is provided by nmap-ncat rpm.We can also use nc and ncat utility to check port status from target hosts and test SSH connection.In my earlier article I had shared the steps to use nc and ncat to transfer files between Linux server.Method 3: Use netcat or nc to test SSH connection Status=`nmap $server -Pn -p $port | egrep -io 'open|closed|filtered'`Įcho "SSH Connection to $server over port $port is possible"Įcho "SSH Connection to $server over port $port is possible but blocked by firewall"Įcho "SSH connection to $server over port $port is not possible"Įcho "Unable to get port $port status from $server" We can use this command in our shell script to test SSH connection over port 22 ~]# cat /tmp/check_connectivity.sh Here server2 is my target host and we are looking for nmap port status ~]# nmap server2 -Pn -p ssh | egrep -io ~]# nmap server2 -Pn -p ssh | egrep -io 'open|closed|filtered'ĪLSO READ: Add timestamp to SSHD DEBUG logs Shell script Example Open means that an application on the target machine is listening for connections/packets on that portĬlosed ports have no application listening on them, though they could open up at any timeįiltered means that a firewall, filter, or other network obstacle is blocking the port so that Nmap cannot tell whether it is open or closed. Here, -Pn Treat all hosts as online - skip host discovery Timeout $connect_timeout bash -c " -PN -p ssh | egrep 'open|closed|filtered' We can use this tool in a shell script to test SSH connection over port 22 # cat /tmp/check_connectivity.sh ALSO READ: PSSH - 10 practical examples to use parallel SSH in Linux Shell Script Example
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |