5

I am trying to connect to a database that needs proxy (socks) to be able to connect, if I use the proxy connection manually, I can connect, but I need to make the script connect to the proxy (socks) of the machine to make this SELECT

SCRIPT

import socket
import socks
import requests
import pymssql

socks.set_default_proxy(socks.SOCKS5, "138.34.133.155", 1080, True, 'user','password')
socket.socket = socks.socksocket

server = '172.43.56.89'
username = 'user'
password = 'password'
database = 'dbname'

conn = requests.get(pymssql.connect(host=server,user=username,password=password,database=database))

cursor = conn.cursor()

cursor.execute("SELECT column FROM table")

row = cursor.fetchall()

conn.close()

for i in row:
    print(i)

OUTPUT

Traceback (most recent call last): File "connection.py", line 15, in conn = requests.get(pymssql.connect(host=server,user=username,password=password,database=database)) File "src\pymssql.pyx", line 642, in pymssql.connect pymssql.OperationalError: (20009, 'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (172.43.56.89:1433)\nNet-Lib error during Unknown error (10060)\n')

Luis Henrique
  • 526
  • 2
  • 17

2 Answers2

1

I think an option is to mount a local tunnelling sock with port forwarding, to map your database port and act as if your server where a localhost one.

It's really efficient if you're running your python script on a Unix computer.

Something like this system call (for a 3306 mariaDB) :

ssh -L 3306:localhost:3306 user@x.x.x.x

First, your run SSH, then, you tell him to enable a port forwarding from the 3306 port to the localhost:3306 port of the server you connect through user@IP.

With this, every query from your local machine:3306 will by send to your MariaDB:3306 server, allowing you to use it as if you where on the server.

Blag
  • 5,394
  • 2
  • 18
  • 43
  • socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "ip", 1080, True, 'user','password') socket.socket = socks.socksocket socket.socket(os.system('ssh ip')) – Luis Henrique Oct 29 '19 at 12:16
  • Returned error: socket.error: [Errno 10047] An address incompatible with the requested protocol was used – Luis Henrique Oct 29 '19 at 12:17
  • @LuisHenrique I don't speech of a proxy, but directly as a tunnelling through a [system command](https://stackoverflow.com/questions/89228/calling-an-external-command-from-python) that run in background. – Blag Oct 29 '19 at 13:11
  • Okay, I get your idea, but I can't write it :/ – Luis Henrique Oct 29 '19 at 13:12
  • @LuisHenrique are you on a unix/linux computer for the python (it's a server, a client or else?) ? If it's on windows, it'll probably be harder :/ – Blag Oct 29 '19 at 13:53
  • I'm on a Red Hat linux machine – Luis Henrique Oct 29 '19 at 13:55
  • 1
    @LuisHenrique ok nice~; so, just try to run by hand in a console `ssh -L 3306:localhost:3306 user@x.x.x.x` and change the port for the DB one, then try to run your `.py` as if your db was on your localhost, without proxy. – Blag Oct 29 '19 at 14:21
  • Ok, I will do the tests and add here, thank you very much! – Luis Henrique Oct 29 '19 at 14:33
  • @LuisHenrique Not very clear about this solution, could you post your code? – yayayahei Jun 10 '20 at 12:53
0

If you do not want to hack into pymssql source code, there're external tools that redirect all TCP traffic over the socks proxy, such as FreeCap for Windows, RedSocks for Linux and Proximac for macOS.

Arnie97
  • 905
  • 5
  • 17