{"id":40,"date":"2006-07-12T13:18:01","date_gmt":"2006-07-12T11:18:01","guid":{"rendered":"http:\/\/www.hackenberger.at\/wordpress\/?page_id=40"},"modified":"2006-07-12T13:39:45","modified_gmt":"2006-07-12T11:39:45","slug":"how-to-configure-hotplugging-to-execute-a-specific-script-on-addingremoving-a-device","status":"publish","type":"page","link":"https:\/\/www.hackenberger.at\/blog\/linux-guides\/gentoo-guides\/how-to-configure-hotplugging-to-execute-a-specific-script-on-addingremoving-a-device\/","title":{"rendered":"Custom hotplugging scripts"},"content":{"rendered":"<p><strong>Instructions how to configure hotplugging to execute a specific script on adding\/removing a device<br \/>\n<\/strong><\/p>\n<p>The first thing you have to do is to get hotplugging working in general. Visit \t\t\t\t<a href=\"http:\/\/linux-hotplug.sourceforge.net\/\">Linux Hotplugging<\/a> to learn how \t\t\t\tto do that.<br \/>\nYou should have a file named usb.usermap in your \/etc\/hotplug\/ directory. This file \t\t\t\thandles to scripts for particular devices. You have to set several colums in this \t\t\t\tfile to get a script working. The first colum is the filename of the script. The \t\t\t\tscripts are always expected in \/etc\/hotplug\/usb\/. The next column is, i don&#8217;t know \t\t\t\twhat it is for. The next two are the Vendor and Product id of your device. \t\t\t\t\/proc\/bud\/usb\/devices helps you out with these as soon as you plug in your device. \t\t\t\tYou don&#8217;t need the rest of the columns. \t\t\t\tHere \t\t\t\tis an example for my HP IPAQ:<br \/>\nipaq 0x0003 0x049f 0x505a 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000005<br \/>\nThis has to be ONE single line.<\/p>\n<p>Now that hotplug knows which script to invoke, you can write the script itself. \t\t\t\tVisit \/etc\/hotplug\/usb.agent for a description of preset variables. Here is an \t\t\t\texample script, setting up IP-Masquerading for my IPAQ:<\/p>\n<blockquote><p>#!\/bin\/bash<\/p>\n<p>#This is the IPAQ Organizer. We want IP-Masquerading<br \/>\n#Bring up the interface (We assume the standard Opie usb IP)<br \/>\nifconfig usb0 192.168.0.200 netmask 255.255.255.0 pointopoint 192.168.0.202 up<br \/>\n#Write the route to the IPAQ into the routing table<br \/>\nroute add -host 192.168.0.202 usb0 > \/dev\/null<br \/>\n#Now set up IP-Masquerading<br \/>\niptables -t nat -I POSTROUTING -j MASQUERADE -s 192.168.0.202\/16<br \/>\niptables -I FORWARD -s 192.168.0.202\/16 -j ACCEPT<br \/>\niptables -I FORWARD -d 192.168.0.202\/16 -j ACCEPT<br \/>\n#And active it<br \/>\necho 1 > \/proc\/sys\/net\/ipv4\/ip_forward<br \/>\n#set the remover script<br \/>\n:> $REMOVER<br \/>\n#Remove all the iptables entries we&#8217;ve created<br \/>\necho &#8220;iptables -t nat -D POSTROUTING -j MASQUERADE -s 192.168.0.202\/16&#8221; >> $REMOVER<br \/>\necho &#8220;iptables -D FORWARD -s 192.168.0.202\/16 -j ACCEPT&#8221; >> $REMOVER<br \/>\necho &#8220;iptables -D FORWARD -d 192.168.0.202\/16 -j ACCEPT&#8221; >> $REMOVER<br \/>\nchmod +x $REMOVER<\/p><\/blockquote>\n<p>Very important are the line down from &#8220;:> $REMOVER&#8221;. The REMOVER variable is your \t\t\t\tscript which is called on removing the device.<\/p>\n<p>Ok, thats it, you have your mapping from the device to the script, you have your \t\t\t\tscript itself and have set up a REMOVER script as well.<\/p>\n<p>Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Instructions how to configure hotplugging to execute a specific script on adding\/removing a device The first thing you have to do is to get hotplugging working in general. Visit Linux Hotplugging to learn how to do that. You should have a file named usb.usermap in your \/etc\/hotplug\/ directory. This file handles to scripts for particular &hellip; <a href=\"https:\/\/www.hackenberger.at\/blog\/linux-guides\/gentoo-guides\/how-to-configure-hotplugging-to-execute-a-specific-script-on-addingremoving-a-device\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Custom hotplugging scripts<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":35,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/pages\/40"}],"collection":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/pages\/40\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/pages\/35"}],"wp:attachment":[{"href":"https:\/\/www.hackenberger.at\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}