Can connect with ping but not curl
Asked Answered
C

2

15

I am testing connecting to a remote server from my local machine. Using ping I can successfully connect but if I try with curl it just hangs:

Working with ping:

$ ping db-machine-02
Pinging db-machine-02.comp.org [xxx.xx.x.xxx] with 32 bytes of data:
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51
Reply from xxx.xx.x.xxx: bytes=32 time=12ms TTL=51

Ping statistics for xxx.xx.x.xxx:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 12ms, Maximum = 12ms, Average = 12ms

Fails/timeout with curl:

$ curl -v db-machine-02
* STATE: INIT => CONNECT handle 0x600077108; line 1332 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x600077108; line 1373 (connection #0)
*   Trying xxx.xx.x.xxx:80...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x600077108; line 1452 (connection #0)
* Connection timed out after 300181 milliseconds
* multi_done
* Closing connection 0
* The cache now contains 0 members
curl: (28) Connection timed out after 300181 milliseconds

Why can I connect with ping command but not with curl command?

Coverture answered 22/7, 2019 at 12:16 Comment(0)
B
12

This could be caused by port 80 on the remote machine being closed. Check your firewall rules to ensure this port is open.

Ping does not connect to a port (it uses ICMP), so that is why pinging works without opened ports.

On a side note, your ping and curl commands show you're trying to connect to "db-machine-02". Be aware that databases do not commonly communicate over port 80 by default.

Bertram answered 22/7, 2019 at 12:30 Comment(2)
Ok so its not possible to test connections with curl without specifying port?Coverture
That's right. If you do not specify a port or protocol, curl tries to establish an HTTP connection to port 80 by default. As you can see in the output of your curl command.Bertram
R
2

If using a proxy server, you may run into trouble if the proxy server cannot 'see' the destination host. For example, if using a test or development environment and your linux development host is configured to use the corporate proxy, then you may need to configure or override the no_proxy environment variable.

For example: Overriding no_proxy enables curl to work when accessing a local VM webserver from a local VM dev box (and http_proxy was set to the corporate proxy). In linux, one option is to override no_proxy as you issue the curl command:

joe@vm-dev-host:~$ no_proxy="192.168.1.123" curl -X GET "http://192.168.1.123/somedata"
Rebba answered 28/1, 2020 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.