I'm struggling to get curl and socat to play nicely together. The situation is the following:

  1. I post XML to login to a system.
  2. Return message contains authentication token.
  3. I post subsequent requests with the token. Caveat: if the connection is broken, the token expires, so I can't just use plain curl.

It works fine in PowerShell: https://pastebin.com/5Jiw1fct

However, I need this to run in Linux. Since I need the connection to persist, I decided to use socat.

If I run this to POST the XML:

curl http://$target_ip -d @./xml/login.xml

I get a proper answer from the system. But the connection is closed, so I can't re-use the token.

However, if I try this (of course, after socat):

curl --unix-socket /tmp/$target_ip.sock -d @./xml/login.xml

Curl complain I don't have the URL set.


Thanks as always.

  • Add the socat command you use to your question. It should listen to the domain socket and connect to $target_ip:80. For your curl command, use the original command and add the --unix-socket option, i.e. keep the http://$target_ip part. – meuh Jan 15 '18 at 20:24

Curl might connect to a Unix socket, but the hostname is required for the HTTP request.

Have you tried setting up socat to listen on and connect to the IP address (not name) of target system, adding the hostname from the url to /etc/hosts with an IP address of, then using the unmolested URL in the curl invocation?

I've had some pain in the past with services running on non-standard ports - so if you already have a webserver running then you'll need to use network namespaces or some other magic so that curl and the remote server both think you are connecting to the intended port.

Alternatively you can use the original URL, the socat listening on a non-standard port on lo and specify the --resolve option in curl rather than having to fudge the port and/or add an entry in /etc/hosts (requires curl >=7.57)

While it is very unusual that you have a token tied to the TCP session, it seems even more surprising that this is for an http rather than an https service.

  • I don't have root access to the machine, so I'm not sure how much of /etc/hosts wizardly I can do. I spoke to our vendor - when they described me their APIs, I was as surprised as you are with the token tied to the session. It's being a royal nightmare to write for it. I don't want to have ending up to install node or so. What I need is super simple, I should not need to write any more than a few lines of bash. – Shiunbird Jan 15 '18 at 18:22
  • Without root access your only choice is the --resolve option. Did you try? – symcbean Jan 15 '18 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.