I tried creating a SSH tunnel using
ssh -L 3306:localhost:22 <hostip>
Then running my python script to connect via localhost
conn = MySQLdb.connect(host'localhost', port=3306, user='bob', passwd='na', db='test')
However, I receive the following error
(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
How can I make sure I'm hitting the correct host and not just some problem with the bind?
Try changing "localhost" to "127.0.0.1", it should work as you expect. This behavior is detailed in the manual:
UNIX sockets and named pipes don't
work over a network, so if you specify
a host other than localhost, TCP will
be used, and you can specify an odd
port if you need to (the default port
is 3306):
db=_mysql.connect(host="outhouse", port=3307, passwd="moonpie", db="thangs")
If you really had to, you could
connect to the local host with TCP by
specifying the full host name, or
127.0.0.1.
Does mysqld run on port 22 on the remote? Call me ignorant but I think what you're trying to do is
ssh -n -N -f -L 3306:localhost:3306 remotehost
Then making MySQL connections on local machine will transparently get tunneled over to the target host.
You can't specify localhost as the hostname, as this suggests that MySQLdb should try to use a UNIX socket. Use 127.0.0.1 for the host instead.
If you want to make sure the connection works, you can use the standard mysql client.
Related
I'm using an SSH connection with Paramiko.
My code:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=remote_host, username=remote_user, password=remote_password,
port=remote_port)
How to send HTTP GET request from connected remote host (use it like a proxy)?
I've found solution according to the answer:
with SSHTunnelForwarder(
ssh_address_or_host=(remote_host, remote_port),
ssh_username=remote_user,
ssh_password=remote_password,
remote_bind_address=("www.python.org", 80),
) as tunnel:
conn = http.client.HTTPConnection("127.0.0.1", port=tunnel.local_bind_port)
conn.request("GET", '/')
There are two options:
External tool
Use any tool available on the SSH server that can send the HTTP request. E.g. curl or wget:
curl https://www.example.com/
And execute it using Paramiko: Python Paramiko - Run command
This solution is easier, but has the dependency on the command – So it's also platform dependent.
Port forwarding
Forward a local port to the remote HTTP server port 80 and connect to the forwarded port using your local Python code.
You will find lot of examples how to forward a database port. Like this one: Enable Python to Connect to MySQL via SSH Tunnelling
In your case, you need to do the same, just instead of using a database client to connect to the forwarded port, you connect an HTTP client (like HTTPConnection).
Also in most cases, the database forwarding usually ends on the SSH server itself (localhost/127.0.0.1), while you want to connect further.
This solution is more complicated, but without external dependencies – So it's platform independent. On the other hand, the port forwarding is a special privilege, that may be restricted on the server (but usually it is not). Before trying to implement it, you can test it with your SSH client.
To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in Pycharm only accepts one ssh tunnel.
Configure Remote Python Interpreter dialog box
Is there any way to set a multi-hop ssh to have access to the interpreter and data files on the host from local?
You can use port forwarding on ssh.
1. Open a terminal and run:
On your local system:
ssh -L 6000:<target_server_ip>:22 <proxy_server_user>#<proxy_server_ip>
You should be connected to the proxy now. You can substitute 6000 with any port.
2. (optional) Test
Now you can ssh into the target server on another terminal with:
ssh -p 6000 <target_server_user>#localhost
3. Configure PyCharm
Keep in mind not to close the first terminal!
The same goes for the PyCharm. Just set the remote interpreter connection through ssh with the following configuration:
host: localhost
port: 6000
user: target_server_user
PyCharm seemingly parses the local .ssh/config too.
If you already have configured ssh hopping there, you can just specify the target server in your pycharm ssh-config.
~/.ssh/config (source)
Host bastion
Hostname bastion.domain.com
Port 2222 # a non-standard port is a good idea
User ironicbadger
Host servera
Hostname servera.lan.local
User servera-user
ProxyCommand ssh bastion -W %h:%p
in pycharm:
Host servera, User name server-user
For users on ssh version 7.3 or later this can be simplified using the ProxyJump parameter.
Host bastion
Hostname bastion.domain.com
User bastion-user
Host servera
Hostname servera.lan.local
User servera-user
ProxyJump bastion
I launched an AWS linux instance and installed and ran mongo as instructed here. The mongo service is running and accepting connections on 27017. However, when I go to the server publik dns with port 27017 the server does not respond and I don't see the default mongo message.
I am trying to run a Python(Flask) server on another instance and trying to connect to the mongo server using the private ip, but the connection does not happen. I get this error message on the terminal :
pymongo.errors.ServerSelectionTimeoutError: xxx.xx.xx.xx:27017: [Errno
111] Connection refused
Is this not the right way to use mongo db on aws ? If this approach is feasible, what is causing the connection to not happen ?
All inputs appreciated, much thanks!
It is possible that your mongodb is configured to only accept connection from local host. Edit /etc/mongod.conf file to comment out the line that bindIP like in the example below -
# network interfaces
net:
port: 27017
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
I am connecting to a remote server's port 27017 using ssh and then accessing the mongo database on that system. I can successfully connect it via the shell script. However, when I write a python program and try to connect to that system, I am not able to connect. I use the following piece of code:
conn = MongoClient('mongodb://username:password#hostname:27017/database')
I would want to mention that I am accessing the destination system behind a proxy. However, the port 27017 is enabled for my system to connect to the destination system.
conn = MongoClient('mongodb://username:password#hostname:27017/database')
Your hostname is 27017 but the port you enabled is 27107
I have a simple xml rpc server running on a localhost. The server has the following address:
servAddr = ("localhost", 8080). When I run the client on a different machine I get a connection refused error. However, when i change the line
servAddr = ("localhost", 8080)
to
servAddr = ("myhostname", 8080)
everything works fine...
Why ? And how to know hostname in startup script for machine ?
'localhost' refers to your local machine. It's an alias to 127.0.0.1 ip address. It only works when the server and the client are running on the same machine.
When you try to put the client on an other machine, localhost refer to the machine of the client --> no the machine of the server !
More information about localhost here.
localhost is a relative host name that (usually) always resolves to your local computer. So when you're on a different computer and connecting to "localhost", you'll be attempting to connect to it, rather than your actual XMLRPC server.