LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-24-2012, 11:30 PM   #1
abhi6691
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 06-25-2012, 12:19 AM   #2
rmugunthan
Member
 
Registered: Jan 2009
Location: Coimbatore, India
Distribution: rhel
Posts: 49

Rep: Reputation: 3
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.
 
1 members found this post helpful.
Old 06-25-2012, 02:38 AM   #3
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,639

Rep: Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653
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
 
1 members found this post helpful.
Old 06-25-2012, 07:00 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,799

Rep: Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568Reputation: 7568
probably you can try nmap and look for ssh port.
 
Old 06-25-2012, 08:45 AM   #5
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
Old 06-25-2012, 10:15 AM   #6
abhi6691
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by John VV View Post
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
 
Old 06-25-2012, 04:04 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,639

Rep: Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653Reputation: 2653
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.
 
Old 06-25-2012, 04:11 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
remote ssh between two computers with dynamic IPs dimm0k Slackware 8 05-08-2012 01:45 PM
unable to ssh remote server (firewall turn off) ankitsagwekar Linux - Server 9 06-28-2011 11:47 PM
do ssh first to router to turn off in apccontrol script ? b-RAM Linux - Software 1 05-12-2010 12:16 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM
shell script for remote ssh login sefaklc Programming 5 08-28-2007 09:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration