Module ssh_tunnel
Data Types
location()
location() = {string(), integer()}
Function Index
connect/0 | Module for creating SSH tunnels using ssh . |
connect/3 | Create a connetion to a remote host with the provided options. |
direct_stream_local/2 | Creates a ssh stream local-forward channel to a remote unix domain socket. |
direct_tcpip/3 | Creates a ssh directtcp-ip forwarded channel to a remote port. |
open_channel/6 | |
start_tunnel/3 | Starts a SSHTunnel.Tunnel process. |
Function Details
connect/0
connect() -> any()
Module for creating SSH tunnels using ssh
.
https://github.com/drowzy/ssh_tunnel
It provides functions to create forwarded ssh channels, similair
to how other channels can be created using ssh_connection
.
There are two type of channels supported
* directtcp-ip` - Forwards a port from the client machine to the remote machine.
This is the same as `ssh -nNT -L 8080:forward.example.com:9000 user@sshserver.example.com
* direct-streamlocal
- Forwards to a unix domain socket.
This is the same as ssh -nNT -L 8080:/var/lib/mysql/mysql.sock user@sshserver.example.com
When using direct_tcpip/3
or direct_stream_local/2
directly there
will not be any local port or socket bound, this can either be done
using ssh_tunnel
or by manually sending data with ssh_connection.send/3
.
Although connect/1
can be used to connect to the remote host, other
methods are supported.
One can use [SSHex](https://github.com/rubencaro/sshex), ssh:connect/3
for instance.
## Tunnels Tunnels are on-demand TCP servers and are bound listeners to either a port or a path. The tunnel will handle relaying TCP messages to the ssh connection and back.
## Examples {ok, SshRef} = ssh_tunnel:connect("sshserver.example.com", 22, []),
{ok, Pid} = ssh_tunnel:start_tunnel(Pid, {tcpip, {8080, {"192.168.90.15", 80}}}),
% Send a TCP message for instance HTTP
Resp = HTTPoison.get!("127.0.0.1:8080"),
io:format("Received body: ~p\n", [Resp])
connect/3
connect(Host :: list() | tuple(),
Port :: integer(),
Opts :: list()) ->
{ok, pid()} | {error, term()}
Create a connetion to a remote host with the provided options.
This function is mostly used as convenience wrapper around ssh_connect/3
and does not support all options.
returns: {ok, Connection}
or {error, Reason}
.
https://manpages.debian.org/stretch/erlang-manpages/ssh.3erl.en.html
direct_stream_local/2
direct_stream_local(Pid :: pid(), SocketPath :: string()) ->
{ok, integer()} | {error, term()}
Creates a ssh stream local-forward channel to a remote unix domain socket.
It sends the request that the server make a connection to its local Unix domain socket.
The returned channel together with a ssh connection reference (returned from ssh:connect/4
)
can be used to send messages with ssh_connection:send/3
.
returns: {ok, Channel}` or `{error, Reason}
.
Ex:
msg = "GET /images/json HTTP/1.1\r\nHost: /var/run/docker.sock\r\nAccept: */*\r\n\r\n"
{ok, Pid} = ssh_tunnel:connect("192.168.90.15", 22),
{ok, Ch} = ssh_tunnel:direct_stream_local(Pid, "/var/run/docker.sock"),
ok = ssh_connection.send(Pid, Ch, Msg)
direct_tcpip/3
direct_tcpip(Pid :: pid(), From :: location(), To :: location()) ->
{ok, integer()} | {error, term()}
Creates a ssh directtcp-ip forwarded channel to a remote port.
The returned channel together with a ssh connection reference (returned
from :ssh.connect/4
) can be used to send messages with ssh_connection:send/3
returns: {ok, channel}
or {error, reason}
.
## Examples:
msg = "GET / HTTP/1.1\r\nHost: localhost:8080\r\nUser-Agent: curl/7.47.0\r\nAccept: */*\r\n\r\n"
{ok, Pid} = ssh_tunnel:connect("192.168.1.10", 22),
{ok, Ch} = ssh_tunnel:direct_tcpip(Pid, {"127.0.0.1", 8080}, {"192.168.1.10", 80}),
ok = ssh_connection:send(Pid, Ch, Msg),
recieve do
{ssh_cm, _, {data, Channel, _, Data}} -> io:format("Data: ~p\n", [Data])
end
open_channel/6
open_channel(Pid, Type, Msg, WindowSize, MaxPktSz, Timeout) -> any()
start_tunnel/3
start_tunnel(Pid :: pid(),
Transport :: tcp | local,
To :: tuple() | integer()) ->
{ok, pid()} | {error, term()}
Starts a SSHTunnel.Tunnel process. The tunnel will listen to either a local port or local path and handle passing messages between the TCP client and ssh connection. ## Examples {ok, SSH} = ssh_tunnel:connect("sshserver.example.com", 22), {ok, Pid} = ssh_tunnel:start_tunnel(Pid, tcp, {8080, {"192.168.90.15", 80}}) # Send a TCP message %HTTPoison.Response{body: body} = HTTPoison.get!("127.0.0.1:8080") IO.puts("Received body: #{body})