pahaz / sshtunnel

SSH tunnels to remote server.
MIT License
1.2k stars 184 forks source link

Using sshtunnel on Mac? #290

Open Rapid1898-code opened 3 months ago

Rapid1898-code commented 3 months ago

i try to access my mysql-database using a sshtunnel in python with the following code -

import mysql.connector
import sshtunnel

if __name__ == '__main__':
  with sshtunnel.SSHTunnelForwarder(
          ("xyz.a2hosting.com", 7822),
          ssh_username="myName",
          ssh_password="myPW",
          remote_bind_address=("127.0.0.1", 3306),
          local_bind_address=("0.0.0.0", 3306)
  ) as tunnel:
      mydb = mysql.connector.connect(
          user="myUser",
          password="myPW",
          host="127.0.0.1",
          database="myDB",
          port="3306")
      c = mydb.cursor()
  print(f"Access is working fine!")

This works fine on my windows computer without problems. (working directly when i run the code and also as an executable file creatd with pyinstaller)

But on my Mac i get the following error:

(openai) PolziMacs-Mini:colli239 polzimac$ python test1.py
2024-03-20 09:35:50,928| ERROR   | Password is required for key /Users/polzimac/.ssh/id_ed25519
2024-03-20 09:35:53,579| ERROR   | Problem setting SSH Forwarder up: Couldn't open tunnel 0.0.0.0:3306 <> 127.0.0.1:3306 might be in use or destination not reachable
Traceback (most recent call last):
  File "/Users/polzimac/Documents/DEV/Fiverr/TRY/colli239/test1.py", line 5, in <module>
    with sshtunnel.SSHTunnelForwarder(
  File "/Users/polzimac/Documents/DEV/venv/openai/lib/python3.9/site-packages/sshtunnel.py", line 1608, in __enter__
    self.start()
  File "/Users/polzimac/Documents/DEV/venv/openai/lib/python3.9/site-packages/sshtunnel.py", line 1344, in start
    self._raise(HandlerSSHTunnelForwarderError,
  File "/Users/polzimac/Documents/DEV/venv/openai/lib/python3.9/site-packages/sshtunnel.py", line 1174, in _raise
    raise exception(reason)
sshtunnel.HandlerSSHTunnelForwarderError: An error occurred while opening tunnels.

How can i run this also on my Mac-Computer?

(i would like to run this code without any additonal adjustements in the terminal - so the python-code and the executable program when created with pyinstaller should run on any computer without having to do anything on the computer in the terminal - like it is working on windows)

pahaz commented 1 month ago

Password is required for key /Users/polzimac/.ssh/id_ed25519

As I can see, you need to provide an SSH key password. And sshtunnel should work well on Mac.