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

開いているSSHトンネルをリストアップ

Advertisement

私は Linux マシン上の様々なサーバへの SSH トンネルをたくさん使っています (データベースやウェブサーバなどへのトンネル用)。

netstat上で以下のようにgrepすることでローカル接続を識別することができます。

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

しかし、これでは接続先のリモートポートを表示してくれません(そして明らかにトンネル化されていない標準的な SSH 接続も含まれています)

UPDATE . 回答は問題ありませんが、接続しているリモートポートを表示してくれません。例えば、私はよくmysqlにトンネルで接続していて、サーバ上ではlocalhost:3308が:3306にマッピングされていることがあります。通常は選択したローカルポートから推測することができますが、両方にアクセスできるようになるといいですね。

何かアイデアはありますか?

Advertisement
Advertisement

回答 (9)

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

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)

(これは -L 909090:localhost:80 トンネルになります)

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)

ssh-daemon がポート 22 (最終行) で listen し、2 つのサブプロセスが生成され (最初の 2 行、'user’ のログイン)、ポート 5000 で作成された -R トンネル、および私の (ローカルの) マシンから localhost:80 (www) にポートを転送する -L トンネルが生成されます。

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

これで問題が解決するわけではありませんが、たまには便利です。

ssh セッション内から:

  1. Enter
  2. ~ と入力して #

は、そのセッションのトンネル上で開いているすべての接続のリストを表示します。

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

このコマンドを試してみてください。

ps aux | grep ssh
7
7
7
2011-02-21 12:07:47 +0000
netstat -tpln | grep ssh
  • t. TCP
  • p: プロセスの表示
  • l: リスニング
  • n: 数値

EDIT: @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

と読めます。SSH (SSHdではありません) はローカルの TCP ポート 1443 を聞いています。

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

これはこの質問のgoogleのトップの結果なので、私の答えをここに書いておきます。私は徹夜で結果をフィルタリングして、この形式の逆引きsshトンネルだけを表示する長い複雑なコマンドを思いつきました。

publicipaddress:remoteforwardedport

ここにコードがあります。私はローカルポート5900を私の公開sshサーバに転送する逆sshトンネルを実行していますが、この気の利いたコマンドは、リモートポートを持つすべての私の公開IPアドレスを表示します。

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

サンプル出力。

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

lsofが好きではないので、別の方法を提案します(別の人に教えてもらいました :)。

$ netstat -l | grep ssh

この方法では、LISTEN モードで開かれている (ssh によってデフォルトでは省略されている) netstat によって作成された ssh トンネルを表示します。

Advertisement

関連する質問

6
10
19
12
8
Advertisement