Skip to content

Instantly share code, notes, and snippets.

@a-nldisr
Last active January 19, 2022 11:41
Show Gist options
  • Save a-nldisr/340f1f3a9cf01c20a7a5687a76ba8cdd to your computer and use it in GitHub Desktop.
Save a-nldisr/340f1f3a9cf01c20a7a5687a76ba8cdd to your computer and use it in GitHub Desktop.
# Some containers do not provide netstat or lsof. This command will get you the local IP a session is established on
# In short: This gets the output from namespace 1 (where usually the process is running inside the container), filters out the field where the hex IP is located and converts it into a readable IP in the right format. (some fields are inverted etc)
# Benefit is that you dont need netstat installed to see connections.
printf '%d.%d.%d.%d\n' $(grep -v local /proc/1/net/tcp | awk '{print $2}'|cut -d: -f1| sed -r 's/(..)/0x\1 /g') | awk -F. '{for (i=NF; i>0; --i) printf "%s%s", (i<NF ? "." : ""), $i; printf "\n"}'
# This lists the connections that are established.
printf '%d.%d.%d.%d\n' $(grep -v rem /proc/1/net/tcp | awk '{print $3}'|cut -d: -f1| sed -r 's/(..)/0x\1 /g') | awk -F. '{for (i=NF; i>0; --i) printf "%s%s", (i<NF ? "." : ""), $i; printf "\n"}'
# It could be shorter..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment