Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ping each ip in a file?

I have a file named "ips" containing all ips I need to ping. In order to ping those IPs, I use the following code:

cat ips|xargs ping -c 2

but the console show me the usage of ping, I don't know how to do it correctly. I'm using Mac os

like image 668
user1687717 Avatar asked Jan 21 '13 13:01

user1687717


People also ask

How do I ping multiple hosts and save results in a test file?

First, put all of your server names into a text file with each server name on a separate line. Let's call it "servers. txt" and save it (as you going to ping server names so make sure name resolution is happening).


1 Answers

You need to use the option -n1 with xargs to pass one IP at time as ping doesn't support multiple IPs:

$ cat ips | xargs -n1 ping -c 2

Demo:

$ cat ips
127.0.0.1
google.com
bbc.co.uk

$ cat ips | xargs echo ping -c 2
ping -c 2 127.0.0.1 google.com bbc.co.uk

$ cat ips | xargs -n1 echo ping -c 2
ping -c 2 127.0.0.1
ping -c 2 google.com
ping -c 2 bbc.co.uk

# Drop the UUOC and redirect the input
$ xargs -n1 echo ping -c 2 < ips
ping -c 2 127.0.0.1
ping -c 2 google.com
ping -c 2 bbc.co.uk
like image 187
Chris Seymour Avatar answered Sep 30 '22 04:09

Chris Seymour