Skip to content

Instantly share code, notes, and snippets.

@itsthatguy
Forked from avescodes/ports.sh
Created July 8, 2014 21:11
Show Gist options
  • Save itsthatguy/d097b4cb304407a4858c to your computer and use it in GitHub Desktop.
Save itsthatguy/d097b4cb304407a4858c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
pattern=$1
PORTS=""
if [ -z "$pattern" ]; then
PORTS=$(lsof -iTCP -sTCP:LISTEN)
else
PORTS=$(lsof -iTCP -sTCP:LISTEN | grep -i "$pattern")
fi
i=0
echo "<?xml version='1.0'?><items>"
while read -r line; do
if [ $i -gt 0 ]; then
x=($line)
boop=$(cut -d ":" -f 2 <<< "${x[8]}")
echo "<item uid='${x[1]}' arg='' valid='yes'>
<title>${x[0]}: ${boop}</title>
<subtitle>PID: ${x[1]}</subtitle>
<icon>icon_port.png</icon>
</item>"
fi
((i++))
done <<<"$PORTS"
echo '</items>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment