Is there any way I can access a device plugged into the USB port of my local machine while connected to a remote machine via SSH?
What I am trying to do is set up a development environment in the cloud, and flash programs to a microcontroller connected via SSH.
Best Answer
This assumes you are running Linux on both sides. If you are running Windows, there are possibilities (like
putty
andCygWin
), but it is a bit more difficult to do.Yes, you probably can. But this is very likely not want want to do:
If you are flashing, what happens if the network connection breaks?
Do you really want to allow a cloud to "infect" your local USB?
Serial port
If your local USB device has something like a serial port for flashing, you perhaps can use some local port forwarding and
socat
to connect to the serial line. Provided that your development environment allows toflash
using a TCP port. Like follows:On your local machine:
ssh -R1234:localhost:1234 user@remote.example.com
This opens port
127.0.0.1:1234
onremote.example.com
which then connects to127.0.0.1:1234
on your local machine.Now you use
socat
to connect to the serial line of yourUSB
:On your local machine:
socat -d -d -d tcp-listen:1234,reuseaddr,fork,bind=127.0.0.1 /dev/ttyUSB0
TCP -> Pipe
On the other side, if the software cannot use TCP but wants to talk to a pipe or tty, you can use
socat
again to provide that:On the remote:
socat -d -d -d unix-listen:.mypipe tcp-connect:127.0.0.1:1234
Now let your remote software connect to
.mypipe
and it is transparently connected to/dev/ttyUSB0
on your local computer.But your mileage may vary. It depends heavily what your software in the cloud needs.
Special USB drivers
For example it may be that you need to create some special stub, which talks to
USB
. Luckily this can be forked of by socat as well:On the local side:
socat -d -d -d tcp-listen:1234,reuseaddr,fork,bind=127.0.0.1 exec:/path/to/program
The remote side is more difficult, because if it expects to talk to
USB
, you probably need to create some dummy USB driver for the Linux kernel and load it there. Not easy to manage, sorry.To sum it up
SSH
in combination withsocat
is able to provide nearly all the interconnection between your local machine and "the cloud" you might think of.However connecting to a local
USB
may or may not need some special treatment, on both sides. The problematic part is notSSH
orsocat
, it is how to allow the remote software to talk over such a tunnel to your local USB.Perhaps somebody else knows some
plug and play USB emulator
for Linux, but I never came across such a beast. And I do Linux for nearly 20 years now ..As a starting point, perhaps look at that answer