Skip to content

Share devices over the net automated bootup

Mike Johnson edited this page Feb 24, 2018 · 7 revisions

I love netevent, kudos to Mr. Bumiller. If you need this, you really need it and I've found nothing else that compares. However I had quite a bit of trouble getting it to start on bootup, my use case, I ALWAYS want it there, so here's what works for me and hopefully others. I am mainly concerned with initializing on the input equipped machine (the other is a rarely rebooted server), but that requires the inputless machine to be listening, so this required 2 bash scripts, 1 on each machine. The input equipped machine runs the script on the other machine via ssh on bootup, because when it went down, it left the listening ports on the inputless machine unusable and they must be restarted. The preferred mode would be to run as unprivileged user. Here is how to accomplish that.

Set up uinput

In my distro (Funtoo) and many others, uinput is owned by root:root. We need to modify that while still maintaining security. There are many guides on the net, take your pick, this is what I did:

# https://tkcheng.wordpress.com/2013/11/11/changing-uinput-device-permission/
###Creating A New Group: uInput
Add new group by following command
$ sudo groupadd -f uinput
$ sudo gpasswd -a username uinput
Verify username has been added into uinput group (you will have to relog in)
$ groups
####Creating New udev Rule
create a new file name: /etc/udev/rules.d/99-input.rules
Insert the following in the code
KERNEL==”uinput”, GROUP=”uinput”, MODE:=”0660″
Machine reboot
Verify settings by following command:
$ ls -l /dev/uinput
crw-rw---- 1 root uinput 10, 223 Nov 11 15:35 /dev/uinput

inputless machine initialization script:

#!/bin/bash
# /usr/local/bin/remote_input_slave_setup
# ref: https://github.com/Blub/netevent/wiki/Share-devices-over-the-net
killall -9 nc # perhaps an issue if you are using netcat for other things
nc -l -p 1955 | /home/<user>/.git/netevent/netevent -write & 
nc -l -p 1956 | /home/<user>/.git/netevent/netevent -write

Chown UNPRIVILEGED USER and chmod 744. Note that I am using netcat6, which assumes tcp unless otherwise specified, there is no "-t" option as Blub uses with his examples.

input equipped machine initialization script:

#!/bin/bash
# /usr/local/bin/remote_input_master_setup
# ref: https://github.com/Blub/netevent/wiki/Share-devices-over-the-net
killall netevent
touch /home/<user>/mytmpfs/.netevent_mouse
ssh <inputless machine hostname or ip> /usr/local/bin/remote_input_slave_setup &
sleep 1
/home/<user>/.git/netevent/netevent -nograb -toggler "/home/<user>/mytmpfs/.netevent_mouse" -read /dev/input/by-id/usb-Logitech_Optical_USB_Mouse-event-mouse | nc <inputless machine ip> 1955 &
sleep 0.3
/home/<user>/.git/netevent/netevent -nograb -hotkey EV_KEY:<your hotkey choice> "@toggle" -ontoggle "echo \$GRAB > /home/<user>/mytmpfs/.netevent_mouse" -read /dev/input/by-id/usb-Logitech_USB_Keyboard-event-kbd | nc <inputless machine ip> 1956 &

Chown UNPRIVILEGED USER and chmod 744. Obviously you will have to change the device references to match your setup. I do recommend that you use /dev/input/by-id/ rather than /dev/input/eventxx because your machine can arbitrarily assign those event numbers, by-id is failsafe.

Mr. Bumiller suggests using a FIFO for your "toggler" file. I was never able to make this work reliably. What worked for me was creating a directory for a small tmpfs directory in my $HOME.

$ mkdir /home/<user>/mytmpfs

Then I added this to my /etc/fstab:

tmpfs                   /home/<user>/mytmpfs tmpfs             size=1K         0 0

Finally I added this line to my /etc/local.d/my.start file, this is distro dependent so you may need to get creative with how to accomplish this on your distro (mine is Funtoo):

(sleep 5;su <user> -c '/usr/local/bin/remote_input_master_setup') &

That's it, now on startup everything's going with no effort. On the rare occasion that I need to restart the inputless machine I simply run /usr/local/bin/remote_input_master_setup on the input equipped machine after the server has booted.

After using netevent for a while, I found it would be handy to have a visual indicator as to state. One of netevents great features is that it works in console as well as X and I access my remote machine in both daily. But the input equipped machine is virtually always in X. So xset can be used to set keyboard leds as an indicator. Here's how to do that easily. Change this line:

/home/<user>/.git/netevent/netevent -nograb -hotkey EV_KEY:<your hotkey choice> "@toggle" -ontoggle "echo \$GRAB > /home/<user>/mytmpfs/.netevent_mouse" -read /dev/input/by-id/usb-Logitech_USB_Keyboard-event-kbd | nc <inputless machine ip> 1956 &

To this:

/home/<user>/.git/netevent/netevent -nograb -hotkey EV_KEY:<your hotkey choice> "@toggle" -ontoggle "echo \$GRAB > /home/<user>/mytmpfs/.netevent_mouse;su <unprivileged user> -c '/usr/local/bin/toggle_scrolllock'" -read /dev/input/by-id/usb-Logitech_USB_Keyboard-event-kbd | nc <inputless machine ip> 1956 &

Create /usr/local/bin/toggle_scrolllock:

#!/bin/bash

if [[ $(cat /home/<user>/mytmpfs/.netevent_mouse) > 0 ]]; then
	xset led named "Scroll Lock"
else
	xset -led named "Scroll Lock"
fi

Make it executable and owned by unprivileged user.

Now the scroll lock led will tell you on which machine the keyboard/mouse is active.

Good luck!