Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. SERVICE_NAMES=(
  4. 'nguri TimeCapsule' # For AFP
  5. 'nguri SMB'         # For SMB
  6. 'nguri TimeCapsule' # For AFP Icon
  7. 'nguri SMB'         # For SMB Icon
  8. 'Brother MFC-9120CN'    # Printer
  9. 'Brother MFC-9120CN'    # Print spooler
  10. 'Brother MFC-9120CN'    # PDL Data stream
  11. )
  12.  
  13. SERVICE_TYPES=(
  14. _afpovertcp._tcp
  15. _smb._tcp
  16. _device-info._tcp
  17. _device-info._tcp
  18. _ipp._tcp
  19. _printer._tcp
  20. _pdl-datastream._tcp
  21. )
  22.  
  23. SERVICE_PORTS=(
  24. 548
  25. 445
  26. 1
  27. 1
  28. 631
  29. 515
  30. 9100
  31. )
  32.  
  33. SERVICE_IPS=(
  34. 192.168.1.1
  35. 192.168.1.1
  36. 192.168.1.1
  37. 192.168.1.1
  38. 192.168.1.2
  39. 192.168.1.2
  40. 192.168.1.2
  41. )
  42.  
  43. # FIX: Use one valid string format across all three service text definitions
  44. SERVICE_TEXTS=(
  45. ''
  46. ''
  47. model=TimeCapsule8,119
  48. model=Xserve
  49. 'txtvers=1 qtotal=1 pdl=application//vnd.hp-PCL,application//vnd.brother-hbp rp=duerqxesz5090 ty="Brother MFC-9120CN" product="(Brother MFC-9120CN)" adminurl=http://BRN001BA9243652.local./ priority=50 usb_MFG=Brother usb_MDL=MFC-9120CN Color=T Copies=T Duplex=F PaperCustom=T Binary=T Transparent=T TBCP=F'
  50. 'txtvers=1 qtotal=1 pdl=application/vnd.hp-PCL,application/vnd.brother-hbp rp=duerqxesz5090 ty="Brother MFC-9120CN" product="(Brother MFC-9120CN)" adminurl=http://BRN001BA9243652.local./ priority=75 usb_MFG=Brother usb_MDL=MFC-9120CN Color=T Copies=T Duplex=F PaperCustom=T Binary=T Transparent=T TBCP=F'
  51. 'txtvers=1 qtotal=1 pdl=application/vnd.hp-PCL,application/vnd.brother-hbp ty="Brother MFC-9120CN" product="(Brother MFC-9120CN)" adminurl=http://BRN001BA9243652.local./ priority=25 usb_MFG=Brother usb_MDL=MFC-9120CN Color=T Copies=T Duplex=F PaperCustom=T Binary=T Transparent=F TBCP=T'
  52. )
  53.  
  54. declare -a PIDS
  55. SLEEP_INTERVAL=10
  56.  
  57. while [ true ]; do
  58.     ifconfig ppp0 &>/dev/null
  59.     if [ "$?" -eq "0" ]; then
  60.         # We are connected to ppp0
  61.         for ((n=0; n < ${#SERVICE_NAMES[@]}; n++)); do
  62.             # Is dns-sd already running for a particular service?
  63.             kill -0 "${PIDS[$n]}" &>/dev/null
  64.             DNSSD_RUNNING=$?
  65.            
  66.             # Start if not running...
  67.             if [ "$DNSSD_RUNNING" -ne "0" ]; then
  68.                 echo "Starting DNS SD Proxy for service ${SERVICE_TYPES[$n]} on server ${SERVICE_NAMES[$n]}"
  69.                
  70.                 #FIX: by using a temporar variable we can debug the exact command that will be used including all quotes
  71.                 CMD="dns-sd -P \"${SERVICE_NAMES[$n]}\" ${SERVICE_TYPES[$n]} local ${SERVICE_PORTS[$n]} \"${SERVICE_NAMES[$n]}.local\" \"${SERVICE_IPS[$n]}\" ${SERVICE_TEXTS[$n]} &>/dev/null &"
  72.                 echo "DEBUG: $CMD"
  73.                 # use exactly this command
  74.                 eval $CMD
  75.                 PIDS[$n]=$!;
  76.            
  77.             # Don't do anything if already running...
  78.             else
  79.                 echo "DNS SD Proxy for service ${SERVICE_TYPES[$n]} on server ${SERVICE_NAMES[$n]} is already running"
  80.             fi
  81.         done
  82.        
  83.     else  # We're not connected to ppp0
  84.         echo "ppp0 is not connected"
  85.        
  86.         for ((n=0; n < ${#SERVICE_NAMES[@]}; n++)); do
  87.             # If we have running dns-sd instances, stop them
  88.             if [ ! "${PIDS[$n]}" = "" ]; then
  89.                 echo "Stopping DNS SD Proxy for service ${SERVICE_TYPES[$n]} on server ${SERVICE_NAMES[$n]}"
  90.                 kill "${PIDS[$n]}" &>/dev/null
  91.                 PIDS[$n]="";
  92.             else
  93.                 echo "DNS SD Proxy for service ${SERVICE_TYPES[$n]} on server ${SERVICE_NAMES[$n]} is not running"
  94.             fi
  95.         done
  96.     fi
  97.    
  98.     # Wait a while...
  99.     sleep $SLEEP_INTERVAL
  100. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement