|
import paramiko |
|
import pymysql |
|
from sshtunnel import SSHTunnelForwarder |
|
|
|
|
|
ssh_host = '129.159.146.88' |
|
ssh_user = 'ubuntu' |
|
ssh_key_path = 'C:/Users/kerts/OneDrive/Documents/Keys/Ubuntu_Oracle/ssh-key-2023-02-12.key' |
|
|
|
|
|
mysql_host = 'localhost' |
|
mysql_port = 3306 |
|
mysql_user = 'root' |
|
mysql_password = 'naP2tion' |
|
mysql_db = 'warbot' |
|
|
|
|
|
ssh_client = paramiko.SSHClient() |
|
ssh_client.load_system_host_keys() |
|
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
|
private_key = paramiko.RSAKey.from_private_key_file(ssh_key_path) |
|
|
|
|
|
with SSHTunnelForwarder( |
|
(ssh_host, 22), |
|
ssh_username=ssh_user, |
|
ssh_pkey=private_key, |
|
remote_bind_address=(mysql_host, mysql_port)) as tunnel: |
|
|
|
|
|
mysql_conn = pymysql.connect( |
|
host='localhost', |
|
port=tunnel.local_bind_port, |
|
user=mysql_user, |
|
password=mysql_password, |
|
db=mysql_db |
|
) |
|
|
|
|
|
with mysql_conn.cursor() as cursor: |
|
query = 'SELECT * FROM conversations WHERE username = "user1";' |
|
cursor.execute(query) |
|
rows = cursor.fetchall() |
|
for row in rows: |
|
print(row) |
|
|
|
|
|
mysql_conn.close() |
|
|
|
|
|
ssh_client.close() |
|
|
|
|