Skip to content

Instantly share code, notes, and snippets.

@meoow
Created August 4, 2014 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoow/ef197d10d8b072077638 to your computer and use it in GitHub Desktop.
Save meoow/ef197d10d8b072077638 to your computer and use it in GitHub Desktop.
Show current listening ports and established connections of system
#!/bin/bash
if [[ $1 =~ (tcp|udp) ]] || [ -z "$1" ];then
pass
else
echo Wrong option!
exit 1
fi
trap 'tput clear;exit 0' 2
col=$(tput cols)
while true; do
tput clear
lsof -nPi4$1 |awk 'NR>1{split($9,ip,"->")
split(ip[1],ip1,":")
split(ip[2],ip2,":")
printf("%-10s%5s %-15s%1s%5s %1s %-15s%1s%5s %s\n",$1,$2,ip1[1],(NR>1?":":""),ip1[2],ip[2]!=""?">":"",ip2[1],length(ip2)>1?":":"",ip2[2],$10)
}'
tput cup 0 $col
sleep 3
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment