If you insist on using ping and sed, you can do: % ping -c1 | sed -nE 's/^PING+\((+)\). In essence, with my configuration, this will first check /etc/hosts, then mDNS and at last DNS. In /etc/nf, so getent hosts will query in sequence and use gethostbyaddr(3) or gethostbyname(3) based on name and ahosts will use getaddrinfo(3). In my computer, i have: hosts: files mdns4 dns etc/nsswitch, then use the getent command with hosts (or ahosts) database e.g.: getent hosts Here's a dig output: % dig +short Īs a side note, in Linux, if you want to query by the NSSwitch (Name Service Switch) i.e. You should look at dig, host, nslookup - whatever suites you the best. Ping is for checking whether a host is up or down based on ICMP response, it is never the right tool for only resolving IP address, there are dedicated tools for that. Again, thank you to everyone that tried to help. I want to check that output for any IP address like 159.143.23.12 134.12.178.131 124.143.12.132 if (IPs are found in ) then // bunch of actions // else // bunch of actions // Is fgrep a good idea I have bash available.I am now using a timer script that I created to configure iptables at certain times to block certain domain name resolutions. 26 I have a script that generates some output. I have since refined my methods and do not use any of the answers here but I thank everyone for their attempts with helping here. I have, since asking this question, learned proper POSIX regex to do what I needed and I need to make it clear that I was originally asking about the regular expressions for sed that would print a single instance of an IP from ping output. This is NOT asking to resolve a domain name and therefore is NOT A DUPLICATE!!! Since some marked this question a duplicate of some other bullshit question that has nothing to do with this one, I will make it clear enough that retards with English comprehension troubles can understand: This question is specifically about managing ping output using sed in order to keep the tool I have created more portable as ping comes default with almost any and all linux distros. I already know/knew how to resolve IP address from a host name or domain. How can I remove the duplicate with sed so that I can store the single IP address into a variable and run the script as a cronjob, or am I on a better track using tr? Which gives me an output of: you can see, there is a duplicate of the same IP address. Rtt min/avg/max/mdev = 46.748/46.748/46.748/0.000 msĪnd the command I have narrowed this output down with is: ping -c 1 -s 16 | grep -o '(*' | tr -d '(44):\n' I have a file with a lot of IP addresses in it named address.list. So if I do a ping for Google: ping -c 1 -s 16 I don't want to do this as I believe a single ping will be much more lightweight and portable. to 3:30 P.M.).Īs of currently, the tool works fine, but I have to input IP addresses to certain websites manually into a text file and have the tool pull the IP address based on the nth line using the "head" and "tail" commands. This tool will block certain IP addresses for a certain time range (i.e. If you want to put a little more effort in, and parse more data out, I recommend using the -online argument to the ip command, which will let you treat every line as a new device: $ ip -o linkġ: lo: mtu 16436 qdisc noqueue \ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00Ģ: eth0: mtu 1500 qdisc pfifo_fast qlen 1000\ link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ffģ: eth1: mtu 1500 qdisc pfifo_fast qlen 1000\ link/ether 00:0c:29:30:21:52 brd ff:ff:ff:ff:ff:ffĤ: tun0: mtu 1500 qdisc pfifo_fast qlen 100\ link/ĥ: sit0: mtu 1480 qdisc noop \ link/sit 0.0.0.0 brd 0.0.0.I have crafted a Bash tool that runs on a server. You can trivially get the mac address from this output with awk: $ ip link show eth0 | awk '/ether/ ' I like using /sbin/ip for these kind of tasks, because it is far easier to parse: $ ip link show eth0Ģ: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |