The example in the accepted answer executes in 3 1/2 seconds, about 169 times slower than the 1/40 second in example for this answer. The correct way to grep a variable with on shells that support herestrings is using input redirection with 3 of these guys: <, so your grep command ( $ip variable) should actually read as follows: ip="$(grep -oE '' /dev/null I have also heard that backticks are deprecated and should be replaced with $(). The awk answer is good for your specific case, but the reason you were receiving an error is because you were trying to use grep as if it were searching for a file instead of a variable.Īlso, when using regular expressions, I always use grep -E just to be safe. If you insist on using ping and sed, you can do: % ping -c1 | sed -nE 's/^PING+\((+)\).You almost had it right the first time. 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 am now using a timer script that I created to configure iptables at certain times to block certain domain name resolutions. 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!!! How to parse the IP ADDRESS "ONLY" from ping OUTPUT when using ping on a domain name. 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. ![]() 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. ![]() I have crafted a Bash tool that runs on a server.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |