Using curl with ssh tunnels and a socks proxy - Thu, Mar 17, 2022
Using curl with ssh tunnels and a socks proxy
Where I work i often have to use several jump hosts to get to the machines in different environments (dev, ref, etc.). In order access http-based services that only run on those machines socks proxies
are very useful.
Using a ssh config with DynamicForwards
lets me connect directly to machines, multiple jumps away.
The following example shows the ssh config for one of those tunnels and the curl to access a loki
instance on that machine:
Host loki-host
HostName 172.16.0.2
IdentityFile ~/.ssh/id_rsa
User tom
ForwardAgent yes
ProxyJump jumphost
Compression yes
LogLevel QUIET
DynamicForward 1337
Host jumphost
HostName 192.168.10.1
IdentityFile ~/.ssh/id_rsa
User tom
ForwardAgent yes
Compression yes
LogLevel QUIET
DynamicForward 1337
$ curl --socks5-hostname localhost:1337 -G -s "http://localhost:3100/loki/api/v1/labels" | jq
{
"status": "success",
"data": [
"__name__",
"agent",
"cluster",
"filename",
"job"
]
}
The ssh config lets me connect to loki-host
without having to supply passwords. The command ssh loki-host
will bring up the socks proxy on port 1337
which is then used by the curl. Please note that you should alter the NO_PROXY
environment variable not to include localhost
for this example (If you use one). Otherwise curl will try to use the proxy instead of the socks proxy.