Ping bash script to file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Ping bash script to file
# 1  
Old 09-28-2011
Ping bash script to file

Hello:

I have this script:

#!/bin/bash
#for loop
for ip in `cat ips`
do
ping $ip | grep "is alive">>pingtestlog
done


And its working properly with this input:

ericadm@amxcruas1> cat ips
10.196.60.4
10.196.61.210
10.196.62.73
10.196.61.152

And this output:

ericadm@amxcruas1> cat pingtestlog
10.196.60.4 is alive
10.196.61.210 is alive
10.196.62.73 is alive
10.196.62.43 is alive



But I will like to include the date on the output filename, and inside the file.

Can you please help me with this!!!
# 2  
Old 09-28-2011
Code:
#!/bin/bash
outputfile="pingtestlog"$(date +%Y%m%d)
date > $outputfile
while read ip
do
 ping $ip | grep "is alive" >> $outputfile
done < ips

# 3  
Old 09-28-2011
Thanx!
But I got the following error:

ericadm@amxcruas1> /bin/sh ./pingdate.sh
./pingdate.sh: syntax error at line 2: `outputfile="pingtestlog"$' unexpected
# 4  
Old 09-28-2011
can you execute this alone in the console.

Code:
 
outputfile="pingtestlog"$(date +%Y%m%d)
echo $outputfile
 
or 
 
outputfile="pingtestlog".`date +%Y%m%d`
echo $outputfile

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 09-28-2011
ericadm@amxcruas1> /bin/sh ./file.sh
./file.sh: syntax error at line 2: `outputfile="pingtestlog"$' unexpected
ericadm@amxcruas1> cat file.sh
#!/bin/bash
outputfile="pingtestlog"$(date +%Y%m%d)


The last one seems to be working:

outputfileericadm@amxcruas1> /bin/sh ./file2.sh
pingtestlog.20110928
ericadm@amxcruas1> cat file2.sh
#!/bin/bash
outputfile="pingtestlog".`date +%Y%m%d`
echo $outputfile

---------- Post updated at 03:00 AM ---------- Previous update was at 02:55 AM ----------

Thanx!!! itkamaraj

It's working:

ericadm@amxcruas1> /bin/sh ./pingdate.sh
ericadm@amxcruas1> cat pingtestlog.20110928
Wed Sep 28 01:58:42 CST 2011
10.196.60.4 is alive
10.196.61.210 is alive
10.196.62.73 is alive
10.196.61.152 is alive
ericadm@amxcruas1> cat pingdate.sh
#!/bin/bash
outputfile="pingtestlog".`date +%Y%m%d`
date > $outputfile
while read ip
do
ping $ip | grep "is alive" >> $outputfile
done < ips
# 6  
Old 09-29-2011
You whole script (at least what you mention) has been done:
Code:
http://fping.sourceforge.net/

It's not a script, but it's very full featured and handy.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Ftp bash script appends to file using cron instead of coping new file

I have a bash script that is running a mysql query and creating a csv file with a time stamp. It then uploads that to a ftp server. Everything works great when I manually run it. But then I have a cron job set to run every monday, wednesday and friday at 5am est. When the cron job runs, it... (7 Replies)
Discussion started by: akallenberger
7 Replies

3. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies

4. Shell Programming and Scripting

Expect script brokes when Ping file save

Hi, For some reason I was needed to ping some URL and save the output to a file through Expect script, following is a very basic of that script, but it fails everytime. When I just copied the same 'ping' line to normal Terminal and run, it runs fine. Please, help. #!/bin/sh spawn ping -c 3... (1 Reply)
Discussion started by: santanu4ver
1 Replies

5. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

6. Shell Programming and Scripting

ping with timeout bash

Hi experts: I want to do below thing with bash if ping 192.168.0.1 sucussful then do somthing without delay, if ping failed within 20s, then kill ping and exit not sure this can use "alarm" to do this. any code will be welcome Thanks (1 Reply)
Discussion started by: yanglei_fage
1 Replies

7. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

8. Shell Programming and Scripting

Bash script for ping in your own subnet

I have a question for one who wants to help me. I want to create a bash script to ping IP-adresses. It must first ask me the beginnen IP, then the ending IP like: 192.168.100.1 - 192.168.100.255. When nothing is filled in, then it must find my subnet and ping that, like when my ip is... (14 Replies)
Discussion started by: ugurgazi
14 Replies

9. AIX

Script to ping servers in a file

I would like to ping a list of servers in a text file. Can anyone help? (1 Reply)
Discussion started by: gbarkhor
1 Replies

10. Shell Programming and Scripting

Ping script using Redhat and BAsh

Alright, I have being checking out various posting here trying to hack together something for a friend Needed: A script that can run in cron doing: ping of several hosts and notifying via email when they are unavailable. I am not going to post my non working messes (I am a basic... (11 Replies)
Discussion started by: edkung
11 Replies
Login or Register to Ask a Question