LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-24-2012, 07:41 AM   #1
Beverly
LQ Newbie
 
Registered: Nov 2012
Location: South Africa
Distribution: N/A
Posts: 1

Rep: Reputation: Disabled
Write a shell script that pings a list of hosts and reports the unreachable ones.


Hi there,

I recently attended at Interview and they have now sent me a techinal assessment to complete. I have not used Linux before. Could you please assist me?

I will really appreciate any help from you guys.


Here is the Question:

Write a shell script that pings a list of hosts and reports the unreachable ones.

INPUTS:
a) As a command line parameter, the filename of the text file to read
b) The text file containing host names or IP addresses, one per line

OUTPUTS:
To stdout, print unreachable host names, one per line
 
Old 11-24-2012, 07:59 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Have you tried anything? If so, show us what you've got.

That said, first you have to think about how would you do it manually.
1) start with pinging a single host.
2) figure out how to get and process the information that ping returns to you.
3) make a loop that will do that for all servers in the list

If you're new to unix shell scripting, these could help you get started:

http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://tldp.org/LDP/abs/html/index.html

If you get stuck, just post a specific problem and we will be happy to help you.
 
Old 11-24-2012, 10:08 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Beverly;

I assume you meant a job interview. If so, we would be doing you a major disservice by simply providing the answer. They are trying to see if you know how to do basic Linux tasks. If you don't, they will find that out sooner or later.

Start here to learn the basics of shell scripting:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

If it was NOT a job interview, then give us some more details.....
 
Old 11-24-2012, 04:46 PM   #4
TheBigMing
Member
 
Registered: Dec 2008
Location: east anglia
Distribution: SuSE, antiX
Posts: 45

Rep: Reputation: 8
This should work OK.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Contents of pingtext.txt:

www.linuxquestions.org
www.linuxcommando.blogspot.co.uk
www.ubuntuforums.org/
www.example.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The forward slash at the end of ubuntuforums.org
will cause ping to fail
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pingtest.sh

#!/bin/bash

# NB: backticks ( ` ) or 'reverse pipe'
# surround cat ~/pingtest.txt

for name in `cat ~/pingtest.txt`

do
ping -c 1 $name
if [ $? -ne 0 ]; then
echo $name >> pingtest.fail
fi
done

# $? is the exit code for for the 'ping'
# command if '0' it went well, anything
# else badly


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ming

Last edited by TheBigMing; 11-25-2012 at 03:12 AM. Reason: added info (didn't read brief properly!)
 
Old 11-25-2012, 10:45 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@TheBigMing, could you please pay attention to what others say in the thread? The previous posters made it clear that this is not a situation where just giving the person a solution is proper.

In any case, the script you gave has at least two errors in it. First, Don't Read Lines With For. Second, you should never leave variable expansions unquoted.

Finally, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
 
Old 11-25-2012, 12:10 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
This can be accomplished much more efficiently and concisely with a single nmap command (ok, you also have to massage the output a bit with grep+cut or awk).
 
Old 11-25-2012, 05:11 PM   #7
TheBigMing
Member
 
Registered: Dec 2008
Location: east anglia
Distribution: SuSE, antiX
Posts: 45

Rep: Reputation: 8
@TheBigMing, could you please pay attention to what others say in the thread? The previous posters made it clear that this is not a situation where just giving the person a solution is proper.

@David the H: Without wishing to start a flame war: I fundamentally & totally disagree! In 1982 the publishing company I worked for purchased a Commodore PET. In those days nobody (apart from the IT Trogs running some monolith in an air conditioned basement) had any idea about how a computer worked, there was no broadband and the WWW didn't exist. I had to produce a condense of quarterly sales figures which I did by hand from a thick sheaf of printouts - columns of figures by type, by account, by area, % change with totals at the end of columns & lines. It was a nightmare.

I learned to programme in dBase II from a manual and some printouts of other peoples code (and the other peoples code here is important). It took me two or three days to do but when I'd written my 1st program I could mark up the sheets, get my secretary to input the figures & produce a dot matrix print out. A job which used to take three days took less than one.

In about 1990 I got my own PC and, again, I learned to program WordPerfect 5.1 from the 980 page manual and a shareware disc full of macro's. WordPerfect 5.1 was a great program - and it's manual remains to this day an examplar of what all manuals should be - but it was not easy to get to grips with. Pages 826 to 847 consisted entirely of Macros & Merge Value Tables - 21 pages of numbers which you could use do things in a macro - 'if {system} right~=49664~' for instance to indent text (how do I know, I still have my heavily annotated manual). I eventually wrote, amongst other things, a program for an antiquarian bookseller which not only kept stock but compiled, typeset & produced illustrated catalogues.

The point I am making here is that I learned, and continue to learn, very largely, from example. To a certain extent I understand your position. You wish people to start, learn the basics, gradually increase their knowledge & thus become proficient - but in this case the supplicant does not have time do that. She has to complete a questionnaire for a job. If the interviewers do their job properly they will question her about the script she's given them. If she does her job properly she'll find out how & why it works. She's a modern girl, all modern girls have a cllection of technical knowledge vastly greater than the knowledge I had in 1982.

In 1979 we published a book by a Univ. of Essex lecturer called Andrew Lister. The book was "The Fundamentals of Operating Systems" and I promoted it throughout the UK and Northern Europe. It was my very first contact with computers. I went into an office in a Computer Studies department and produced the book. He said: "This is software. My field is hardware". I replied: "I don't understand the difference". To start, that's how much I knew.

Last edited by TheBigMing; 11-25-2012 at 05:21 PM.
 
Old 11-25-2012, 06:09 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by TheBigMing View Post
I learned to programme in dBase II from a manual and some printouts of other peoples code (and the other peoples code here is important). It took me two or three days to do but when I'd written my 1st program I could mark up the sheets, get my secretary to input the figures & produce a dot matrix print out. A job which used to take three days took less than one.
But if the other people's code already did exactly what you needed, would you have learned anything, or would you have simply used their code as is?
 
Old 11-25-2012, 07:59 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
BigMing;

I humbly suggest that you look at some aspects of how things work here. First, the forum does in fact discourage doing people's homework for them----but, never mind that: On first principles, we can create a compelling argument for not handing people the answers---for anything.

Take a look at the behavior of the typical "homework troll". In many cases (including this thread), they simply do not follow up (hint: As I write this, Beverly has seen NONE of the replies). Sometimes, you'll be able to see that the OP has read the thread, but there will still be no actual reply. And, all too seldom, the OP will actually post some kind of follow-up. The point is that the vast majority of homework posters learn nothing by having their work done by us.

Just as we should not be giving handouts to teenagers, we should not reward those who lack the motivation and/or common courtesy to accept and act on REAL help.
 
  


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 script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 06:27 PM
Bash/Perl script - provide list of hosts to choose from and ssh to chosen host loadedmind Programming 4 04-07-2010 01:19 PM
[SOLVED] average number of hosts that respond to pings during a traceroute icygalz Linux - Newbie 1 02-27-2010 03:21 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM

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

All times are GMT -5. The time now is 08:36 AM.

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