Last active
February 20, 2022 23:12
-
-
Save whiteinge/10314597 to your computer and use it in GitHub Desktop.
Output IP addresses for machines connected to the local machine using /proc/net/tcp for speed
This file contains hidden or 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/awk -f | |
| # Output IP addresses for machines that are connected to the local machine. | |
| # | |
| # Usage: | |
| # ./connected_ips.awk /proc/net/tcp | |
| # ./connected_ips.awk -v port=4505 /proc/net/tcp | |
| function fromhex(s){ | |
| # Amazing voodoo from William. | |
| # http://compgroups.net/comp.lang.awk/reading-hexadecimal-numbers/33952 | |
| return index("0123456789abcdef", tolower(substr(s, length(s)))) \ | |
| -1 + (sub(/.$/, "" ,s) ? 16 * fromhex(s) : 0) | |
| } | |
| BEGIN { | |
| port_match = port ? sprintf(":%X$", port) : ":*$" | |
| } | |
| # Skip the header line and loopback addresses; optionally filter by port. | |
| NR != 1 && $3 !~ /:0000$/ && $2 ~ port_match { | |
| split($3, conn, ":"); ip = conn[1] | |
| if (!port) { sub(/[A-F0-9]+:/, "", $2); printf("%d\t", fromhex($2)) } | |
| printf("%d.%d.%d.%d\n", \ | |
| fromhex(substr(ip, 7, 2)), | |
| fromhex(substr(ip, 5, 2)), | |
| fromhex(substr(ip, 3, 2)), | |
| fromhex(substr(ip, 0, 2))) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment