Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Unix script to PING a list of devices and output the result to a txt file

Posted on 2014-11-04
8
570 Views
Last Modified: 2014-12-08
Hi

Im very new to Unix scripting and I'm having trouble running a basic script.  Id like to create a Unix script to PING a list of devices and output the result to a txt file.

Im trying to pull the list of devices from a txt file and then output the result to a txt file.

here is what I have so far.
--------------------------------------------------------------------------------------------------------------------------------------------------
#! /bin/bash

for $i in 'cat serverlist'
do
ping $i >> output.txt
done
----------------------------------------------------------------------------------------------------------------------------------------------------

I have tested this on a Cygwin installed on my windows box but i get the following error.

$ ./_Ping.sh
./_Ping.sh: line 2: $'\r': command not found
./.sh: line 4: syntax error near unexpected token `$'do\r''
'/.sh: line 4: `do

Any ideas?
0
Comment
Question by:tjoconnor
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Accepted Solution

by:
savone earned 500 total points
ID: 40421430
You need backticks (not single quotes) to execute a command.  Also ping will continue forever in linux unless you put a count on it.

#!/bin/bash
for i in `cat serverlist`;
do
ping -c 2 $i >> ping
done
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40421432
Several: problems:

1) Use backticks or $( ) around the "cat" command and don't use "$i" (that's the content of the variable "i") in the "for" line, use "i" alone:

for i in $(cat serverlist)
do
ping -c 1 $i >> output.txt
done

2) Your script file contains "carriage return + line feed" at the line ends. This is DOS standard and is wrong in Unix, which uses "linefeed" alone.
If you transferred the script file from Windows via FTP transfer it again using the "ASCII" transfer method, or edit the file in Unix and remove the extra carriage return characters.

3) Limit the number of ping attempts, so it will not run endlessly (see above)
0
 
LVL 23

Expert Comment

by:savone
ID: 40421433
To explain further:

To execute a command in the for loop you have to use backticks (under the tilde on the keyboard) not single quotes.

The command "ping -c 2" tells the system to only ping it twice, otherwise it will go on forever and the script will never stop.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40421445
Note:

If you expect error messages from the "ping" command take care to store these messages in the log file or in a separate error log file instead of displaying them on your terminal:

1) One file:

ping -c 1 $i >> output.txt 2>&1

2) Separate error log:

ping -c 1 $i >> output.txt 2>>errors.txt
0
 

Author Comment

by:tjoconnor
ID: 40421507
Thanks for the responses. I seem to be ok with the file now as there is no error on the command line.

However my output file looks like this

Bad option -c.

Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
            [-r count] [-s count] [[-j host-list] | [-k host-list]]
            [-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40421517
Your ping command does not support the "-c" flag. Which is your OS? HPUX?
It seems that you'll have to use "-n" instead:

ping -n 1 $i >> output.txt 2>>errors.txt
0
 

Author Comment

by:tjoconnor
ID: 40421540
Hi

Yes I used -n 3 $I>>..... and it worked

Thanks a million for your help today guys, really appreciated it
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40421585
You're always welcome!
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question