timp87

dial.sh

Jan 23rd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.59 KB | None | 0 0
  1. #!/bin/sh
  2. # This script is written by [email protected].
  3. # Please, see `man 8 chat` for more info and
  4. # http://tehm.info/modem_AT_command
  5. # for AT commands description.
  6.  
  7. MODEM="cuau0"
  8. TIMEOUT="30"
  9.  
  10. LOCKFILE="/var/spool/lock/LCK..${MODEM}"
  11. PHONES="/etc/phones"
  12.  
  13. LOGGER="/usr/bin/logger -t dial"
  14.  
  15. COUNT="20"
  16. PAUSE="10"
  17. DIAL="0"
  18.  
  19. # Small description for shell running
  20. echo "Please, see syslog messages for description"
  21.  
  22. # Have we got a username?
  23. if [ -z $1 ]; then
  24.     RESULT="Please, provide a username!";
  25.     EXIT_CODE="1";
  26. else
  27.     # Get a phone number
  28.     $LOGGER "Getting a phone number of user $1 from file $PHONES"
  29.     NUMBER=`grep -w "$1" $PHONES | awk '{print $2}'`
  30.         if [ x${NUMBER} = x"" ]; then
  31.             RESULT="Can't find such username or it doesn't have a number";
  32.             EXIT_CODE="1";
  33.         else
  34.             $LOGGER "Phone number is $NUMBER";
  35.  
  36.             # Don't give up for $COUNT times
  37.             while [ $COUNT -gt 0 ] ; do
  38.  
  39.                 # Check lock file
  40.                 if [ -f $LOCKFILE ]; then
  41.                     PID=`cat "$LOCKFILE" | tr -d ' '`;
  42.                     # Whether the process $PID is running?
  43.                     RUN=`ps axopid -p $PID | grep -v PID | wc -l`;
  44.                     if [ $RUN -eq 0 ]; then
  45.                         $LOGGER "Stale lock on $MODEM PID=${PID}... overriding.";
  46.                         DIAL="1";
  47.                     else
  48.                         $LOGGER "It's already locked by pid $PID. Waiting for $PAUSE seconds...";
  49.                     fi
  50.                 else
  51.                     DIAL="1";
  52.                 fi
  53.  
  54.                 if [ $DIAL -eq 1 ]; then
  55.                     $LOGGER "Making lock file...";
  56.                     echo -e "\t$$" > $LOCKFILE;
  57.  
  58.                     # Let's dial
  59.                     $LOGGER "Dialing..."
  60.                     chat -t $TIMEOUT -e \
  61.                         '' ATZ OK-ATZ-OK ATS7=0 OK \
  62.                         ATDT"${NUMBER}" 'NO CARRIER' \
  63.                         < /dev/${MODEM} 2>&1 > /dev/${MODEM} \
  64.                         | grep -v '^$' | $LOGGER
  65.  
  66.                     # Delete lock file
  67.                     rm "$LOCKFILE";
  68.                     RESULT="Dial has been done!";
  69.                     EXIT_CODE="0";
  70.                     break;
  71.                 fi
  72.  
  73.                 COUNT=$(($COUNT-1));
  74.                 sleep $PAUSE;
  75.  
  76.                 if [ $COUNT -eq 0 ]; then
  77.                     RESULT="Script has exhausted attempts!";
  78.                     EXIT_CODE="1";
  79.                 fi
  80.  
  81.             done
  82.         fi
  83. fi
  84.  
  85. $LOGGER $RESULT;
  86. exit $EXIT_CODE;
Advertisement
Add Comment
Please, Sign In to add comment