Write a shell script that pings a list of hosts and reports the unreachable ones.
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.
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
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:
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The forward slash at the end of ubuntuforums.org
will cause ping to fail
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@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.
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.
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).
@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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.