From: Matthias Kruk Date: Mon, 26 Jul 2021 02:40:33 +0000 (+0900) Subject: include/ssh: Add functions to list running tunnels and proxies X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=1a7834717231b797e102cffb8f4f5b26bf34ab73;p=toolbox-goodies include/ssh: Add functions to list running tunnels and proxies The ssh module does not provide any means to figure out what tunnels and proxies have been established, making it hard to automate ssh connections. This commit adds the ssh_tunnel_list() and ssh_proxy_list() functions which may be used to list the established tunnels and proxies, respectively. --- diff --git a/include/ssh.sh b/include/ssh.sh index 148ad5f..36d7696 100755 --- a/include/ssh.sh +++ b/include/ssh.sh @@ -56,19 +56,18 @@ _ssh_tunnel_ctrl_socket_name() { } _ssh_proxy_ctrl_socket_name() { - local host - local port + local proxy="$1" + local user="$2" + local host="$3" + local port="$4" local sockdir - host="$1" - port="$2" - if ! sockdir=$(_ssh_get_socket_dir); then return 1 fi - echo "$sockdir/proxy-$host-$port.sock" + echo "$sockdir/proxy-$proxy-$user-$host-$port.sock" return 0 } @@ -167,7 +166,8 @@ ssh_proxy_open() { addrspec="$local_addr:$local_port" sshtarget="$proxy_user@$proxy_host" - if ! ctrl_sock=$(_ssh_proxy_ctrl_socket_name "$local_addr" "$local_port"); then + if ! ctrl_sock=$(_ssh_proxy_ctrl_socket_name "$proxy_host" "$proxy_user" \ + "$local_addr" "$local_port"); then return 1 fi @@ -202,3 +202,47 @@ ssh_close() { return 0 } + +ssh_tunnel_list() { + local sockdir + local socket + + if ! sockdir=$(_ssh_get_socket_dir); then + return 1 + fi + + while read -r socket; do + local tunnel + + tunnel="${socket##*/}" + tunnel="${tunnel%.sock}" + tunnel="${tunnel#tunnel-}" + + echo "${tunnel//-/ }" + done < <(find "$sockdir" -mindepth 1 -maxdepth 1 \ + -iname "tunnel-*.sock") + + return 0 +} + +ssh_proxy_list() { + local sockdir + local socket + + if ! sockdir=$(_ssh_get_socket_dir); then + return 1 + fi + + while read -r socket; do + local proxy + + proxy="${socket##*/}" + proxy="${proxy%.sock}" + proxy="${proxy#proxy-}" + + echo "${proxy//-/ }" + done < <(find "$sockdir" -mindepth 1 -maxdepth 1 \ + -iname "proxy-*.sock") + + return 0 +}