| function is_listening() { | |
| ss -ln | grep :8022 | |
| } | |
| function get_sshd_pids() { | |
| echo $(sudo lsof -i 2>/dev/null | sed -nr 's/^sshd\s+([0-9]+).*localhost:8022.*$/\1/p' | sort | uniq) | |
| } | |
| function main() { | |
| if [[ $(is_listening) ]]; then | |
| echo "Something is listening on port 8022" | |
| echo "===============================" | |
| echo "|About to prompt user for sudo|" | |
| echo "===============================" | |
| local pids | |
| pids=$(get_sshd_pids) | |
| echo "Found sshd PIDS to kill: ${pids}" | |
| echo $pids | xargs kill -9 | |
| else | |
| echo "No processes listening on 8022. Nothing to cleanup" | |
| fi | |
| } | |
| main |