I've found a really cool way to give a one-time access to another user.. by sharing your own session.
This solution builds on bash coprocesses. The idea is:
- you
ssh
into the server using your credentials
- start a
nc
server listing on port 2222
- connect the two sessions: whenever anyone connects to 2222, they actually send commands to the opened ssh session
- publish your port 2222 to the internet using the same ssh client
Follow me :)
Public an open SSH session to port 2222
Start an ssh coprocess and publish its i/o on your local port 2222:
$ coproc ssh user@server -tt
$ eval "exec nc -kl 0.0.0.0 2222 <&${COPROC[0]} >&${COPROC[1]}"
Install [ngrok]. Grab an authtoken from their website:
$ npm install ngrok -g
$ ngrok authtoken <token> # grab it from their website:
Publish your port 2222 to the Internet:
$ ngrok tcp 2222
Forwarding tcp://0.tcp.ngrok.io:16135 -> localhost:2222
Now tell your friend to connect to the server using telnet:
$ telnet 0.tcp.ngrok.io 16135
Don't keep the connection open for too long; it's not secure at all! :)
Improvement: co-operation
Now let's watch what your friend is doing on that server using tmux shared sessions. Like this:
$ ssh user@host -t -- tmux new -As shared-session
this starts a named session that everyone can connect to. Let's use it in our scenario:
$ coproc ssh user@server -tt -- tmux new -As shared-session
$ eval "exec nc -kl 0.0.0.0 2222 <&${COPROC[0]} >&${COPROC[1]}"
$ ngrok tcp 2222
Forwarding tcp://0.tcp.ngrok.io:16135 -> localhost:2222
now you connect to this session to watch what people are doing there:
$ ssh user@server -t -- tmux new -As shared-session
now tell your friend to connect to it
$ telnet 0.tcp.ngrok.io 16135
Watch carefully :) As he starts typing rm -rf /*
, kill the first terminal window!
Improvement: auto-restart
If the session quits, you might want to auto-restart it. Put the whole coproc && eval
thing into a while true ; do ... done
loop