pastebin - collaborative debugging tool
rovema.kpaste.net RSS


ssh tuntap #017
Posted by Anonymous on Thu 14th Apr 2022 14:40
raw | new post
modification of post by Anonymous (view diff)

  1. #!/usr/bin/ksh93
  2.  
  3. #
  4. # Ethernet/tap forwarding via ssh
  5. #
  6.  
  7. set -o xtrace
  8. set -o errexit
  9. set -o nounset
  10.  
  11. # list of commands our ssh session should execute
  12. typeset -a sshcmds=(
  13.         #
  14.         # cleanup:
  15.         #
  16.         # we need to clean up the bridge interface ourselves, if
  17.         # we do not do that we risk getting that any subsequent
  18.         # ifconfig/ip/etc command hangs
  19.         #
  20.         '(( trap_once=0 )) ;'
  21.         "netcleanup() { "
  22.                 "set +o errexit ;"
  23.                 '(( trap_once++ > 0 )) && return 0 ;'
  24.                 "brctl delif br19 tap19 ;"
  25.                 "brctl delif br19 eno1 ;"
  26.                 "ifconfig tap19 down ;"
  27.                 "ifconfig br19 down ;"
  28.                 "brctl delbr br19 ;"
  29.                 "exit 0 ;"
  30.         "} ;"
  31.         'trap netcleanup EXIT ;'
  32.         'trap netcleanup HUP ;'
  33.  
  34.         # tell NetworkManager NOT to touch the requested interface
  35.         'nonetmanage() { '
  36.                 'nmcli dev set "$1" managed no ;'
  37.         '} ;'
  38.  
  39.         "set -o xtrace ;"
  40.         "set -o errexit ;"
  41.  
  42.         "nonetmanage tap19 ;"
  43.         "nonetmanage eno1 ;"
  44.  
  45.         "brctl addbr br19 ;"
  46.         "nonetmanage br19 ;"
  47.         "brctl addif br19 tap19 ;"
  48.         "brctl addif br19 eno1 ;"
  49.         "brctl show ;"
  50.         "ifconfig tap19 promisc ;"
  51.         "ifconfig tap19 up ;"
  52.         "ifconfig br19 up ;"
  53.  
  54.         "read dummy ;" # wait
  55.  
  56.         # this is tricky (and nasty):
  57.         # we have to tell our session sshd instance to quit, and then
  58.         # busy-wait until we get SIGHUP from gthat sshd process back
  59.         # so our HUP trap handler function gets called
  60.         'kill -s INT ${PPID} ;'
  61.         'while true ; do sleep 0.1 ; done ;'
  62. )
  63.  
  64. ssh \
  65.         -o ExitOnForwardFailure=yes \
  66.         -o PermitLocalCommand=yes \
  67.         -o LocalCommand="ifconfig tap19 up" \
  68.         -o Tunnel=ethernet -w 19:19 \
  69.         root@10.49.20.202 \
  70.         "${sshcmds[*]}"
  71.  
  72. # cleanup
  73. # ...
  74.  
  75. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at