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

Lista de túneles SSH abiertos

Advertisement

Utilizo un montón de túneles SSH a varios servidores en mi máquina linux (para hacer túneles a bases de datos, servidores web, etc.) y sería muy útil ver una lista de los túneles abiertos actuales a través de un script de shell.

Puedo identificar las conexiones locales a través de un grep en netstat a lo largo de las líneas de:

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

pero esto no me muestra el puerto remoto al que está conectado (y obviamente incluye las conexiones SSH estándar que no tienen túnel)

UPDATE : Las respuestas están bien pero no me muestran el puerto remoto al que estoy conectado. Por ejemplo, a menudo tengo un túnel a través de mysql, digamos localhost:3308 mapeando a :3306 en el servidor. Normalmente puedo adivinar por los puertos locales que he elegido, pero sería bueno tener acceso a ambos.

¿Alguna idea?

Advertisement
Advertisement

Respuestas (9)

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

si sólo quiere listar los túneles creados por 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)

(eso sería un túnel -L 9090:localhost:80)

si quiere ver los túneles / conexiones hechas 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)

el ssh-daemon escucha en el puerto 22 (última línea), se generan 2 subprocesos (primeras 2 líneas, login de ‘user’), un túnel -R creado en el puerto 5000, y un túnel -L que reenvía un puerto de mi máquina (local) a localhost:80 (www).

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

no es exactamente la solución a tu problema, pero también es útil a veces:

Desde dentro de una sesión ssh:

  1. presione enter
  2. escriba ~ y luego #

le muestra una lista de todas las conexiones abiertas sobre sus túneles para esa sesión.

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

Prueba este comando, puede ser útil:

ps aux | grep ssh
7
7
7
2011-02-21 12:07:47 +0000
netstat -tpln | grep ssh
  • t: TCP
  • p: mostrar proceso
  • l: escuchar
  • n: valores numéricos

EDIT: ejemplo para el comentario de @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

Que se puede leer como: SSH (no SSHd) está escuchando el puerto TCP local 1443.

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

Este es el primer resultado de google para esta pregunta, así que pondré mi respuesta aquí. Me quedé toda la noche filtrando los resultados, y se me ocurrió un comando largo y complejo que muestra sólo sus túneles ssh inversos en este formato:

publicipaddress:remoteforwardedport

Aquí está el código, estoy corriendo Ubuntu Server 12. Estoy ejecutando túneles ssh inversos que reenvían el puerto local 5900 a mi servidor ssh público, y este ingenioso comando muestra todas mis direcciones ip públicas con el puerto remoto.

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

Salida de muestra:

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

Como no me gusta lsof, sugiero un método alternativo (me lo enseñó otro chico :)):

$ netstat -l | grep ssh

De esta forma se muestran los túneles ssh creados por ssh que se abren en modo LISTEN (y que son omitidos por defecto por netstat).

Advertisement

Preguntas relacionadas

6
10
19
12
5
Advertisement