How to send my ping.sh to a .txt file?

DanielQuevedo

New Member
Joined
Jan 1, 2020
Messages
4
Reaction score
2
Credits
0
Hello guys. I'm having trouble with a ping command here. I wanna create a bash script that do an no-stopping ping in a network and save the result in a .txt file. Here's my code:

ping hostname | while read pong; do echo "$(date): $pong"; >>ping;txt; done

My ping.txt is empty. Can you help me? Thanks in advance!
 


you don't need a script to do a continuous ping in Linux. Continuous is the default. What is pong and what purpose does it serve in the script?
Try "ping hostname >> ping.txt"
 
Just put the redirection after the done statement.
e.g.
Bash:
ping hostname  | while read pong; do echo "$(date):$pong"; done >> ping.txt

Putting the redirection immediately after the done will redirect all output from the while loop to the output file.
 
you don't need a script to do a continuous ping in Linux. Continuous is the default. What is pong and what purpose does it serve in the script?
Try "ping hostname >> ping.txt"
Thanks for the reply man. My ping.txt contains the ping of ~5 min of the ping itself. i'd like a ping.txt that holds a continuous ping all day long.
 
Thanks for the reply man. My ping.txt contains the ping of ~5 min of the ping itself. i'd like a ping.txt that holds a continuous ping all day long.
In linux, if you use ping without using parameters like -c, -w or -W; ping will just run continuously until you stop/kill the process.

The only exception to this is if your distro has an alias set up for ping, which might specify a count.
So if you only got 5 minutes, perhaps ping is set up as an alias using the -w (deadline) option and a value of 300.
e.g.
alias ping='ping -w300'

The way to check whether a command has been aliased is to use the type command:
e.g.
type ping
If it has been aliased, you will see something like:
ping is aliased to 'ping -w300'

If it has not been aliased, you will see something like:
ping is hashed (/bin/ping)
Which means that it's a binary executable.

If an alias has been set up, it means that each time you type:
ping hostname >> ping.txt

The shell will expand that to:
ping -w300 hostname >> ping.txt

To remove an alias - you can either unalias ping - which will affect only the current teminal session. Or to permanently remove it - you can remove the alias from your .bashrc (or whichever dot file the alias has been set up in), OR simply escape the alias using \ping instead of ping.

\ping will escape any aliases set up for ping and will only pass the parameters that you specify to ping. In fact if you prepend a backslash before any command, it will escape any aliases for that command.

So typically things like ls and grep will have aliases set up that set some default parameters. And very often these aliases are extremely handy. But sometimes you might need their output to be slightly different, so you use a backslash before the command to run the program without the alias.

So for example, my grep is aliased to:
alias grep='grep --colour=auto -n'
So grep will always highlight results in colour and show a line-number. And very often when I'm grepping source code - I want to see the line numbers and have things coloured.

But in a shell script where I might be grepping for a line in a file that contains a certain keyword, so I can parse out a line of data - I don't want the line-number to be included in greps output - because it adds an extra, unwanted field to the start of that line of data. So I will use \grep to escape the alias for grep. That way my script can find lines in a data file that contain a keyword and then cleanly parse the rest of the data in that line.
 
In linux, if you use ping without using parameters like -c, -w or -W; ping will just run continuously until you stop/kill the process.

The only exception to this is if your distro has an alias set up for ping, which might specify a count.
So if you only got 5 minutes, perhaps ping is set up as an alias using the -w (deadline) option and a value of 300.
e.g.
alias ping='ping -w300'

The way to check whether a command has been aliased is to use the type command:
e.g.
type ping
If it has been aliased, you will see something like:
ping is aliased to 'ping -w300'

If it has not been aliased, you will see something like:
ping is hashed (/bin/ping)
Which means that it's a binary executable.

If an alias has been set up, it means that each time you type:
ping hostname >> ping.txt

The shell will expand that to:
ping -w300 hostname >> ping.txt

To remove an alias - you can either unalias ping - which will affect only the current teminal session. Or to permanently remove it - you can remove the alias from your .bashrc (or whichever dot file the alias has been set up in), OR simply escape the alias using \ping instead of ping.

\ping will escape any aliases set up for ping and will only pass the parameters that you specify to ping. In fact if you prepend a backslash before any command, it will escape any aliases for that command.

So typically things like ls and grep will have aliases set up that set some default parameters. And very often these aliases are extremely handy. But sometimes you might need their output to be slightly different, so you use a backslash before the command to run the program without the alias.

So for example, my grep is aliased to:
alias grep='grep --colour=auto -n'
So grep will always highlight results in colour and show a line-number. And very often when I'm grepping source code - I want to see the line numbers and have things coloured.

But in a shell script where I might be grepping for a line in a file that contains a certain keyword, so I can parse out a line of data - I don't want the line-number to be included in greps output - because it adds an extra, unwanted field to the start of that line of data. So I will use \grep to escape the alias for grep. That way my script can find lines in a data file that contain a keyword and then cleanly parse the rest of the data in that line.
Thanks for the help man! It's working now.
 
I know this is an old thread, but a search brought me here yesterday. I'm running embedded Linux on a SoM that uses a variant of: Yocto Project™ 3.2 (gatesgarth), so everything is scaled back a bit, but I could not pipe ping results to a file unless I did this squirrely thing:

ping 10.0.0.2 -4 -c 8 -s 128 -p A5 -A | cat >> /lib/firmware/$LogName

Maybe this will be of use or interest to the next person that comes along... peace.

And thanks for all the good advice that turns up on this site!
 
@JasKinasis :-

I'm no command-line guru, Jason - and I know this is an old thread, so apologies for posting here again! - but.....wouldn't you need to set-up a continuously updating text file in somewhere like /tmp?

I only say this because an acquaintance and I between us "hacked" the code for Puppy's default CPU temp tray indicator to make it work for Nvidia GPUs instead. After an alarming couple of years with ridiculously high temperatures on my old Latitude D630's Nvidia mobile GPU, I first came up with an on-demand indicator running from a tray icon. I then conceived the notion of having a continuous GPU temp display in the tray alongside the CPU temp indicator, so.....we modified the existing code for that, hacked-about a bit & made it happen.

In all the above cases, we tend to use /tmp under Puppy for any process that's being continuously updated. I was just curious as to how mainstream distros might handle this kind of thing?


Mike.
wonder-small.gif
 

Members online


Top