2011-02-21 11:09:34 +0000 2011-02-21 11:09:34 +0000
71
71
Advertisement

Elencare i tunnel SSH aperti

Advertisement

Uso un sacco di tunnel SSH verso vari server sulla mia macchina linux (per il tunnelling a database, server web, ecc.) e sarebbe davvero comodo visualizzare un elenco di tunnel attualmente aperti tramite uno script di shell.

Posso identificare le connessioni locali tramite un grep su netstat lungo le linee di:

netstat -n --protocol inet | grep ':22'

ma questo non mi mostra la porta remota a cui è connesso (e ovviamente include connessioni SSH standard che non sono tunnel)

UPDATE : Le risposte vanno bene ma non mi mostrano la porta remota a cui sono connesso. Ad esempio ho spesso un tunnel verso mysql, diciamo localhost:3308 che mappano a :3306 sul server. Normalmente posso indovinare dalle porte locali che ho scelto, ma sarebbe bello avere accesso ad entrambe.

Qualche idea?

Advertisement
Advertisement

Risposte (9)

82
82
82
2011-02-21 11:22:02 +0000

se vuoi elencare solo i tunnel creati da ssh:

% sudo lsof -i -n | egrep '\<ssh\>'
ssh 19749 user 3u IPv4 148088244 TCP x.x.x.x:39689->y.y.y.y:22 (ESTABLISHED)
ssh 19749 user 4u IPv6 148088282 TCP [::1]:9090 (LISTEN)
ssh 19749 user 5u IPv4 148088283 TCP 127.0.0.1:9090 (LISTEN)

(che sarebbe un tunnel -L 9090:localhost:80)

se vuoi vedere i tunnel / connessioni fatte a un sshd:

% sudo lsof -i -n | egrep '\<sshd\>'
sshd 15767 root 3u IPv4 147401205 TCP x.x.x.x:22->y.y.y.y:27479 (ESTABLISHED)
sshd 15842 user 3u IPv4 147401205 TCP x.x.x.x:22->y.y.y.y:27479 (ESTABLISHED)
sshd 15842 user 9u IPv4 148002889 TCP 127.0.0.1:33999->127.0.0.1:www (ESTABLISHED)
sshd 1396 user 9u IPv4 148056581 TCP 127.0.0.1:5000 (LISTEN)
sshd 25936 root 3u IPv4 143971728 TCP *:22 (LISTEN)

il demone ssh ascolta sulla porta 22 (ultima linea), vengono generati 2 sottoprocessi (prime 2 linee, login di ‘user’), un tunnel -R creato sulla porta 5000, e un tunnel -L che inoltra una porta dalla mia macchina (locale) a localhost:80 (www).

16
16
16
2013-07-08 21:45:10 +0000

Non esattamente la soluzione per il tuo problema, ma anche utile a volte:

Dall'interno di una sessione ssh:

  1. premi invio
  2. scrivi ~ e poi #

ti mostra un elenco di tutte le connessioni aperte sui tuoi tunnel per quella sessione.

16
Advertisement
16
16
2012-11-03 07:29:20 +0000
Advertisement

Provate questo comando, potrebbe essere utile:

ps aux | grep ssh
7
7
7
2011-02-21 12:07:47 +0000
netstat -tpln | grep ssh
  • t: TCP
  • p: show process
  • l: listening
  • n: valori numerici

EDIT: esempio per il commento di @akira:

(header added, tested on Debian wheezy)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:1443 0.0.0.0:* LISTEN 4036/ssh

Che può essere letto come: SSH (non SSHd) è in ascolto sulla porta TCP 1443 locale.

5
Advertisement
5
5
2014-08-29 12:09:52 +0000
Advertisement

Questo è il primo risultato di google per questa domanda, quindi metterò la mia risposta qui. Sono stato sveglio tutta la notte per filtrare i risultati, e sono arrivato a un lungo e complesso comando che mostra solo i tuoi tunnel ssh inversi in questo formato:

publicipaddress:remoteforwardedport

Ecco il codice, sto eseguendo Ubuntu Server 12. Sto eseguendo tunnel ssh inversi che inoltrano la porta locale 5900 al mio server ssh pubblico, e questo ingegnoso comando mostra tutti i miei indirizzi ip pubblici con la porta remota.

sudo lsof -i -n | egrep '\<sshd\>' | grep -v ":ssh" | grep LISTEN | sed 1~2d | awk '{ print $2}' | while read line; do sudo lsof -i -n | egrep $line | sed 3~3d | sed 's/.*->//' | sed 's/:......*(ESTABLISHED)//' | sed 's/.*://' | sed 's/(.*//' | sed 'N;s/\n/:/' 2>&1 ;done
2
2
2
2019-05-15 23:10:05 +0000
report_local_port_forwardings() {

  # -a ands the selection criteria (default is or)
  # -i4 limits to ipv4 internet files
  # -P inhibits the conversion of port numbers to port names
  # -c /regex/ limits to commands matching the regex
  # -u$USER limits to processes owned by $USER
  # http://man7.org/linux/man-pages/man8/lsof.8.html
  # https://stackoverflow.com/q/34032299

  echo 
  echo "LOCAL PORT FORWARDING"
  echo
  echo "You set up the following local port forwardings:"
  echo

  lsof -a -i4 -P -c '/^ssh$/' -u$USER -s TCP:LISTEN

  echo
  echo "The processes that set up these forwardings are:"
  echo

  ps -f -p $(lsof -t -a -i4 -P -c '/^ssh$/' -u$USER -s TCP:LISTEN)

}

report_remote_port_forwardings() {

  echo 
  echo "REMOTE PORT FORWARDING"
  echo
  echo "You set up the following remote port forwardings:"
  echo

  ps -f -p $(lsof -t -a -i -c '/^ssh$/' -u$USER -s TCP:ESTABLISHED) | awk '
  NR == 1 || /R (\S+:)?[[:digit:]]+:\S+:[[:digit:]]+.*/
  '
}

report_local_port_forwardings
report_remote_port_forwardings

Uscita campione:

LOCAL PORT FORWARDING

You set up the following local port forwardings:

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 10086 user 7u IPv4 1924960 0t0 TCP localhost:2301 (LISTEN)
ssh 10086 user 9u IPv4 1924964 0t0 TCP localhost:2380 (LISTEN)
ssh 10086 user 11u IPv4 1924968 0t0 TCP localhost:2381 (LISTEN)

The processes that set up these forwardings are:

UID PID PPID C STIME TTY TIME CMD
user 10086 7074 0 13:05 pts/21 00:00:00 ssh -N ssh.example.com

REMOTE PORT FORWARDING

You set up the following remote port forwardings:

UID PID PPID C STIME TTY STAT TIME CMD
user 7570 30953 0 11:14 pts/18 S 0:00 ssh -N -R 9000:localhost:3000 ssh.example.com
0
Advertisement
0
0
2014-02-05 12:43:49 +0000
Advertisement
/sbin/ip tunnel list # replacement for the deprecated iptunnel command
0
0
0
2014-10-21 09:16:20 +0000
#!/bin/csh -f echo SSH Tunnels Connected echo foreach f (`netstat -an -p | grep tcp | grep sshd | grep -v :: | grep -v 0:22 | grep LISTEN | cut -d" " -f45- | cut -d"/" -f1`) set ip = `netstat -an -p | grep tcp | grep sshd | grep -v :: | grep -v 0:22 | grep ESTABLISH | grep $f | cut -d" " -f20- | cut -d":" -f1` #set h = `grep -a "$ip" /htdocs/impsip.html | grep br | cut -d" " -f2` echo -n "$ip " echo `netstat -an -p | grep tcp | grep sshd | grep -v :: | grep -v 0:22 | grep LISTEN | grep $f | cut -d":" -f2 | cut -d" " -f1` #echo " $h" end
0
Advertisement
0
0
2017-06-23 10:03:19 +0000
Advertisement

Dato che non mi piace lsof, suggerisco un metodo alternativo (me l'ha insegnato un altro ragazzo :)):

$ netstat -l | grep ssh

In questo modo si mostrano i tunnel ssh creati da ssh che sono aperti in modalità LISTEN (e sono omessi di default da netstat).

Advertisement

Domande correlate

6
10
19
12
5
Advertisement