Scenario: Create a ssh tunnel from an existing ssh connection
Requirement: forward.py. Please get the forward.py from paramiko’s repository
Example 1

from forward import forward_tunnel
## connect to remote host1
c = $.connect('user1@host1',password='123456')
## create the tunnel from localhost:8080 to host2:443 via host1:22
forward_tunnel(8080,'host2',443,c.client.get_transport())
The above example listens on all IP addresses of localhost. If you’d like it to listen on 127.0.0.1 only, you can use the following code:
from forward import ForwardServer, Handler
## connect to remote host1
c = $.connect('user1@host1',password='123456')
## create the tunnel from localhost:8080 to host2:443 via host1:22
class SubHander(Handler):
## hostname or ip of host2
chain_host = 'host2'
chain_port = 443
SubHander.ssh_transport = c.client.get_transport()
ForwardServer(('127.0.0.1',8080), SubHander).serve_forever()
## now, you can open https://localhost:8080
## which would be the web server on host2
Example 2
For security reason, if host2:443 is listening on 127.0.0.1, or only host2:22 is available for host1. you can do it by nested ssh as below: 
from forward import forward_tunnel
## connect to remote host1
$.connect('user1@host1',password='123456')
## connect to remote host2
c = $.connect('user2@host2',password='654321')
## create the tunnel from localhost:8080 to host2:443 via both host1:22 and host2:22
forward_tunnel(8080,'host2',443,c.client.get_transport())
The above example listens on all IP addresses of localhost. If you’d like it to listen on 127.0.0.1 only, you can use the following code:
from forward import ForwardServer, Handler
## connect to remote host1
$.connect('user1@host1',password='123456')
## connect to remote host2
c = $.connect('user2@host2',password='654321')
## create the tunnel from localhost:8080 to host2:443 via both host1:22 and host2:22
class SubHander(Handler):
chain_host = '127.0.0.1'
chain_port = 443
SubHander.ssh_transport = c.client.get_transport()
ForwardServer(('127.0.0.1',8080), SubHander).serve_forever()
## now, you can open https://localhost:8080
## which would be the web server on host2
Example 3, is same as the example 1 by “with $.connect” syntax.
from forward import ForwardServer, Handler
class SubHander(Handler):
## hostname or ip of host2
chain_host = 'host2'
chain_port = 443
## connect to remote host1
with $.connect('user1@host1',password='123456') as c:
## create the tunnel from localhost:8080 to host2:443 via host1:22
SubHander.ssh_transport = c.client.get_transport()
ForwardServer(('127.0.0.1',8080), SubHander).serve_forever()
## now, you can open https://localhost:8080
## which would be the web server on host2
Last Updated: 2026-07-25 16:59:40
Table of contents
- SSHScript v3.1 Documentation
- Accounts (legacy URL)
- Checking the syncing status of two MySQL servers
- Getting Started with SSHScript
- Page not found
- Q: What can it do that Netmiko doesn’t do? (origin)
- SSHScript Documentation
- SSHScript Documents
- SSHScript v2.0 CLI(Draft)
- SSHScript v2.0 Commands Execution and Console
- SSHScript v2.0 Console of sudo and su
- SSHScript v2.0 Getting Started
- SSHScript v2.0 Interactive Commands and Foreground Commands
- SSHScript v2.0 Make Connections
- SSHScript v2.0 Recipes
- SSHScript v2.0 Reference Guide
- SSHScript v2.0 Stdout, Stderr and Exitcode
- SSHScript v2.0 Threading Support
- SSHScript v2.0 Upload and Download Files
- SSHScript v2.0.2 Release Notes
- SSHScript v3.0 Interactive and Foreground Commands
- SSHScript v3.0 Reference Guide
- SSHScript v3.0.0 Release Notes
- SSHScript v3.1
- SSHScript vs. Ansible
- 用 Python 作系統自動化