LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problems writing script to turn off remote computers using ssh (https://www.linuxquestions.org/questions/linux-newbie-8/problems-writing-script-to-turn-off-remote-computers-using-ssh-4175413203/)

abhi6691 06-24-2012 11:30 PM

problems writing script to turn off remote computers using ssh
 
Hello,

I have been trying to write a script in linux(Red Hat) to turn off remote computers using ssh. The problem I am facing right now is to determine which computers are already powered off. I was thinking of using the fping utility but realised that it's not available by default, so now I am lefr with the option of PING and i have no idea how to do that in a script, I mean how to parse the output of ping and use it to my benefit.

Thanks in advance for your help.

rmugunthan 06-25-2012 12:19 AM

Hi,

you can use like below, instead of echo use whatever you want.

ping -c 1 -W 2 $1 > /dev/null
value=`echo $?`
if [ "$value" -eq 0 ] ; then
echo "Server is up"
else
echo "server is down"
fi

execute the script with the input of server ip address or hostname.

John VV 06-25-2012 02:38 AM

something like that sounds a bit like it might be used for
shall we say -- not so good of a purpose ...


ping might not not work
iptables normally is set to be invisible
a ping might not be returned even if the machine is on

pan64 06-25-2012 07:00 AM

probably you can try nmap and look for ssh port.

lithos 06-25-2012 08:45 AM

NMAP would be more accurate if used:
Code:

nmap -p22 IP_address_of_host  (or hostname/domain)


$ nmap -p22 example.com

Starting Nmap 5.61TEST2 ( http://nmap.org ) at 2012-06-25 15:38 CEST
Nmap scan report for example.com (1.5.3.13)
Host is up (0.014s latency).
PORT  STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.18 seconds



then if you want to know if it's opened, just "grep" output:
$ nmap -p22 example.com | grep open
22/tcp open  ssh


abhi6691 06-25-2012 10:15 AM

Quote:

Originally Posted by John VV (Post 4711076)
something like that sounds a bit like it might be used for
shall we say -- not so good of a purpose ...


ping might not not work
iptables normally is set to be invisible
a ping might not be returned even if the machine is on

i am working on a project on remote administration for class room environment

John VV 06-25-2012 04:04 PM

well RHEL is set up to have all ports hidden
that is the default setting in iptables
nmap should then NOT receive a replay one way or the other

all ports should look like there is no computer there.

suicidaleggroll 06-25-2012 04:11 PM

Why not just tell them to shut down via ssh regardless? If the machine is already down, it won't be available to connect. You can use the ConnectTimeout option in ssh so it doesn't wait forever for it to respond. If you set the timeout to just a few seconds, it shouldn't take much longer than trying to ping a dead computer anyway.


All times are GMT -5. The time now is 02:39 PM.