Created
November 20, 2012 18:19
-
-
Save vjt/4119806 to your computer and use it in GitHub Desktop.
Lists all open IPv4 sockets allocated by the current user's running processes whose command line matches the regex passed as the first parameter.
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
#!/bin/sh | |
# | |
# Lists all open IPv4 sockets allocated by the current user's | |
# running processes whose command line matches the regex passed | |
# as the first parameter. Uses "unicorn.*worker" by default - | |
# because we all love unicorns! :-) | |
# | |
# - [email protected] Tue Nov 20 19:06:08 CET 2012 | |
# | |
# Get process from the first argument | |
PROC=$1 | |
USER=$(whoami) | |
DEFP="unicorn.*worker" | |
case "$PROC" in | |
-h|--help|--usage) | |
echo "Displays the IPv4 sockets opened by the current user's" | |
echo "processes matching the given regexp (default: \\"$DEFP\\")" | |
echo "Usage: $0 [process regex]" | |
echo | |
exit 1 | |
;; | |
esac | |
[ -z "$PROC" ] && PROC=$DEFP | |
# Wrap the first letter of the regexp within square brackets, to | |
# avoid `grep` itself coming out in the output. | |
PROC=$(echo $PROC | sed -r 's#(^.)(.*)#[\\1]\\2#') | |
# Get the PIDs matching the resulting regex. | |
PIDS=$(ps -U $USER -o pid,cmd | grep "$PROC" | cut -f1 -d' ' | tr '\\n' ',') | |
# Run! | |
lsof -nP -p $PIDS -a -i4 2>/dev/null | |
# Forward the exit status | |
exit $! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment