Guest User

Untitled

a guest
Aug 21st, 2016
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 21.26 KB | None | 0 0
  1. #!/bin/bash
  2. #************************************************************
  3. # Company: SonicWALL Ltd.                                   *
  4. #                                                           *
  5. # Description: script cdpinstall.sh                         *
  6. #                                                           *
  7. # Author: root                                              *
  8. #                                                           *
  9. # Version: 1.1                                              *
  10. #                                                           *
  11. # Modifier: Jowis, 2011/07/04                               *
  12. #************************************************************
  13.  
  14. VERSION=""
  15. VERSION_OLD=""
  16. VERSION_NEW=""
  17. SRCDIR=`(cd \`dirname $0 \`; pwd )`
  18. SRCPKG=$SRCDIR/cdpagent.tar.gz
  19. TMPDIR=/sbin/cdp/install
  20. SYSLIBDIR=/usr/lib
  21. INSTALLDIR=/sbin
  22. ACCESSTYPE="root"
  23. ACCESSMODE="744"
  24. CDPAUTOUPDATE=cdpautoupdate
  25. CDPAGENTSERVICE=cdpagentservice
  26. CDPAGENTPROXY=cdpagentproxy
  27. CDPAGENTCONF=config.xml
  28. PIDOF=pidof
  29. MODE="interactive"
  30. AutoUpdateFlag="false"
  31. LOGFILE="/var/log/cdp/cdpinstall.log"
  32. argument=""
  33. commands=""
  34. SERVERIP=""
  35.  
  36. function die()
  37. {
  38.     echo $@ | tee -a ${LOGFILE}
  39.     exit 1
  40. }
  41.  
  42. usage()
  43. {
  44.     echo "Usage: cdpinstall.sh [command] [option]"
  45.     echo '  "command:"'
  46.     echo '     "cdpinstall.sh -i" to install ( full installation)'
  47.     echo '     "cdpinstall.sh -u" to uninstall'
  48.     echo '     "cdpinstall.sh -d" to install CDP Agent Service only'
  49.     echo '     "cdpinstall.sh -v|--version" output version info'
  50.     echo '  "option:"'
  51.     echo '     "-y|-Y"  Automatically insert "yes" when querying yes/no during installation'
  52.     echo '     "--access=all"  Allow every user to access the CDP Agent Tool besides root user'
  53.     echo '     "--serverip=ipAddr"  Set the default server to connect, eg: --serverip=10.10.10.10'
  54.    
  55.     exit 1
  56. }
  57.  
  58. confirm()
  59. {
  60.     #auto update case, return true
  61.     if [ "$MODE" = "non-interactive" ]; then
  62.         return 0
  63.     fi
  64.     #other case, interactive mode, ask for confirm
  65.     local yorn
  66.     while true;
  67.     do
  68.         echo -n $1
  69.         read yorn
  70.         [ "$yorn" == "" ] && yorn=$2
  71.         case $yorn in
  72.         [nN])
  73.             return 1;;
  74.         [yY])
  75.             return 0;;
  76.         esac
  77.     done
  78. }
  79.  
  80. setinstallpath()
  81. {
  82.     if [ "$ACCESSTYPE" = "all" ]; then
  83.         INSTALLDIR=/usr/bin
  84.         TMPDIR=/usr/bin/cdp/install
  85.         ACCESSMODE="755"
  86.     fi
  87. }
  88.  
  89. distinfo()
  90. {
  91.     [ -e /etc/mandrake-release ] && { DIST=mandrake; return; }
  92.     [ -e /etc/redhat-release ] && { DIST=redhat; return; }
  93.     [ -e /etc/SuSE-release ] && { DIST=suse; return; }
  94.     [ -e /etc/debian_version ] && { DIST=debian; return; }
  95.     [ -e /etc/turbolinux-release ] && { DIST=turbo; return; }
  96.     [ -e /etc/mach_init.d ] && { DIST=mac; return; }
  97.     local issue=/etc/issue
  98.     if [ -e $issue ]; then
  99.         grep -i debian $issue && { DIST=debian; return; }
  100.         grep -i "red *hat" $issue && { DIST=redhat; return; }
  101.         grep -i "suse\|s\.u\.s\.e" $issue && { DIST=suse; return; }
  102.         grep -i "caldera" $issue && { DIST=caldera; return; }
  103.     fi
  104.  
  105.     die "Your Linux distribution is not currently supported by the CDP Agent Tool!"
  106. }
  107.  
  108. get_old_pkg_version()
  109. {
  110.     if [ -s /etc/cdp/version ]; then
  111.         read VERSION_OLD </etc/cdp/version
  112.     fi
  113. }
  114.  
  115. get_new_pkg_version()
  116. {
  117.     tar xzf $SRCPKG version >/dev/null
  118.     if [ $? -ne 0 ]; then
  119.         echo "Get version of the new pkg, Could not find version file in source pkg!" | tee -a ${LOGFILE}
  120.     else
  121.         if [ -s $SRCDIR/version ]; then
  122.             read VERSION_NEW <$SRCDIR/version
  123.             rm -f $SRCDIR/version
  124.         else
  125.             echo "Get version of the new pkg, No version info in version file!" | tee -a ${LOGFILE}
  126.         fi
  127.     fi  
  128. }
  129.  
  130. get_installed_version()
  131. {
  132.     if [ -s /etc/cdp/version ]; then
  133.         read VERSION </etc/cdp/version
  134.         return 0
  135.     fi
  136.     return 1
  137. }
  138.  
  139. lib_config_install()
  140. {
  141.     cp -f $SRCDIR/cdpinstall.sh $INSTALLDIR/cdp
  142.  
  143.   #here is modified by Jowis to update serverList info in config.xml, begin
  144.     local serverInfo="   <serverList ip=\"$SERVERIP\" port=\"1000\" isdefault=\"1\" name=\"\" id=\"\" key=\"\" />"
  145.     local tempInfo="<serverList ip=\"$SERVERIP\" port=\"1000\" isdefault=\"1\" name=\"\" id=\"\" key=\"\" />"
  146.  
  147.     #just show the default server ip here if you have set
  148.     [ -n "$SERVERIP" ] && echo "lib_config_install, default server info: $tempInfo" | tee -a ${LOGFILE}
  149.  
  150.     #you input the option --serverip=x.x.x.x when you execute this script
  151.     if [ -e /etc/cdp/config.xml ] && [ -f /etc/cdp/config.xml ]; then
  152.         cat /etc/cdp/config.xml | grep "<serverList*\>" >temp
  153.         #there is a server list in /etc/cdp/config.xml
  154.         if [ -s temp ]; then
  155.             if [ -n "$SERVERIP" ]; then
  156.                 if grep "<serverList ip=\"$SERVERIP\"*\>" temp >/dev/null 2>&1 ; then
  157.                     sed -i 's/isdefault="1"/isdefault="0"/g' temp
  158.                     sed -i "/ip=\"$SERVERIP\"/ s/isdefault=\"0\"/isdefault=\"1\"/" temp
  159.                 else
  160.                     sed -i 's/isdefault="1"/isdefault="0"/g' temp
  161.                     sed -i "1i\ $serverInfo" temp
  162.                 fi
  163.             fi
  164.         #no server list in /etc/cdp/config.xml
  165.         else
  166.             if [ -n "$SERVERIP" ]; then
  167.                 echo " $serverInfo" >>temp
  168.             fi
  169.         fi
  170.         #add new server list to config.xml
  171.         chmod 644 $TMPDIR/config.xml
  172.         sed -i '/<AgentConfig*/ r temp' $TMPDIR/config.xml
  173.         rm -rf temp
  174.     #you do not input the --serverip option, do following
  175.     elif [ -n "$SERVERIP" ]; then
  176.         chmod 644 $TMPDIR/config.xml
  177.         sed -i "/<AgentConfig*/ a\ $serverInfo" $TMPDIR/config.xml
  178.     fi
  179.   #here is modified by Jowis to update serverList in config.xml, end
  180.    
  181.     cp $TMPDIR/config.xml /etc/cdp/config.xml && chmod 777 /etc/cdp/config.xml
  182.     cp $TMPDIR/version /etc/cdp/version
  183.     cp $TMPDIR/lib/* /usr/lib/cdp/. && chmod 644 /usr/lib/cdp/*
  184.     cp $TMPDIR/lib/libbotan-1.10.so.0 /usr/lib/libbotan-1.10.so.0 && chmod 644 /usr/lib/libbotan-1.10.so.0
  185. }
  186.  
  187. check_src()
  188. {
  189.     echo "Verifying install package..." | tee -a ${LOGFILE}
  190.     [ -f $SRCPKG ] || die "Installation failed. $SRCPKG is missing!"
  191.     local f
  192.     local missing=
  193.     for f in config.xml pidof shm_cleanup snwlget tput AdobeAIRInstaller.bin autoupdate.sh functions vars watchdog.sh patchnow version cdpagentservice cdpdaemon.redhat cdpdaemon.suse cdpdaemon.debian cdpagentd cdpagentproxyd cdpagentproxy cdpcli CDPAgentTool.desktop FlexAgentApp32.png;
  194.     do
  195.         tar tzf $SRCPKG $f > /dev/null || missing="$missing $f"
  196.     done
  197.     [ "$missing" == "" ] ||
  198.         die "Installation failed. The following source file(s) can't be found:$missing"
  199. }
  200.  
  201. check_running()
  202. {
  203.     if ps -Ao comm | grep cdpagenttool > /dev/null;  then
  204.         confirm "The CDPAgentTool is running. Do you want to stop it? [y/n]" ||
  205.             die "Installation aborted."
  206.            
  207.        pid=`ps -C cdpagenttool -o pid,args | grep cdpagenttool | awk '{print $1}'`
  208.        kill -TERM $pid && echo "Stop CDPAgentTool Success" | tee -a ${LOGFILE}
  209.     fi
  210.  
  211.     if ps -Ao comm | grep cdpagentproxy > /dev/null; then
  212.         confirm "The CDPAgentProxy process is running. Do you want to stop it? [y/n]" ||
  213.             die "Installation aborted."
  214.  
  215.         /etc/init.d/cdpagentproxyd stop | tee -a ${LOGFILE}
  216.     fi
  217.    
  218.     if ps -Ao comm | grep cdpagentservice > /dev/null; then
  219.         confirm "The CDPBackupService process is running. Do you want to stop it? [y/n]" ||
  220.             die " Installation aborted."
  221.  
  222.         /etc/init.d/cdpagentd stop | tee -a ${LOGFILE}
  223.     fi
  224. }
  225.  
  226. check_installed()
  227. {
  228.     echo "Checking for previous installation..." | tee -a ${LOGFILE}
  229.     local instscript=
  230.     if [ -f /usr/bin/cdp/cdpinstall.sh ]; then
  231.         instscript=/usr/bin/cdp/cdpinstall.sh
  232.     elif [ -f /sbin/cdp/cdpinstall.sh ]; then
  233.         instscript=/sbin/cdp/cdpinstall.sh
  234.     #no cdpagent installed in you computer, we will return here
  235.     else
  236.         check_running
  237.         return
  238.     fi
  239.  
  240.     #check installed pkg version
  241.     get_old_pkg_version
  242.     get_new_pkg_version
  243.  
  244.     if [ "$VERSION_OLD" != "$VERSION_NEW" ]; then
  245.         echo "You currently have version '$VERSION_OLD' installed." | tee -a ${LOGFILE}
  246.     else
  247.         echo "The current version '$VERSION_NEW' is already installed." | tee -a ${LOGFILE}
  248.     fi
  249.  
  250.     confirm "Would you like to uninstall the current version and proceed with the install? [y/n] " ||
  251.         die "No install actions performed. The current version must be uninstalled first. "
  252.     uninstall
  253. }
  254.  
  255. prep_install()
  256. {
  257.     echo "Preparing to install CDP Tools..." | tee -a ${LOGFILE}
  258.     rm -f /etc/cdp/version
  259.     rm -rf $INSTALLDIR/cdp
  260.     rm -rf /usr/lib/cdp
  261.  
  262.     mkdir -p $INSTALLDIR/cdp  2>&1 | tee -a ${LOGFILE}
  263.     mkdir -p $INSTALLDIR/cdp/watchdog 2>&1 | tee -a ${LOGFILE}
  264.     mkdir -p $INSTALLDIR/cdp/watchdog/download 2>&1 | tee -a ${LOGFILE}
  265.     mkdir -p $INSTALLDIR/cdp/watchdog/update 2>&1 | tee -a ${LOGFILE}
  266.  
  267.     mkdir -p /etc/cdp 2>&1 | tee -a ${LOGFILE}
  268.     mkdir -p /usr/bin/cdp 2>&1 | tee -a ${LOGFILE}
  269.     mkdir -p /usr/lib/cdp 2>&1 | tee -a ${LOGFILE}
  270.     mkdir -p /var/log/cdp 2>&1 | tee -a ${LOGFILE}
  271.     chmod a+rwx /var/log/cdp
  272.     chmod a+w /etc/cdp
  273.  
  274.     mkdir -p $TMPDIR 2>&1 | tee -a ${LOGFILE}
  275.     cd $TMPDIR
  276.     tar zxf $SRCPKG >/dev/null 2>&1 | tee -a ${LOGFILE}
  277.     lib_config_install
  278. }
  279.  
  280. install_daemon()
  281. {
  282.     echo "Installing CDP Agent Daemons..." | tee -a ${LOGFILE}
  283.  
  284.     cp $TMPDIR/$CDPAUTOUPDATE  $INSTALLDIR/cdp/cdpautoupdate && chmod 744 $INSTALLDIR/cdp/cdpautoupdate
  285.     cp $TMPDIR/autoupdate.sh  $INSTALLDIR/cdp/autoupdate.sh && chmod 744 $INSTALLDIR/cdp/autoupdate.sh
  286.     cp $TMPDIR/$CDPAGENTSERVICE  $INSTALLDIR/cdp/cdpagentservice && chmod 744 $INSTALLDIR/cdp/cdpagentservice
  287.     cp $TMPDIR/$CDPAGENTPROXY $INSTALLDIR/cdp/$CDPAGENTPROXY && chmod $ACCESSMODE $INSTALLDIR/cdp/$CDPAGENTPROXY    
  288.     cp $TMPDIR/watchdog.sh  $INSTALLDIR/cdp/watchdog/watchdog.sh  && chmod 744 $INSTALLDIR/cdp/watchdog/watchdog.sh
  289.     cp $TMPDIR/functions  $INSTALLDIR/cdp/watchdog/functions && chmod 744 $INSTALLDIR/cdp/watchdog/functions
  290.     cp $TMPDIR/patchnow  $INSTALLDIR/cdp/watchdog/patchnow && chmod 744 $INSTALLDIR/cdp/watchdog/patchnow
  291.     cp $TMPDIR/version  $INSTALLDIR/cdp/watchdog/version  && chmod 744 $INSTALLDIR/cdp/watchdog/version
  292.     cp $TMPDIR/vars  $INSTALLDIR/cdp/watchdog/vars  && chmod 744 $INSTALLDIR/cdp/watchdog/vars
  293.     cp $TMPDIR/shm_cleanup  $INSTALLDIR/cdp/shm_cleanup && chmod 744 $INSTALLDIR/cdp/shm_cleanup
  294.    
  295.     case $DIST in
  296.     redhat | mandrake | turbo | caldera)
  297.     cp $TMPDIR/cdpdaemon.redhat $INSTALLDIR/cdp/cdpdaemon && chmod 744 $INSTALLDIR/cdp/cdpdaemon
  298.     cp $TMPDIR/cdpagentd /etc/rc.d/init.d/cdpagentd && chmod 744 /etc/rc.d/init.d/cdpagentd
  299.     cp $TMPDIR/cdpagentproxyd /etc/rc.d/init.d/cdpagentproxyd && chmod 744 /etc/rc.d/init.d/cdpagentproxyd
  300.     for level in 3 4 5;
  301.     do
  302.         ln -sf /etc/rc.d/init.d/cdpagentd /etc/rc.d/rc${level}.d/S99cdpagentd;
  303.         ln -sf /etc/rc.d/init.d/cdpagentproxyd /etc/rc.d/rc${level}.d/S99cdpagentproxyd;
  304.     done
  305.     for level in 1 2 6;
  306.     do
  307.         ln -sf /etc/rc.d/init.d/cdpagentd /etc/rc.d/rc${level}.d/K99cdpagentd;
  308.         ln -sf /etc/rc.d/init.d/cdpagentproxyd /etc/rc.d/rc${level}.d/K99cdpagentproxyd;
  309.     done
  310.     /etc/rc.d/init.d/cdpagentd start | tee -a ${LOGFILE}
  311.     /etc/rc.d/init.d/cdpagentproxyd start | tee -a ${LOGFILE}
  312.     ;;
  313.  
  314.     suse)
  315.     cp $TMPDIR/cdpdaemon.suse $INSTALLDIR/cdp/cdpdaemon && chmod 744 $INSTALLDIR/cdp/cdpdaemon
  316.     cp $TMPDIR/cdpagentd /etc/init.d/cdpagentd && chmod 744 /etc/init.d/cdpagentd
  317.     cp $TMPDIR/cdpagentproxyd /etc/init.d/cdpagentproxyd && chmod 744 /etc/init.d/cdpagentproxyd
  318.     for level in 3 4 5;
  319.     do
  320.         ln -sf /etc/init.d/cdpagentd /etc/init.d/rc${level}.d/S99cdpagentd;
  321.         ln -sf /etc/init.d/cdpagentproxyd /etc/init.d/rc${level}.d/S99cdpagentproxyd;        
  322.     done
  323.     for level in 1 2 6;
  324.     do
  325.         ln -sf /etc/init.d/cdpagentd /etc/init.d/rc${level}.d/K99cdpagentd;
  326.         ln -sf /etc/init.d/cdpagentproxyd /etc/init.d/rc${level}.d/K99cdpagentproxyd;
  327.     done
  328.     /etc/init.d/cdpagentd start | tee -a ${LOGFILE}
  329.     /etc/init.d/cdpagentproxyd start | tee -a ${LOGFILE}
  330.     ;;
  331.  
  332.     debian)
  333.     cp $TMPDIR/cdpdaemon.debian $INSTALLDIR/cdp/cdpdaemon && chmod 744 $INSTALLDIR/cdp/cdpdaemon
  334.     cp $TMPDIR/cdpagentd /etc/init.d/cdpagentd && chmod 744 /etc/init.d/cdpagentd
  335.     cp $TMPDIR/cdpagentproxyd  /etc/init.d/cdpagentproxyd && chmod 744 /etc/init.d/cdpagentproxyd
  336.     for level in 2 3 4 5;
  337.     do
  338.         ln -sf /etc/init.d/cdpagentd /etc/rc${level}.d/S99cdpagentd;
  339.         ln -sf /etc/init.d/cdpagentproxyd /etc/rc${level}.d/S99cdpagentproxyd;
  340.     done
  341.     for level in 1 6 S;
  342.     do
  343.         ln -sf /etc/init.d/cdpagentd /etc/rc${level}.d/K99cdpagentd;
  344.         ln -sf /etc/init.d/cdpagentproxyd /etc/rc${level}.d/K99cdpagentproxyd;
  345.     done
  346.     /etc/init.d/cdpagentd start | tee -a ${LOGFILE}
  347.     /etc/init.d/cdpagentproxyd start | tee -a ${LOGFILE}
  348.     ;;
  349.     esac
  350. }
  351.  
  352. install_utilities()
  353. {
  354.     cp $TMPDIR/tput  $INSTALLDIR/cdp/tput && chmod $ACCESSMODE $INSTALLDIR/cdp/tput
  355.     cp $TMPDIR/pidof  $INSTALLDIR/cdp/pidof && chmod $ACCESSMODE $INSTALLDIR/cdp/pidof
  356.     cp $TMPDIR/snwlget  $INSTALLDIR/cdp/snwlget && chmod $ACCESSMODE $INSTALLDIR/cdp/snwlget
  357. }
  358.  
  359. install_agent_tool()
  360. {
  361.     echo "Installing CDP Agent Tool..." | tee -a ${LOGFILE}
  362.    
  363.     #install Adobe AIR
  364.     $TMPDIR/AdobeAIRInstaller.bin -silent -eulaAccepted | tee -a ${LOGFILE}
  365.     if [ -s "/opt/Adobe AIR" ]; then
  366.         cp -r $TMPDIR/CDPAgentTool $INSTALLDIR/cdp/ && chmod -R $ACCESSMODE $INSTALLDIR/cdp/CDPAgentTool
  367.     else
  368.         die "Adobe AIR Installation fails..."
  369.     fi
  370.  
  371.     cp $TMPDIR/FlexAgentApp32.png $INSTALLDIR/cdp/FlexAgentApp32.png
  372.     cp $TMPDIR/CDPAgentTool.desktop $INSTALLDIR/cdp/CDPAgentTool.desktop
  373.     cp $TMPDIR/cdpcli $INSTALLDIR/cdp/cdpcli && chmod $ACCESSMODE $INSTALLDIR/cdp/cdpcli
  374.  
  375.     ln -s $INSTALLDIR/cdp/CDPAgentTool/bin/CDPAgentTool $INSTALLDIR/cdpagenttool && chmod $ACCESSMODE $INSTALLDIR/cdpagenttool
  376.  
  377.     #create desktop icon
  378.     if [ "$ACCESSTYPE" = "all" ]; then
  379.         #add agenttool icon to all users in /home
  380.         for i in /home/*;
  381.         do
  382.             #avoid creating symbolic link in /home/lost+found if this dir exists
  383.             [ "$i" == "/home/lost+found" ] && continue
  384.             ln -sf $INSTALLDIR/cdp/CDPAgentTool.desktop $i/Desktop/CDPAgentTool.desktop && chmod 777 /$i/Desktop/CDPAgentTool.desktop
  385.         done
  386.         #add agenttool icon to root desktop
  387.         if [ -d /root/Desktop ]; then
  388.             ln -sf $INSTALLDIR/cdp/CDPAgentTool.desktop /root/Desktop/CDPAgentTool.desktop && chmod 744 /root/Desktop/CDPAgentTool.desktop
  389.         fi
  390.     else
  391.        #access type is root, we need to modify the properties of desktop icon to avoid invalid command path
  392.        sed -i 's/usr\/bin/sbin/g' $INSTALLDIR/cdp/CDPAgentTool.desktop
  393.        ln -sf $INSTALLDIR/cdp/CDPAgentTool.desktop /root/Desktop/CDPAgentTool.desktop && chmod 744 /root/Desktop/CDPAgentTool.desktop
  394.     fi
  395. }
  396.  
  397. uninstall()
  398. {
  399.     confirm "Are you sure you want to uninstall the CDP Agent Tool? [y/n]" || exit 1
  400.  
  401.     check_running
  402.  
  403.     echo "Uninstalling CDP Agent Tool..." | tee -a ${LOGFILE}
  404.     # uninstall service
  405.     case $DIST in
  406.     redhat | mandrake | turbo | caldera)
  407.         rm -f /etc/rc.d/init.d/cdpagentd /etc/rc.d/rc?.d/[SK]99cdpagentd
  408.         rm -f /etc/rc.d/init.d/cdpagentdproxyd /etc/rc.d/rc?.d/[SK]99cdpagentproxyd
  409.         ;;
  410.     suse)
  411.         rm -f /etc/init.d/cdpagentd /etc/init.d/rc?.d/[SK]99cdpagentd
  412.         rm -f /etc/init.d/cdpagentproxyd /etc/init.d/rc?.d/[SK]99cdpagentproxyd
  413.         ;;
  414.     debian)
  415.         rm -f /etc/init.d/cdpagentd /etc/rc?.d/[SK]99cdpagentd
  416.         rm -f /etc/init.d/cdpagentproxyd /etc/rc?.d/[SK]99cdpagentproxyd
  417.         ;;
  418.     esac
  419.    
  420.     rm -f /etc/cdp/version
  421.     rm -rf /usr/bin/cdp
  422.     rm -rf /sbin/cdp
  423.     rm -rf /usr/lib/cdp
  424.     rm -f /usr/bin/cdpagenttool
  425.     rm -f /sbin/cdpagenttool
  426.  
  427.     for i in /home/*;
  428.     do
  429.         rm -f $i/Desktop/CDPAgentTool.desktop;
  430.     done
  431.  
  432.     [ -d /root/Desktop ] && rm -f /root/Desktop/CDPAgentTool.desktop
  433. }
  434.  
  435. remove_tmpdir()
  436. {
  437.     echo "Removing temporary directory..." | tee -a ${LOGFILE}
  438.     rm -rf $TMPDIR
  439. }
  440.  
  441. install_success()
  442. {
  443.     get_installed_version
  444.     echo "CDPAgentTool $VERSION has been installed successfully..." | tee -a ${LOGFILE}
  445.     echo "Type \"cdpagenttool\" to start CDPAgentTool." | tee -a ${LOGFILE}
  446.     echo "Or use the Desktop icon to start CDPAgentTool." | tee -a ${LOGFILE}
  447. }
  448.  
  449. #here is added by Jowis, begin
  450. check_ipAddr()
  451. {
  452.     local serverip=$1
  453.     local ip_byte1=0
  454.     local ip_byte2=0
  455.     local ip_byte3=0
  456.     local ip_byte4=0
  457.     if [ -z "$serverip" ]; then
  458.         echo "IP address is null, use a line like --serverip=10.10.10.10" | tee -a ${LOGFILE}
  459.         return 1
  460.     else
  461.         temp_str=`echo $serverip | sed 's/^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$//'`
  462.         if [ -z "$temp_str" ]; then
  463.             ip_byte1=`echo $serverip | cut -f 1 -d "."`
  464.             ip_byte2=`echo $serverip | cut -f 2 -d "."`
  465.             ip_byte3=`echo $serverip | cut -f 3 -d "."`
  466.             ip_byte4=`echo $serverip | cut -f 4 -d "."`
  467.             [ $ip_byte1 -le 255 ] && [ $ip_byte2 -le 255 ] && [ $ip_byte3 -le 255 ] && [ $ip_byte4 -le 255 ] && return 0
  468.             return 1
  469.         else
  470.             return 1
  471.         fi
  472.     fi
  473. }
  474.  
  475. check_log_file()
  476. {
  477.     #check whether the log dir exists or not
  478.     if [ ! -d /var/log/cdp ]; then
  479.         mkdir -p /var/log/cdp
  480.     fi
  481.  
  482.     #add current time to log file
  483.     local curTime=`date +"Time: %F %T"`
  484.     echo "$curTime execute cdpinstall.sh..." > ${LOGFILE}
  485. }
  486.  
  487. parse_command()
  488. {
  489.     #argument number is valid?
  490.     [ $# -eq 0 ] && { echo "Failed, command parameter is null!" | tee -a ${LOGFILE}; usage; }
  491.     [ $# -gt 4 ] && { echo "Failed, invalid parameter number!" | tee -a ${LOGFILE}; usage; }
  492.  
  493.     #retrieve command and options
  494.     for loop in $*
  495.     do
  496.        case $1 in
  497.  
  498.        -i|-d|-a|-u|-v|--version)
  499.           if [ -n "$commands" ] ; then
  500.              echo "Failed, commands $commands and $1 can not used at the same time!" | tee -a ${LOGFILE}
  501.              usage
  502.           fi
  503.           commands=$1
  504.           shift;;
  505.  
  506.        --access=all)
  507.           ACCESSTYPE="all"
  508.           shift;;
  509.  
  510.        --serverip=*)
  511.           SERVERIP=${1#"--serverip="}
  512.           check_ipAddr "$SERVERIP" || { echo "Failed, invalid IP address!" | tee -a ${LOGFILE}; usage; }
  513.           shift;;
  514.    
  515.        --autoUpdate)
  516.           AutoUpdateFlag="true"
  517.           MODE="non-interactive"
  518.           shift;;
  519.  
  520.        -y|-Y)
  521.           MODE="non-interactive"
  522.           shift;;
  523.  
  524.        *)
  525.           echo "Failed, invalid command or option: $1" | tee -a ${LOGFILE}
  526.           usage;;
  527.        esac
  528.     done
  529.  
  530.     #has invalid command?
  531.     [ "$commands" == "" ] && { echo "Failed, no command found!" | tee -a ${LOGFILE}; usage; }
  532. }
  533.  
  534. get_last_used_options()
  535. {
  536.     echo "You did not enter any input command parameters, Get the last used parameters from /etc/cdp/install_params if it exists" | tee -a ${LOGFILE}
  537.     if [ -s /etc/cdp/install_params ]; then
  538.         read argument </etc/cdp/install_params
  539.         echo "Current command parameters will be set to \"$argument\"" | tee -a ${LOGFILE}
  540.     else
  541.         echo "File /etc/cdp/install_params does not exist, Please refer to the usage summary." | tee -a ${LOGFILE}
  542.     fi
  543. }
  544.  
  545. save_last_used_options()
  546. {
  547.     #no need to save in case of get version or autoupdate
  548.     [ "$commands" == "-v" ] && return
  549.     [ "$commands" == "--version" ] && return
  550.     [ "$commands" == "-u" ] && return
  551.     [ "$AutoUpdateFlag" == "true" ] && return
  552.  
  553.     if [ ! -d /etc/cdp ]; then
  554.         mkdir -p /etc/cdp
  555.     fi
  556.     #save commands and options to file
  557.     echo "$argument" >/etc/cdp/install_params
  558.     echo "Saving current command parameters to file /etc/cdp/install_params" | tee -a ${LOGFILE}
  559. }
  560. #here is added by Jowis, end
  561.  
  562. ########################################################################
  563. #check whether we support current OS distribution or not
  564. distinfo
  565.  
  566. #check and add current time to log
  567. check_log_file
  568.  
  569. #if no input command and options, retrieve last used to $argument
  570. argument=$*
  571. [ $# -eq 0 ] && get_last_used_options
  572.  
  573. #parse and do command and options checking
  574. parse_command $argument
  575.  
  576. #save current command and options
  577. [ $# -ne 0 ] && save_last_used_options
  578.  
  579. #set install path according to access type
  580. setinstallpath
  581.  
  582. #exit 0
  583.  
  584. #core installation procedure
  585. case $commands in
  586. -i)
  587.     check_src
  588.     check_installed
  589.     prep_install
  590.     install_utilities    
  591.     install_daemon
  592.     install_agent_tool
  593.     remove_tmpdir
  594.     install_success
  595.     ;;
  596. -u)
  597.     uninstall
  598.     ;;
  599. -d)
  600.     check_src
  601.     check_installed
  602.     prep_install
  603.     install_daemon
  604.     remove_tmpdir
  605.     ;;
  606. -a)    
  607.     check_src
  608.     check_installed
  609.     prep_install
  610.     install_agent_tool
  611.     remove_tmpdir
  612.     ;;    
  613. -v|--version)
  614.     get_installed_version && echo $VERSION
  615.     ;;
  616. *)
  617.     usage
  618.     ;;
  619. esac
  620. exit 0
  621. ######################################################################
Add Comment
Please, Sign In to add comment