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.
 

Members online


Latest posts

Top