https://github.com/pertsevds/python_ssh_tunnel
Ports and sockets forwarding tunnel over SSH with systems SSH client.
https://github.com/pertsevds/python_ssh_tunnel
Last synced: 9 months ago
JSON representation
Ports and sockets forwarding tunnel over SSH with systems SSH client.
- Host: GitHub
- URL: https://github.com/pertsevds/python_ssh_tunnel
- Owner: pertsevds
- License: apache-2.0
- Created: 2022-09-30T08:37:01.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-30T05:10:21.000Z (about 3 years ago)
- Last Synced: 2025-01-26T04:42:55.792Z (11 months ago)
- Language: Python
- Size: 18.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python_ssh_tunnel
Ports and sockets forwarding tunnel over SSH with systems SSH client.
You can use it like this to tunnel MySQL over SSH with TCP ports:
```python
import pymysql
from ssh_tunnel import create_ssh_tunnel
with create_ssh_tunnel(
hostname="my_db_server.com",
local_socket="127.0.0.1:3307",
remote_socket="127.0.0.1:3306",
) as tunnel:
with pymysql.connect(
host="127.0.0.1",
port=3307,
user="user",
password="password",
database="database",
cursorclass=pymysql.cursors.DictCursor,
) as connection:
with connection.cursor() as cursor:
sql = "SELECT * FROM `table`"
cursor.execute(sql)
result = cursor.fetchone()
print(result)
```
or you can use it with unix sockets:
```python
import pymysql
from ssh_tunnel import create_ssh_tunnel
with create_ssh_tunnel(
hostname="my_db_server.com",
local_socket="/tmp/mysql-local.sock",
remote_socket="/tmp/mysql.sock",
) as tunnel:
with pymysql.connect(
unix_socket="/tmp/mysql-local.sock",
user="user",
password="password",
database="database",
cursorclass=pymysql.cursors.DictCursor,
) as connection:
with connection.cursor() as cursor:
sql = "SELECT * FROM `table`"
cursor.execute(sql)
result = cursor.fetchone()
print(result)
```