Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a hostlist and there are several hosts. I want to use ssh to connect to them. I want to see whether the host is available or not by the time length of ssh takes. If it take long than 5s (which means it is not available). Then stop it, and ssh to next host. Once a host is available, then output the name of host. I previously use nmap, but the IT security told it is not allowed to install it on the desktop on campus.
Code:
host_list="/home/campus27/zwang10/Desktop/cluster/program/hostlist"
HOSTS=`cat $host_list`
for line in $HOSTS
do
timeout -5s `ssh $line`
done
Just need to point out that you only REALLY need to check if port 22 is accepting connections at that address. For that your could use nc (netcat) or even telnet. There is also a bash trick that uses a virtual device file for shell network activity. Most likely the telnet thing is easiest. I seem to remember solving a problem somewhat like that in perl once, which may apply here.
There are undoubtedly other and arguably better ways. The optimal choice for you may depend upon the software set that is acceptable in your environment. I cannot determine that from your initial post.
The important point is: do not limit yourself to using ssh. Find another tool, one that is readily available to you, that will help you solve that problem.
I have a hostlist and there are several hosts. I want to use ssh to connect to them. I want to see whether the host is available or not by the time length of ssh takes. If it take long than 5s (which means it is not available). Then stop it, and ssh to next host. Once a host is available, then output the name of host. I previously use nmap, but the IT security told it is not allowed to install it on the desktop on campus.
Code:
host_list="/home/campus27/zwang10/Desktop/cluster/program/hostlist"
HOSTS=`cat $host_list`
for line in $HOSTS
do
timeout -5s `ssh $line`
done
The above script is all I can do.
Instead of making a session with ssh, why not use "ssh $line echo 'Ok'". That way you will get back an "Ok" message if things are working (and exit status 0) --- and if your timeout occurs, things are NOT working (exit status 124).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.