#!/usr/bin/expect
set ipaddr [lindex $argv 0]
set pwdi [lindex $argv 1]
set type1 [lindex $argv 2]
set memaddr [lindex $argv 3]
set dbaddr [lindex $argv 4]
set app1addr [lindex $argv 5]
set app2addr [lindex $argv 6]
set domain [lindex $argv 7]
set pwd1 "普通密码"
set pwd2 "ROOT密码"
set center "数据中心IP"
set timeout 3600
# ssh to target machine
spawn ssh -p 22 root@$ipaddr
# expect "(yes/no)?"
# send "yes\n"
set timeout 3600
expect "password:"
send "$pwdi\n"
# backup etc
expect "#"
send "cd /\n"
expect "#"
send "tar czf etc.tar.gz etc\n"
expect "#"
send "mv -f etc.tar.gz /root\n"
expect "#"
send "cd /root\n"
# get system package
expect "#"
send "sftp -oPort=60022 test@$center\n"
expect "(yes/no)?"
send "yes\n"
expect "password:"
send "$pwd1\n"
expect "sftp>"
send "cd system/install\n"
expect "sftp>"
send "get system.$type1.tar.gz\n"
expect "sftp>"
send "get src.tar.gz\n"
expect "sftp>"
send "quit\n"
expect "#"
send "tar zxf system.$type1.tar.gz\n"
expect "#"
send "cd /root/system.$type1\n"
expect "#"
send "cp /root/src.tar.gz .\n"
expect "#"
send "tar zxf src.tar.gz\n"
# init user
expect "#"
send "cd /root/system.$type1/script\n"
expect "#"
send "./add_user.sh\n"
expect "New UNIX password:"
send "$pwd1\n"
expect "Retype new UNIX password:"
send "$pwd1\n"
expect "#"
send "passwd root\n"
expect "New UNIX password:"
send "$pwd2\n"
expect "Retype new UNIX password:"
send "$pwd2\n"
# init etc
expect "#"
send "cd /root/system.$type1/etc\n"
expect "#"
send "awk 'BEGIN { cmd=\"cp -fpR * /etc\"; print \"y\" |cmd; }'\n"
expect "#"
send "cd /etc/rc3.d\n"
expect "#"
send "./update_service.sh\n"
expect "#"
send "rm -f update_service.sh\n"
# yum install
expect "#"
send "cd /root/system.$type1/script\n"
expect "#"
send "./yum_install.sh\n"
expect "N]:"
send "y\n"
expect "script]#"
# package install
# libevent
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf libevent-1.4.8-stable.tar.gz\n"
expect "#"
send "cd libevent-1.4.8-stable\n"
expect "libevent-1.4.8-stable]#"
send "./configure --prefix=/usr\n"
expect "libevent-1.4.8-stable]#"
send "make\n"
expect "libevent-1.4.8-stable]#"
send "make install\n"
# libiconv
expect "libevent-1.4.8-stable]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf libiconv-1.12.tar.gz\n"
expect "#"
send "cd libiconv-1.12\n"
expect "libiconv-1.12]#"
send "./configure --prefix=/usr\n"
expect "libiconv-1.12]#"
send "make\n"
expect "libiconv-1.12]#"
send "make install\n"
# mysql
expect "libiconv-1.12]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf mysql-5.0.45.tar.gz\n"
expect "#"
send "cd mysql-5.0.45\n"
expect "mysql-5.0.45]#"
send "./configure --prefix=/home/mysql --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312,latin1 --with-pthread --enable-thread-safe-client 'CFLAGS=-O3 -DDBUG_OFF' 'CXXFLAGS=-DDBUG_OFF -O3 -fno-exceptions -fno-rtti'\n"
expect "mysql-5.0.45]#"
send "make\n"
expect "mysql-5.0.45]#"
send "make install\n"
expect "mysql-5.0.45]#"
send "cd /home/mysql/bin\n"
expect "#"
send "./mysql_install_db --user=mysql --group=mysql --datadir=/home/mysql/data\n"
# memcached
expect "#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf memcached-1.2.6.tar.gz\n"
expect "#"
send "cd memcached-1.2.6\n"
expect "memcached-1.2.6]#"
send "./configure --prefix=/home/memcached\n"
expect "memcached-1.2.6]#"
send "make\n"
expect "memcached-1.2.6]#"
send "make install\n"
# ntp
expect "memcached-1.2.6]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf ntp-4.2.4p4.tar.gz\n"
expect "#"
send "cd ntp-4.2.4p4\n"
expect "ntp-4.2.4p4]#"
send "./configure --prefix=/home/test/ntp\n"
expect "ntp-4.2.4p4]#"
send "make\n"
expect "ntp-4.2.4p4]#"
send "make install\n"
# nginx
expect "ntp-4.2.4p4]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf nginx-0.7.61.tar.gz\n"
expect "#"
send "cd nginx-0.7.61\n"
expect "nginx-0.7.61]#"
send "./configure --user=nobody --group=nobody --with-http_ssl_module --prefix=/home/nginx --with-cc-opt='-O3'\n"
expect "nginx-0.7.61]#"
send "make\n"
expect "nginx-0.7.61]#"
send "make install\n"
# spawn-fcgi
expect "nginx-0.7.61]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf lighttpd-1.4.19.tar.gz\n"
expect "#"
send "cd lighttpd-1.4.19\n"
expect "lighttpd-1.4.19]#"
send "./configure\n"
expect "lighttpd-1.4.19]#"
send "make\n"
expect "lighttpd-1.4.19]#"
send "cd src\n"
expect "#"
send "cp spawn-fcgi /home/nginx/sbin\n"
# php
expect "#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf php-5.2.6.tar.gz\n"
expect "#"
send "cd php-5.2.6\n"
expect "php-5.2.6]#"
send "./configure --prefix=/home/test/php --with-mysql=/home/mysql --with-pdo-mysql=/home/mysql --with-png-dir=/usr --with-jpeg-dir=/usr --with-zlib=/usr --with-freetype-dir=/usr --with-gd=/usr --enable-xml --with-libxml-dir=/usr --with-openssl=/usr --enable-trans-sid --enable-magic-quotes --enable-memory-limit --enable-track-vars --enable-discard-path --disable-debug --enable-sockets --enable-mbstring=all --with-gettext --with-iconv --with-config-file-path=/home/test/php/etc/ --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-pcntl --with-iconv=/usr --disable-inline-optimization --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-zend-multibyte\n"
expect "php-5.2.6]#"
send "make\n"
expect "php-5.2.6]#"
send "make install\n"
# php memcache
expect "php-5.2.6]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf memcache-3.0.2.tar.gz\n"
expect "#"
send "cd memcache-3.0.2\n"
expect "memcache-3.0.2]#"
send "./configure '--with-php-config=/home/test/php/bin/php-config' '-with-gettext' '--enable-socket' '--enable-memcache' '--enable-sysvshm' '--enable-shmop'\n"
expect "memcache-3.0.2]#"
send "make\n"
expect "memcache-3.0.2]#"
send "make install\n"
# php xcache
expect "memcache-3.0.2]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf xcache-2.0-beta.tar.gz\n"
expect "#"
send "cd xcache-2.0-beta\n"
expect "xcache-2.0-beta]#"
send "./configure '--with-php-config=/home/test/php/bin/php-config' '--enable-xcache'\n"
expect "xcache-2.0-beta]#"
send "make\n"
expect "xcache-2.0-beta]#"
send "make install\n"
# chkrootkit
expect "xcache-2.0-beta]#"
send "cd /root/system.$type1/src\n"
expect "#"
send "tar zxf chkrootkit.tar.gz\n"
expect "#"
send "cd chkrootkit-0.48\n"
expect "chkrootkit-0.48]#"
send "make\n"
expect "chkrootkit-0.48]#"
send "mkdir /home/test/chkrootkit\n"
expect "#"
send "mkdir /home/test/chkrootkit/sbin\n"
expect "#"
send "mkdir /home/test/chkrootkit/logs\n"
expect "#"
send "chown test /home/test/chkrootkit\n"
expect "#"
send "cp -fp check_wtmpx chkdirs chklastlog chkproc chkrootkit chkutmp chkwtmp ifpromisc strings-static /home/test/chkrootkit/sbin\n"
#post config
expect "#"
send "ldconfig\n"
expect "#"
send "rm -f /etc/my.cnf\n"
expect "#"
send "cp -f /root/system.$type1/script/chkrootkit.sh /home/test/chkrootkit/sbin\n"
expect "#"
send "rm -f /home/test/rkhunter/etc/rkhunter.conf\n"
expect "#"
send "cp -f /root/system.$type1/conf/rkhunter.conf /home/test/rkhunter/etc\n"
expect "#"
send "cp -f /root/system.$type1/script/rkhunter.sh /home/test/rkhunter/bin\n"
expect "#"
send "cp -f /root/system.$type1/conf/zabbix_agentd.conf /home/zabbix/etc\n"
expect "#"
send "cp /root/system.$type1/conf/my.cnf /home/mysql/data\n"
expect "#"
send "mkdir /home/test/php/etc\n"
expect "#"
send "cp /root/system.$type1/conf/php.ini /home/test/php/etc/php.ini.def\n"
expect "#"
send "sed 's/192.168.1.1/$memaddr/g' /home/test/php/etc/php.ini.def > /home/test/php/etc/php.ini\n"
expect "#"
send "cp /root/system.$type1/conf/fcgi.conf /home/nginx/conf\n"
expect "#"
send "rm -f /home/nginx/conf/nginx.conf\n"
expect "#"
send "cp /root/system.$type1/conf/nginx.conf /home/nginx/conf/nginx.conf.def\n"
expect "#"
send "sed 's/192.168.1.2/$dbaddr/g' /ho