Skip to content

Instantly share code, notes, and snippets.

@avescodes
Last active November 8, 2016 13:17
Show Gist options
  • Save avescodes/a241a287f0bab43fba1e to your computer and use it in GitHub Desktop.
Save avescodes/a241a287f0bab43fba1e 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 "$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