Difference between revisions of "Execute on USB insert"

From ArchWiki
Jump to: navigation, search
m (Installation)
m (fixing i18n link)
(18 intermediate revisions by 8 users not shown)
Line 1: Line 1:
Suppose you want to execute a certain program on the insertion of a usb dongle.
+
[[Category:Hardware detection and troubleshooting]]
 +
[[it:Execute on USB insert]]
 +
Suppose you want to execute a certain program on the insertion of a USB dongle.
 
Personally, I added this feature because I was getting frustrated with the computer locking up and all keyboard and mouse input was lost (The sysrq keys were not working).
 
Personally, I added this feature because I was getting frustrated with the computer locking up and all keyboard and mouse input was lost (The sysrq keys were not working).
  
Line 6: Line 8:
 
== Installation ==
 
== Installation ==
  
Put a copy of the {{Filename|/usr/bin/exdongle}} script (given below) in your path.
+
Put a copy of the [[Execute_on_USB_insert#exdongle_script|{{ic|exdongle}} script]] (given below) in your path.
  
 
Add the following line to your udev [[Udev#Auto_mounting_USB_devices|USB mount rule]] (Modify to fit your configuration):
 
Add the following line to your udev [[Udev#Auto_mounting_USB_devices|USB mount rule]] (Modify to fit your configuration):
Line 14: Line 16:
 
Run the configuration option, for example
 
Run the configuration option, for example
  
   exdongle conf -k "$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM" -p "-10"
+
   exdongle conf -k "$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM" -p "-10" -s "on"
  
 
This sets the configuration to a random key value (only programs with the correct keys can be run on the computer), and the runtime priority to -10.
 
This sets the configuration to a random key value (only programs with the correct keys can be run on the computer), and the runtime priority to -10.
Line 24: Line 26:
 
For example:
 
For example:
  
{{File|name=test.sh|content=<nowiki>
+
{{hc|test.sh|2=<nowiki>
 
#!/bin/sh
 
#!/bin/sh
  
Line 39: Line 41:
 
== Security ==
 
== Security ==
  
There may be security issues with this, but it is probably best used for a personal computer.
+
There may be security issues with this, and it is probably best used only on a personal computer.
  
 
== exdongle script ==
 
== exdongle script ==
  
{{File|name=/usr/bin/exdongle|content=<nowiki>
+
{{hc|/usr/bin/exdongle|2=<nowiki>
 
#!/bin/sh
 
#!/bin/sh
# exdongle
+
 
 +
# exdongle mkexdongle
  
 
###############################################################################
 
###############################################################################
Line 56: Line 59:
  
 
   echo ""
 
   echo ""
   echo "USAGE: $0 new <DIR> <PROG>"
+
   echo "USAGE: $0 new <DIR> <PROG>"
   echo "      $0 conf [-k KEY] [-p PRIORITY]"
+
  echo "      $0 del <DIR>"
 +
   echo "      $0 conf [-k KEY] [-p PRIORITY] [-s SWITCH]"
 
   echo "      $0 run <DIR>"
 
   echo "      $0 run <DIR>"
 
   echo ""
 
   echo ""
 
   echo "  new:"
 
   echo "  new:"
   echo "    DIR: The directory on the dongle to execute"
+
   echo "    DIR <S>:   The directory on the dongle to execute"
   echo "    PROG: The script to run on dongle"
+
   echo "    SCRIPT <S>: The script to run on dongle"
 +
  echo ""
 +
  echo "  del:"
 +
  echo "    DIR [S]: The directory on the dongle to execute"
 
   echo ""
 
   echo ""
 
   echo "  conf:"
 
   echo "  conf:"
   echo "    -k: specify key"
+
   echo "    -k <S>: specify key"
   echo "    -p: priority to run script at"
+
   echo "    -p <N>: priority to run script at"
 +
  echo "    -s <on|off>: Activate or deactivate exdongle"
 
   echo ""
 
   echo ""
 
   echo "  run:"
 
   echo "  run:"
   echo "    DIR: Directory to find script in"
+
   echo "    DIR <S>: Directory to find script in"
 
   echo ""
 
   echo ""
  
Line 98: Line 106:
 
###############################################################################
 
###############################################################################
  
function conf() {
+
function del() {
  
   while getopts "k:p:" optname; do
+
   DIR="$2"
 +
  PLOC="$DIR/.$(hostname).$key"
 +
 
 +
  if [ -e "$PLOC" ]; then
 +
    rm -f "$PLOC"
 +
  fi
 +
 
 +
  exit 0
 +
 
 +
}
 +
 
 +
###############################################################################
 +
 
 +
function conf() {
  
 +
  shift
 +
  while getopts "k:p:s:" optname; do
 
     case "$optname" in
 
     case "$optname" in
 
       k)
 
       k)
Line 109: Line 132:
 
         priority="$OPTARG"
 
         priority="$OPTARG"
 
         ;;
 
         ;;
       *)
+
       s)
         echo "\'$optname\' is not an option" 1>&2
+
         if [ "$OPTARG" = "on" ] || [ "$OPTARG" = "off" ]; then
 +
          switch="$OPTARG"
 +
        fi
 
         ;;
 
         ;;
 
     esac
 
     esac
 
   done
 
   done
  
   echo "#!/bin/sh" > "$CONFFILE"
+
   echo "# exdongle configuration file" > "$CONFFILE"
   echo "key=$key" >> "$CONFFILE"
+
   echo "key=\"$key\"" >> "$CONFFILE"
   echo "priority=$priority" >> "$CONFFILE"
+
   echo "priority=\"$priority\"" >> "$CONFFILE"
 +
  echo "switch=\"$switch\"" >> "$CONFFILE"
  
 
   chmod 0600 "$CONFFILE"
 
   chmod 0600 "$CONFFILE"
Line 128: Line 154:
  
 
function run() {
 
function run() {
 +
 +
  if [ ! "$switch" = "on" ]; then
 +
    exit 0
 +
  fi
  
 
   if [ ! "$#" -eq 2 ]; then
 
   if [ ! "$#" -eq 2 ]; then
Line 146: Line 176:
 
   cp "$PLOC" "$ELOC"
 
   cp "$PLOC" "$ELOC"
 
   chmod 0500 "$ELOC"
 
   chmod 0500 "$ELOC"
   nice -n $priority $ELOC
+
   nice -n "$priority" $ELOC
 
+
  rm -f "$ELOC"
 
   exit 0
 
   exit 0
  
Line 165: Line 195:
 
case "$1" in
 
case "$1" in
 
   new)
 
   new)
     new $@
+
     new "$@"
 +
    ;;
 +
  del)
 +
    del "$@"
 
     ;;
 
     ;;
 
   conf)
 
   conf)
     conf "$2" "$3" "$4" "$5" "$6" "$7"
+
     conf "$@"  
 
     ;;
 
     ;;
 
   run)
 
   run)
     run $@
+
     run "$@"
 
     ;;
 
     ;;
 
   *)
 
   *)
     usage $@
+
     usage "$@"
 
     ;;
 
     ;;
 
esac
 
esac
Line 181: Line 214:
  
 
</nowiki>}}
 
</nowiki>}}
 +
 +
== Disable Trackpad on Mouse Connection ==
 +
Adapted from [http://www.howtoforge.com/how-to-auto-disable-the-touchpad-when-the-mouse-is-plugged-in-fedora-13 Howto Forge].
 +
 +
== Notes ==
 +
 +
Some notes:
 +
* The programs/scripts used should be self contained, the only file treated is the one passed.

Revision as of 15:14, 20 June 2012

Suppose you want to execute a certain program on the insertion of a USB dongle. Personally, I added this feature because I was getting frustrated with the computer locking up and all keyboard and mouse input was lost (The sysrq keys were not working).

There is an added security feature so that only dongles with the correct key can run the program.

Installation

Put a copy of the exdongle script (given below) in your path.

Add the following line to your udev USB mount rule (Modify to fit your configuration):

 ACTION=="add", RUN+="/usr/bin/exdongle run /media/%k-%E{dir_name}"

Run the configuration option, for example

 exdongle conf -k "$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM" -p "-10" -s "on"

This sets the configuration to a random key value (only programs with the correct keys can be run on the computer), and the runtime priority to -10.

Setting up a USB dongle

First, write a script that you want to run when the dongle is inserted.

For example:

test.sh
#!/bin/sh

DISPLAY=:0 xterm

Now run the new option:

 exdongle new <Dongle mount point> test.sh

Now, whenever you insert the dongle, a root shell will be opened under the X server running on display :0. This may be useful for some administrative purposes.

Security

There may be security issues with this, and it is probably best used only on a personal computer.

exdongle script

/usr/bin/exdongle
#!/bin/sh

# exdongle mkexdongle

###############################################################################

CONFFILE="/etc/exdongle.conf"

###############################################################################

function usage() {

  echo ""
  echo "USAGE: $0 new <DIR> <PROG>"
  echo "       $0 del <DIR>"
  echo "       $0 conf [-k KEY] [-p PRIORITY] [-s SWITCH]"
  echo "       $0 run <DIR>"
  echo ""
  echo "  new:"
  echo "    DIR <S>:    The directory on the dongle to execute"
  echo "    SCRIPT <S>: The script to run on dongle"
  echo ""
  echo "  del:"
  echo "    DIR [S]: The directory on the dongle to execute"
  echo ""
  echo "  conf:"
  echo "    -k <S>: specify key"
  echo "    -p <N>: priority to run script at"
  echo "    -s <on|off>: Activate or deactivate exdongle"
  echo ""
  echo "  run:"
  echo "    DIR <S>: Directory to find script in"
  echo ""

  exit 0

}

###############################################################################

function new() {

  if [ ! "$#" -eq 3 ]; then
    usage $@;
    exit 1;
  fi

  DIR="$2"
  PROG="$3"
  PLOC="$DIR/.$(hostname).$key"

  rm "$PLOC" &> /dev/null
  cp "$PROG" "$PLOC"

  exit 0

}

###############################################################################

function del() {

  DIR="$2"
  PLOC="$DIR/.$(hostname).$key"

  if [ -e "$PLOC" ]; then
    rm -f "$PLOC"
  fi

  exit 0

}

###############################################################################

function conf() {

  shift
  while getopts "k:p:s:" optname; do
    case "$optname" in
      k)
        key="$OPTARG"
        ;;
      p)
        priority="$OPTARG"
        ;;
      s)
        if [ "$OPTARG" = "on" ] || [ "$OPTARG" = "off" ]; then
          switch="$OPTARG"
        fi
        ;;
    esac
  done

  echo "# exdongle configuration file" > "$CONFFILE"
  echo "key=\"$key\"" >> "$CONFFILE"
  echo "priority=\"$priority\"" >> "$CONFFILE"
  echo "switch=\"$switch\"" >> "$CONFFILE"

  chmod 0600 "$CONFFILE"

  exit 0

}

###############################################################################

function run() {

  if [ ! "$switch" = "on" ]; then
    exit 0
  fi

  if [ ! "$#" -eq 2 ]; then
    usage $@
    exit 1
  fi

  DIR="$2"
  PLOC="$DIR/.$(hostname).$key"
  ELOC="/tmp/.exdongle.prog"

  if [ ! -e "$PLOC" ]; then
    echo "No executable file found!" 1>&2
    exit 0
  fi

  rm -f "$ELOC"
  cp "$PLOC" "$ELOC"
  chmod 0500 "$ELOC"
  nice -n "$priority" $ELOC
  rm -f "$ELOC"
  exit 0

}

###############################################################################

if [ ! "$UID" == "0" ]; then
  echo "You must be root to perform this operation" 1>&2
  exit 1
fi

if [ -e "$CONFFILE" ]; then
  . "$CONFFILE"
fi

case "$1" in
  new)
    new "$@"
    ;;
  del)
    del "$@"
    ;;
  conf)
    conf "$@" 
    ;;
  run)
    run "$@"
    ;;
  *)
    usage "$@"
    ;;
esac

exit 0

Disable Trackpad on Mouse Connection

Adapted from Howto Forge.

Notes

Some notes:

  • The programs/scripts used should be self contained, the only file treated is the one passed.