LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2005, 11:32 PM   #1
ratul_11
LQ Newbie
 
Registered: Dec 2005
Posts: 29

Rep: Reputation: 15
shell programming problem


Hi guys
This is Anirban here. I have the problem again with the previous shell script.Which was
Write a shell script that will check whether a server is up or not(on ping) & log the report to a file.

I have tried to write the program in the following way

#! /bin/sh
echo -n "Enter the IP or Hostname of the Server"
read host
#echo $host
ping -c2 $host &>file2
if [ $? = 0 ];then
echo "Server is up and working" $host
else
echo"Server is not up and not working" $host
fi

But when I am trying to execute the program in the case when the server is not up it is showing
The server is up and running but from the ping statistics it is showing that 100% loss is occured.I am not able to point the problem.(i used the chmod 755 filename before executing the program)

Please help me.And one more thing is there any other option to check the connectivity without ping command?
 
Old 12-16-2005, 02:46 AM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Code:
if [ $? = 0 ];
"=" is for comparing strings. Try:
Code:
if [ $? -eq 0 ];
 
Old 12-16-2005, 02:47 AM   #3
cyberjun
Member
 
Registered: Mar 2005
Distribution: Gentoo kernel 2.6.14
Posts: 96

Rep: Reputation: 15
Hi,
try this:
Code:
#!/bin/bash

echo -n "Enter the IP or Hostname of the Server: "
read host
echo $host
ping -c2 $host > /dev/null 2> /dev/null
if [ $? = 0 ];then
echo "Server is up and working" $host
else
echo "Server is not up and not working" $host
fi
 
Old 12-19-2005, 10:03 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I just use:
Code:
        if ! ping $host
        then
            ERROR failed on $host $nickname
        fi
I think using ping is quite a crude method. There are probably much better
ways to do it.
 
Old 12-19-2005, 07:44 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
ping will tell you whether the TCP/IP stack is working ie basic network cxn is up. Unfortunately, that's all it tells you .... also be aware that some people disable ICMP request/response to try and avoid probs with ping flood attacks.
 
Old 12-20-2005, 03:47 AM   #6
denver1980
Member
 
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48

Rep: Reputation: 15
Best way is tu use nmap ...
Code:
nmap -FP0 192.168.0.100 | tail -1
 
Old 12-20-2005, 07:29 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I've got a script you can have if you want?
Wrote it years ago, it does the job good enough.
 
  


Reply



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
shell programming problem ratul_11 Programming 2 12-15-2005 12:05 AM
shell programming problem ratul_11 *BSD 2 12-14-2005 12:47 PM
new to shell programming bluetwist Debian 3 08-01-2005 03:56 PM
shell script programming problem newbie_ken Programming 5 08-16-2004 11:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 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