Bash script to set up a temporary SSH tunnel

前端 未结 6 1036
你的背包
你的背包 2020-12-12 08:46

On Cygwin, I want a Bash script to:

  1. Create an SSH tunnel to a remote server.
  2. Do some work locally that uses the tunnel.
  3. Then shut down the
6条回答
  •  孤城傲影
    2020-12-12 09:43

    I prefer to launch a new shell for separate tasks and I often use the following command combination:

      $ sudo bash; exit
    

    or sometimes:

      $ : > sensitive-temporary-data.txt; bash; rm -f sensitive-temporary-data.txt; exit
    

    These commands create a nested shell where I can do all my work; when I'm finished I hit CTRL-D and the parent shell cleans up and exits as well. You could easily throw bash; into your ssh tunnel script just before the kill part so that when you log out of the nested shell your tunnel will be closed:

    #!/bin/bash
    ssh -nNT ... &
    PID=$!
    bash
    kill $PID
    

提交回复
热议问题