Skip to content

Instantly share code, notes, and snippets.

@ramonpin
Last active June 24, 2020 22:48
Show Gist options
  • Save ramonpin/6218637 to your computer and use it in GitHub Desktop.
Save ramonpin/6218637 to your computer and use it in GitHub Desktop.
Expect script to automatically create a ssh tunnel from a local port to a remote destination.
#!/usr/bin/expect -f
set ssh_host "172.19.10.80"
set ssh_user "user"
set ssh_password "password"
set dest_host "172.19.10.91"
set dest_port 8080
set local_port 80
set timeout -1
spawn ssh -N $ssh_user@$ssh_host -L 127.0.0.1:$local_port:$dest_host:$dest_port
expect -- "*?assword:*"
send $ssh_password
send "\r"
puts "\rTunnel stablished."
puts "\rEnter 'close' to finish tunneling."
expect_user -re "close"
@ramonpin
Copy link
Author

This can be expanded to create several tunnels by duplicating lines 10-13 and adding the proper configuration variables.

@zakstar
Copy link

zakstar commented Feb 26, 2020

@ramonpin You do not need to duplicate 10-13, all you need to do is repeat -L 127.0.0.1:$local_port:$dest_host:$dest_port on line 10 and it would set up multiple tunnels through a single session with no recursive sessions (which would happen with multiple 10-13 lines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment