How to share Aeotec Z-Stick USB over network using socat (useful for unprivileged LXC containers)
Posted: Monday 28 August 2017 8:29
Assuming Debian (this is for very advanced Linux users, no support / warranty is provided):
On USB server (device with the z-stick plugged in)
1) apt-get install socat screen
2) contents of /etc/init.d/usbserver
3) contents of /scripts/socatserver.sh
On USB client
1) apt-get install socat screen
2) contents of /etc/init.d/usbclient
3) contents of /scripts/socatclientmonitor.sh
The advantage of this setup as opposed to USBIP is that it does not require the loading of a kernel module on the client which makes it easy to use in unprivileged LXC containers.
It's also possible to share RFXCOM (rfxtrx433e) via socat as well (and likely a lot of other USB serial devices).
On USB server (device with the z-stick plugged in)
1) apt-get install socat screen
2) contents of /etc/init.d/usbserver
Code: Select all
#!/bin/sh
### BEGIN INIT INFO
# Provides: usbserver
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description:
### END INIT INFO
case "$1" in
'start')
/usr/bin/screen -S socatmonitor -d -m /scripts/socatserver.sh
;;
'stop')
pkill -f 'socatmonitor'
;;
*)
echo "Usage: $0 { start | stop }"
;;
esac
exit 0
Code: Select all
#!/bin/bash
while true; do
# Check if socat is running for Aeotec Z-Stick Gen 5
if pgrep -f 'zsticksocatserver' >/dev/null 2>&1; then
echo "Z-Stick socat server is running"
else
pkill -f 'zsticksocatserver'
echo "starting zstick socat server..."
/usr/bin/screen -S zsticksocatserver -d -m socat -ls -s -d -d FILE:/dev/ttyACM0,b115200,raw,echo=0 TCP-LISTEN:1235,reuseaddr,reuseport
fi
sleep 1
done
1) apt-get install socat screen
2) contents of /etc/init.d/usbclient
Code: Select all
#!/bin/sh
### BEGIN INIT INFO
# Provides: usbclient
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description:
### END INIT INFO
case "$1" in
'start')
/usr/bin/screen -S socatclientmonitor -d -m /scripts/socatclientmonitor.sh
;;
'stop')
pkill -f 'socatclientmonitor'
;;
*)
echo "Usage: $0 { start | stop }"
;;
esac
exit 0
Code: Select all
#!/bin/bash
while true; do
# Check if socat client is running for Aeotec Z-Stick Gen 5
if [ -e /dev/ttyACM0 ]; then
echo "Z-Stick socat is connected"
else
echo "Z-Stick socat not connected, establishing connection..."
socat pty,link=/dev/ttyACM0,b115200,raw tcp:192.168.0.21:1235&
fi
sleep 1
done
It's also possible to share RFXCOM (rfxtrx433e) via socat as well (and likely a lot of other USB serial devices).