Ping results are not saved in a file [duplicate] - linux

This question already has answers here:
Bash loop only read the last line
(6 answers)
Closed 18 days ago.
I am running a simple shell script which initiates the ping and save the result to a txt file.
ips=$(cat host.txt)
for ip in $ips
do
ping -c 2 $ip > pingtest.txt
done
However for some reason, there is no output in the text file,I am sure the ip is pinging(I have confirmed with TCP DUMP).
Can someone please help me?
hostfile output is:
10.0.0.10
10.0.0.11
172.28.209.43
172.16.84.131

Don't overwrite the file on every iteration. Do something like this.
ips=$(cat host.txt)
for ip in $ips
do
ping -c 2 $ip
done > pingtest.txt
Also as a suggestion, use consistent indentation and avoid word splitting when filename expansion a.k.a globbing is enabled. For example you can use a while read loop instead of a for.
while IFS="" read -r ip
do
ping -c 2 "$ip"
done <host.txt > pingtest.txt

Related

Shell script not able to parse host list and host unknown

I have a script that I am trying to run. The purpose of this script is to run single or multiple commands on list of servers. When running it, the script is not able to parse the hosts file and gives an error:
ssh: Could not resolve host name nodename nor servname provided, or not known
while read host;
do
echo server: $host
sshpass -p 'password' ssh -o "StrictHostKeyChecking no" admin#$host 'command'
done < /path/to/hosts.txt
hosts.txt contains list of IP addresses of the hosts that need to connect in the following format:
server 1
server 2
server 3
I have tried putting the server IPs in "" and '' and ; and ,, but they all give the same error.
Can someone explain what I am doing wrong here?
Please check if $host variable is getting parsed properly. Also, in your hosts.txt, there are two entries in each row. What does it signify?
I have created a sample script to parse space-separated arguments
Input file: hosts.txt
Script output:
#!bin/sh
while IFS=' ', read hostname ipaddress;
do
echo server: $ipaddress
done < hosts.txt
#Cyrus and #Gordon Davisson thank you -n resolved the issue. such a simple fix i am surprised i didnt see that post here before despite of lot of searching. guess i wasnt wording it the right way. Thank all for the input.

writing a basic script that pings the current network [duplicate]

This question already has answers here:
Send a ping to each IP on a subnet
(14 answers)
Closed 3 years ago.
I'm having trouble writing a simple script that pings its current network.
I want the output to look the same as a normal ping but I'm having trouble getting to that point
here's the original question from the assignment
-write a short script(using the ping command)to do a "ping" scan of your current network.
any help you can give is much appreciated
I originally tried printf but I wasn't able to even get a decent output so I gave up and started using echo
This is what I have right now
#!/bin/bash
for ping in $(ping -c 4 -v 192.168.1.10)
do echo $ping
done
my teacher said that I would run into two problems with the simple ping script and that I could find the answer in the ping man pages. however I can't seem to find anything to help.
I basically want the script to run as if I just ran a normal ping command and have it display output in the same way.
According to the question from your assignment, I would say what you need to do is invoking ping in all the IP's in your current network (192.168.1.0 in case it is a /24 mask).
What you are trying to do is a simple ping to 192.168.1.10.
If that is the case, why don't you just call ping command in your script?
#!/bin/bash
ping -c 4 -v 192.168.1.10
I think what your teacher really wants is you to find the way of making a loop over all the IP directions in your network.
#!/bin/bash
ping= ping -c 4 -v 192.168.1.10
print $ping
simply get the out put

Need help for simle bash script

Can someone help me with the syntax for a simple bash script that trying to write:
echo ping -c 1
echo nslookup
Basically I want to receive output of one line from the ping and the nslookup information for a domain that I'm checking. Unfortunately I'm unable to get this correctly.
P.s. this is basically the first thing that I'm trying to accomplish in bash.
Thank you in advance!
Thank you for the provided information on the matter. I felt little ashamed from the nature of my question so I spent little more time to read. The solution that I found is the following:
#!/bin/bash
for i in $*;do
ping -c 1 $i &
nslookup $i &
done
#
'
Once I added the scrit to the /bin folder I used the commands:
chmod +x "script name"
dos2unix ""scrit name"
so not I'm able to use it only by typing the name of the script.
hi after writing and saving script and giving permission to the script
just go the folder and
./
of else you just can use
sh
Hi i would be better if you elaborate your problem, if you want one line output for ping command filter using head
eg
ping www.google.com | head -n 1
or if you are thinking of sending one packet of data to the server
ping -n 1 www.google.com
nslookup www.google.com
and if your are writing Bash Script
#!/bin/bash
ping -n 1 www.google.com
nslookup www.google.com
save the file
give execute permission and run
Please let me know i answered your question

Shell script while only loops once [duplicate]

This question already has answers here:
ssh breaks out of while-loop in bash [duplicate]
(2 answers)
Closed 8 years ago.
I want to get all the servers' time with ssh, but my script only loops once and then exit, what's the reason?
servers.txt
192.168.1.123
192.168.1.124
192.168.1.125
192.168.1.126
gettime.sh
#!/bin/bash
while read host
do
ssh root#${host} date
done < "servers.txt"
OUTPUT
Tue Feb 3 09:56:54 CST 2015
This happens because ssh starts reading the input you intended for your loop. You can add < /dev/null to prevent it:
#!/bin/bash
while read host
do
ssh root#${host} date < /dev/null
done < "servers.txt"
The same thing tends to happen with ffmpeg and mplayer, and can be solved the same way.
PS: This and other issues are automatically caught by shellcheck:
In yourscript line 4:
ssh root#${host} date
^-- SC2095: Add < /dev/null to prevent ssh from swallowing stdin.

bash ping ip run command on reply [duplicate]

This question already has answers here:
Checking host availability by using ping in bash scripts
(11 answers)
Closed 5 years ago.
I want to check the ping replies on two IP addresses and if they are both up, then I execute a command.
For example:
ping 8.8.8.8 on response do
ping 8.8.4.4 on response
execute command
Is there a simple bash script to do this?
According to the manpage on ping:
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.
Thus you can rely on the exit code to determine whether to continue in your script.
ping 8.8.8.8 -c 1
if [ $? = 0 ]
then
echo ok
else
echo ng
fi
Try ping only 1 time with -c 1 option. Change to any number as you like.
$? is the exit code of the previous command. You can refer ping's exit code with it.
Modify the above code snippet to what you want.
Bash commands to return yes or no if a host is up.
Try hitting a site that doesn't exist:
eric#dev ~ $ ping -c 1 does_not_exist.com > /dev/null 2>&1; echo $?
2
Try hitting a site that does exist:
eric#dev /var/www/sandbox/eric $ ping -c 1 google.com > /dev/null 2>&1; echo $?
0
If it returns 0, then the host is evaluated to be responsive. If anything other than zero, then it was determined that the host is unreachable or down.

Resources