Unix Shell - Script to locate all ssh forwarded ports

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > Script to locate all ssh forwarded ports





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Script to locate all ssh forwarded ports
Allistar

2007-01-23, 7:31 am

Hi there,
I would like to display all local ports that have been forwarded using ssh
(so they can be displayed on a desktop widget).

ssh port forwards are commands that always start with either "ssh -L"
or "sudo ssh -L". Here's an example:

sudo ssh -L 80:192.168.127.2:80 -L 2060:192.168.127.124:5900 -p
65456 -i /home/allistar/.ssh/id_rsa allistar@somehost.com cat -

The above command represents two forwarded ports.

I would like to extract out of that this:

80:192.168.127.2:80
2060:192.168.127.124:5900

The below script works fine, but I'm hoping there's a better way of
achieving this.

ps -eo "args" | egrep "^ssh -L|^sudo ssh -L" | sed "s/-L /\n-L \n/g" |
egrep -A 1 "\-L" | egrep -v "^\-" | sed -e 's/ .*//' -e '/^$/d'

Does anyone have any better suggestions that the above script?

Ideally (but not essential) I'd like to associate each forwarded port to the
host that is being connected too.

Thanks,
Allistar.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com