Last active
April 7, 2022 19:28
-
-
Save scentoni/3b0a146d6c95456bbe8dfa7ed830436b to your computer and use it in GitHub Desktop.
hostify: try to translate everything that looks like IP addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# hostify: try to translate everything that looks like IP addresses | |
# into hostnames. Leave them alone if DNS doesn't resolve them. | |
# echo "Please tip your 127.0.0.1 well." |hostify | |
# Please tip your localhost well. | |
use Socket; | |
sub host { return gethostbyaddr(inet_aton($_[0]), AF_INET) // $_[0]} | |
while(<>){ | |
# s/(\d+\.\d+\.\d+\.\d+)/host $1/ge; | |
s/((\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})/host $1/ge; | |
print $_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Might be able to use GNU sed
s///e
to do something like this without perl, see https://stackoverflow.com/questions/39349032/sed-replace-with-the-output-of-a-bash-command-using-capture-group-as-argument