Search code examples
bashssh

How to create a tunnel after ssh to remote machine using private key using bash script


I have a server where i will be logging into the server from my local machine and create a tunnel.

I have a bash script which is not creating tunnel

sshpass -p ${1} ssh ${2}@${3}
ssh -L <port1>:<host>:80 -i /home/<user>/private_key <user_ID>@<host2>

Result i am getting -

sh ssh_to_box.sh pwd username remotehost
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-145-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

144 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***
Last login: Thu May 30 09:36:36 2019 from remotehost
$ 

Script is not doing tunneling. Also it should do $bash

How to ssh to remote and create tunnel from that remote machine and keep the tunnel alive?


Solution

  • Solved the issue:

    sshpass -p ${1} ssh -t ${2}@${3} 'ssh -L '${4}':'${5}':'${6}' -i /home/<Path>_key '${2}'@'${7}''