转换文档格式
sed -i 's/\r$//' *.sh
1、服务器信息巡检
#!/bin/bash
echo "系统巡检脚本:Version `date +%F`"
echo "信安"
echo -e "****************系统检查****************"
echo "系统:`uname -a | awk '{print $NF}'`"
echo "发行版本:`cat /etc/os-release`"
echo "内核:`uname -r`"
echo "主机名:`hostname`"
echo "SELinux:`/usr/sbin/sestatus | grep 'SELinux status:' | awk '{print $3}'`"
echo "语言/编码:`echo $LANG`"
echo "当前时间:`date +%F_%T`"
echo "最后启动:`who -b | awk '{print $3,$4}'`"
echo "运行时间:`uptime | awk '{print $3}' | sed 's/,//g'`"
echo -e "****************CPU检查 ****************"
echo "物理CPU个数: `cat /proc/cpuinfo | grep "physical id" | awk '{print $4}' | sort | uniq | wc -l`"
echo "逻辑CPU个数: `cat /proc/cpuinfo | grep "processor" | awk '{print $3}' | sort | uniq | wc -l`"
echo "每CPU核心数: `cat /proc/cpuinfo | grep "cores" | awk '{print $4}'`"
echo "CPU型号: `cat /proc/cpuinfo | grep "model name" | awk -F":" '{print $2}'`"
echo "CPU架构: `uname -m`"
echo -e "****************内存检查 ****************"
echo "总共内存:`free -mh | awk "NR==2"| awk '{print $2}'`"
echo "使用内存:`free -mh | awk "NR==2"| awk '{print $3}'` "
echo "剩余内存:`free -mh | awk "NR==2"| awk '{print $4}'`"
echo -e "****************硬盘检查 ****************"
echo "总共磁盘大小:`df -hT | awk "NR==2"|awk '{print $3}'`"
echo -e "****************网络检查 ****************"
echo "IP:`ifconfig | awk 'NR==2' | awk '{print $2}'`"
echo "网关:`ip route | awk 'NR==1'| awk '{print $3}'`"
echo "DNS: `cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}'`"
ping -c 4 www.baidu.com > /dev/null
if [ $? -eq 0 ];then
echo "外网连接:正常"
else
echo "外网连接:失败 请检查DNS配置"
fi
echo -e "****************安全检查****************"
echo "登陆用户信息:`last | grep "still logged in" | awk '{print $1}'| sort | uniq`"
md5sum -c --quiet /etc/passwd > /dev/null 2&>1
2、sh部署jdk+tomcat+ftp被动
将apache-tomcat-8.5.65.tar.gz和jdk-8u291-linux-x64.tar.gz跟脚本放到同级目录
#!/bin/bash
echo "---------------------执行java部署---------------------"
yum -y install net-tools vim unzip
java_home="/usr/local/java"
if [ ! -d $java_home ];then
mkdir -p $java_home
fi
#为了脚本可以重复执行,如果已经安装过,就删除
if [ ! -d "${java_home}/jdk1.8.0_291" ]; then
echo "没有配置过java"
else
rm -rf "${java_home}/jdk1.8.0_291"
fi
echo "解压jdk到$java_home"
tar -zxf jdk-8u291-linux-x64.tar.gz
mv jdk1.* $java_home
echo "配置环境变量"
echo 'export JAVA_HOME='${java_home}'/jdk1.8.0_291' > /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
echo 'export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar' >> /etc/profile
echo "环境变量配置完成"
sleep 2
source /etc/profile
java -version
echo ""
echo "---------------------执行tomcat部署---------------------"
#为了脚本可以重复执行,如果已经安装过,就不操作
tomcat_file=/usr/local/tomcat
if [ ! -d "${tomcat_file}" ]; then
mkdir $tomcat_file
fi
for ((i=1;i<=2;i++));
do
if [ $i == 1 ]; then
${tomcat_file}/Tomcat_DzwlServer/bin/shutdown.sh &> /dev/null
if [ ! -d "${tomcat_file}/Tomcat_DzwlServer" ]; then
echo "创建${tomcat_file}/Tomcat_DzwlServer"
tar -zxvf apache-tomcat-8.5.65.tar.gz
mv apache-tomcat-8.5.65 ${tomcat_file}/Tomcat_DzwlServer
echo "配置Tomcat_DzwlServer开机自启"
echo 'export JAVA_HOME='${java_home}'/jdk1.8.0_291' >> /etc/rc.d/rc.local
echo $tomcat_file'/Tomcat_DzwlServer/bin/startup.sh start' >> /etc/rc.d/rc.local
echo "Tomcat_DzwlServer开机自启已配置"
echo "Tomcat_DzwlServer配置8090端口号"
sed -i 's/Connector port="8080"/Connector port="8090"/' $tomcat_file'/Tomcat_DzwlServer/conf/server.xml'
grep "<Connector port=" $tomcat_file'/Tomcat_DzwlServer/conf/server.xml'
else
echo "已存在${tomcat_file}/Tomcat_DzwlServer"
fi
elif [ $i == 2 ]; then
${tomcat_file}/Tomcat_webdzwlclient/bin/shutdown.sh &> /dev/null
if [ ! -d "${tomcat_file}/Tomcat_webdzwlclient" ]; then
echo "创建${tomcat_file}/Tomcat_webdzwlclient"
tar -zxf apache-tomcat-8.5.65.tar.gz
mv apache-tomcat-8.5.65 ${tomcat_file}/Tomcat_webdzwlclient
echo "配置Tomcat_DzwlServer开机自启"
echo $tomcat_file'/Tomcat_webdzwlclient/bin/startup.sh start' >> /etc/rc.d/rc.local
echo "Tomcat_webdzwlclient开机自启已配置"
grep "<Connector port=" $tomcat_file'/Tomcat_webdzwlclient/conf/server.xml'
else
echo "已存在${tomcat_file}/Tomcat_webdzwlclient"
fi
fi
done
echo ""
echo "---------------------执行FTP配置---------------------"
ftp_name="fddFileServer"
ftp_pwd="fdd"
ftp_dir="/usr/share/dzwl/fddfiletmp"
if [ ! -d "${ftp_dir}" ]; then
mkdir -p $ftp_dir
fi
yum install vsftpd -y
echo "开始修改ftp配置"
sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/connect_from_port_20=YES/connect_from_port_20=NO/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#xferlog_file=/xferlog_file=/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#async_abor_enable=YES/async_abor_enable=YES/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#ascii_upload_enable=YES/ascii_upload_enable=YES/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#ascii_download_enable=YES/ascii_download_enable=YES/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#chroot_local_user=YES/chroot_local_user=YES/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#chroot_list_enable=YES/chroot_list_enable=YES/' '/etc/vsftpd/vsftpd.conf'
sed -i 's/#chroot_list_file=/chroot_list_file=/' '/etc/vsftpd/vsftpd.conf'
echo "local_root=$ftp_dir" >> /etc/rc.d/rc.local
{
cat <<'XUNLEI'
allow_writeable_chroot=YES
pasv_enable=YES
pasv_min_port=61000
pasv_max_port=61049
#listen_port=9421
pasv_address=172.16.0.7
XUNLEI
} >> /etc/vsftpd/vsftpd.conf
echo "开始创建${ftp_name}用户"
useradd -d $ftp_dir $ftp_name
echo $ftp_pwd | passwd --stdin $ftp_name
chmod 777 $ftp_dir
if [ ! -f "/etc/vsftpd/chroot_list" ];then
touch "/etc/vsftpd/chroot_list"
fi
echo $ftp_name >> "/etc/vsftpd/chroot_list"
echo "启动ftp,并加入开机自启"
systemctl restart vsftpd
systemctl enable vsftpd
echo ""
echo "---------------------执行防火墙规则配置---------------------"
echo "开启防火墙并添加端口规则"
systemctl start firewalld
firewall-cmd --add-port=8080/tcp --zone=public --permanent
#firewall-cmd --add-port=8021/tcp--zone=public --permanent
firewall-cmd --add-port=60000-60049/tcp --zone=public --permanent
firewall-cmd --add-service=ftp --permanent
firewall-cmd --reload
3、sh部署oracle11g
将linux.x64_11gR2_database_1of2.zip和linux.x64_11gR2_database_2of2.zip跟脚本放到同级目录 注意:如果脚本跑完测试发现 listenner监听 没启动,就查看host和配置文件的监听名是否一致
#!/bin/bash
oracle_SID="dzwl"
oracle_pwd="oracle"
oracle_basehome="/tmp"
host_name="dzwl"
host_ip="172.16.0.1"
echo "---------------------1、执行host修改---------------------"
yum -y install unzip
yum -y install vim
yum -y install expect
hostnamectl set-hostname $host_name
echo "$host_ip $host_name" >> /etc/hosts
echo "---------------------2、执行防火墙,selinux修改---------------------"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' '/etc/selinux/config'
systemctl stop firewalld
echo "---------------------3、安装oracle依赖包---------------------"
yum -y install gcc make binutils gcc-c++ compat-libstdc++-33 elfutils-libelf-devel elfutils-libelf-devel-static elfutils-libelf-devel ksh libaio libaio-devel numactl-devel sysstat unixODBC unixODBC-devel pcre-devel libXp*
echo "---------------------4、新增oracle用户---------------------"
groupadd oinstall
groupadd dba
useradd -g oinstall -G dba oracle
echo $oracle_pwd | passwd --stdin oracle
echo "---------------------5、修改内核参数---------------------"
{
cat <<'XUNLEI'
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 1073741824
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
XUNLEI
} >> /etc/sysctl.conf
sleep 1
sysctl -p
echo "---------------------6、修改限制文件---------------------"
{
cat <<'XUNLEI'
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
XUNLEI
} >> /etc/security/limits.conf
{
cat <<'XUNLEI'
session required /lib64/security/pam_limits.so
session required pam_limits.so
XUNLEI
} >> /etc/pam.d/login
{
cat <<'XUNLEI'
if [ $USER = "oracle" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi
XUNLEI
} >> /etc/profile
echo "---------------------7、创建安装目录、修改文件所属者和权限---------------------"
mkdir -p /u01/app/oracle/product/11.2.0
mkdir /u01/app/oracle/oradata
mkdir /u01/app/oracle/inventory
mkdir /u01/app/oracle/fast_recovery_area
chown -R oracle:oinstall /u01/app/oracle
chmod -R 777 /u01/app/oracle
echo "---------------------8、解压oracle及配置文件修改---------------------"
cd $oracle_basehome
unzip linux.x64_11gR2_database_1of2.zip
unzip linux.x64_11gR2_database_2of2.zip
cp $oracle_basehome'/database/response/db_install.rsp' $oracle_basehome'/database/response/db_install.rsp.bak'
sed -i 's/oracle.install.option=/oracle.install.option=INSTALL_DB_SWONLY/' $oracle_basehome'/database/response/db_install.rsp'
sed -i "s/ORACLE_HOSTNAME=/ORACLE_HOSTNAME=$oracle_SID/" $oracle_basehome'/database/response/db_install.rsp'
sed -i 's/UNIX_GROUP_NAME=/UNIX_GROUP_NAME=oinstall/' $oracle_basehome'/database/response/db_install.rsp'
sed -i "s/INVENTORY_LOCATION=/INVENTORY_LOCATION=\/u01\/app\/oracle\/inventory/" $oracle_basehome'/database/response/db_install.rsp'
sed -i 's/SELECTED_LANGUAGES=/SELECTED_LANGUAGES=en,zh_CN/' $oracle_basehome'/database/response/db_install.rsp'
sed -i "s/ORACLE_HOME=/ORACLE_HOME=\/u01\/app\/oracle\/product\/11.2.0/" $oracle_basehome'/database/response/db_install.rsp'
sed -i "s/ORACLE_BASE=/ORACLE_BASE=\/u01\/app\/oracle/" $oracle_basehome'/database/response/db_install.rsp'
sed -i 's/oracle.install.db.InstallEdition=/oracle.install.db.InstallEdition=EE/' $oracle_basehome'/database/response/db_install.rsp'
sed -i 's/oracle.install.db.DBA_GROUP=/oracle.install.db.DBA_GROUP=dba/' $oracle_basehome'/database/response/db_install.rsp'
sed -i 's/oracle.install.db.OPER_GROUP=/oracle.install.db.OPER_GROUP=dba/' $oracle_basehome'/database/response/db_install.rsp'
sed -i 's/DECLINE_SECURITY_UPDATES=/DECLINE_SECURITY_UPDATES=true/' $oracle_basehome'/database/response/db_install.rsp'
echo "---------------------9、切换oracle用户继续执行---------------------"
#su - oracle -s /bin/bash su.sh
su - oracle -c "echo ORACLE_BASE=/u01/app/oracle >> ~/.bash_profile"
su - oracle -c 'echo ORACLE_HOME=\$ORACLE_BASE/product/11.2.0 >> ~/.bash_profile'
su - oracle -c "echo ORACLE_SID=$oracle_SID >> ~/.bash_profile"
su - oracle -c 'echo PATH=\$PATH:\$ORACLE_HOME/bin >> ~/.bash_profile'
su - oracle -c "echo export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH >> ~/.bash_profile"
su - oracle -c 'grep -Ev "^#|^$" ~/.bash_profile'
echo "---------------------10、安装数据库---------------------"
su - oracle -c "$oracle_basehome/database/./runInstaller -silent -ignorePrereq -ignoreSysPrereqs -responseFile $oracle_basehome/database/response/db_install.rsp"
sleep 5m
#while true; do
# if [ ! -f "/u01/app/oracle/inventory/orainstRoot.sh" ]; then
# sleep 1
# elif [ ! -f "/u01/app/oracle/product/11.2.0/root.sh" ]; then
# sleep 1
# else
# break
# fi
#done
echo -e "安装完成\n"
sh /u01/app/oracle/inventory/orainstRoot.sh
sh /u01/app/oracle/product/11.2.0/root.sh
echo "---------------------11、配置监听---------------------"
sed -i "s/GDBNAME = \"orcl11g.us.oracle.com\"/GDBNAME = \"$oracle_SID\"/" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/SID = \"orcl11g\"/SID = \"$oracle_SID\"/" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/#SYSPASSWORD = \"password\"/SYSPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/#SYSTEMPASSWORD = \"password\"/SYSTEMPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/#SYSMANPASSWORD = \"password\"/SYSMANPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/#DBSNMPPASSWORD = \"password\"/DBSNMPPASSWORD = \"$oracle_pwd\"/" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/#DATAFILEDESTINATION =/DATAFILEDESTINATION =\/u01\/app\/oracle\/oradata" $oracle_basehome'/database/response/dbca.rsp'
sed -i "s/#RECOVERYAREADESTINATION=/RECOVERYAREADESTINATION=\/u01\/app\/oracle\/fast_recovery_area" $oracle_basehome'/database/response/dbca.rsp'
sed -i 's/#CHARACTERSET = "US7ASCII"/CHARACTERSET = "ZHS16GBK"/' $oracle_basehome'/database/response/dbca.rsp'
sed -i 's/#TOTALMEMORY = "800"/TOTALMEMORY = "1638"/' $oracle_basehome'/database/response/dbca.rsp'
su - oracle -c "export DISPLAY=localhost:0.0"
su - oracle -c "netca -silent -responseFile $oracle_basehome/database/response/netca.rsp"
su - oracle -c "lsnrctl start"
su - oracle -c "netstat -tnlup | grep 1521"
su - oracle -c "dbca -silent -responseFile $oracle_basehome/database/response/dbca.rsp"
echo -e "oracle的进程信息是:\n"
su - oracle -c "ps -ef | grep ora_ | grep -v grep"
echo -e "\n'oracle的实例名是:'$oracle_SID\n"
echo -e "\n'oracle的管理密码是:'$oracle_pwd\n"
echo -e "\n'oracle的路径是:'$oracle_basehome\n"
echo "---12、请手动输入select * from v\$version;查询数据库版本,查询不到请startup启动数据库---"
su - oracle -c "sqlplus / as sysdba"
4、防止攻击异常ip自动拦截
#!/bin/bash
Date=$(date +%Y-%m-%d" "%H:%M)
Log_file="/var/log/deny_ip.log"
deny_num=0
deny_ip=$(netstat -tnlap | grep ESTABLISHED | grep -Ev "::" | awk '{print $5}' | awk -F : '{print $1}' | uniq -c | sort -rn | awk '{if ($1 > $deny_num)print $2}')
touch $Log_file
for ip in $deny_ip;
do
if !(firewall-cmd --list-rich | grep $ip );then
echo "$Date加入IP黑名单:$ip"
echo "$Date加入IP黑名单:$ip" >> $Log_file
firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=$ip reject"
else
echo "已加入过IP黑名单:$ip"
fi
done
firewall-cmd --reload
5、一键部署 LAMP 脚本实现
工作结果就是 每台服务器 都可以打开php 测试页面
#!/bin/bash
NGINX_V=1.15.6
PHP_V=5.6.36
MYSQL_V=5.7.26
TMP_DIR=/tmp
msyql_pwd="123"
INSTALL_DIR=/usr/local
echo
echo -e "\tMenu\n"
echo -e "1. Install Nginx"
echo -e "2. Install PHP"
echo -e "3. Install MySQL(需要手动刷新下/etc/profile)"
echo -e "4. Deploy LNMP"
echo -e "9. Quit"
function command_status_check() {
if [ $? -ne 0 ]; then
echo $1
exit
fi
}
function install_mysql() {
echo "开始安装mysql"
cd $TMP_DIR
#wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-${MYSQL_V}-linux-glibc2.12-x86_64.tar.gz
tar zxf mysql-${MYSQL_V}-linux-glibc2.12-x86_64.tar.gz
mv mysql-${MYSQL_V}-linux-glibc2.12-x86_64 $INSTALL_DIR/mysql-${MYSQL_V}
useradd -s /sbin/nologin mysql
mkdir $INSTALL_DIR/mysql-${MYSQL_V}/data
chown -R mysql:mysql $INSTALL_DIR/mysql-${MYSQL_V}
cd $INSTALL_DIR/mysql-${MYSQL_V}/bin
./mysqld --initialize-insecure --user=mysql \
--basedir=$INSTALL_DIR/mysql-${MYSQL_V} \
--datadir=$INSTALL_DIR/mysql-${MYSQL_V}/data
command_status_check "Mysql - 平台环境检查失败!"
yum -y remove mariadb*
echo "导入配置参数"
#导入配置参数
cat > /etc/my.cnf <<EOF
[mysqld]
port=3306
user=mysql
basedir=$INSTALL_DIR/mysql-${MYSQL_V}
datadir=$INSTALL_DIR/mysql-${MYSQL_V}/data
socket=/tmp/mysql.sock
server_id=6
[mysql]
socket=/tmp/mysql.sock
EOF
cat /etc/profile | awk 'END{print $0}' | grep "="
if [ $? -eq 1 ];then
echo 'export PATH=$PATH:'$INSTALL_DIR'/mysql-'${MYSQL_V}'/bin' >> /etc/profile
else
echo ':'$INSTALL_DIR'/mysql-'${MYSQL_V}'/bin' >> /etc/profile
fi
source /etc/profile
cat /etc/rc.local | awk 'END{print $0}' | grep "source"
if [ $? -eq 1 ];then
echo 'source /etc/profile' >> /etc/rc.local
fi
bash /etc/rc.local
yum -y install expect
command_status_check "Mysql - expect安装失败!"
/usr/bin/expect <<EOF
mysqladmin -uroot -p password $msyql_pwd
expect {
"Enter passw*" { send "\r" }
}
expect eof
EOF
:'#serv方式
ln -s $INSTALL_DIR/mysql-${MYSQL_V}/support-files/mysql.server /etc/init.d/mysql
ln -s $INSTALL_DIR/mysql-${MYSQL_V}/bin/mysql /usr/bin/mysql
service mysql restart
'
#systemd方式
cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=$INSTALL_DIR/mysql-${MYSQL_V}/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
EOF
systemctl start mysqld
command_status_check "Mysql - 重启失败!"
echo -e "Mysql - 安装完成 \n需要重启服务器,或者 source /etc/profile 来刷新变量\n初始的默认密码为空,你可以通过 mysqladmin -uroot -p password 新密码 来重置密码"
}
function install_nginx() {
cd $TMP_DIR
yum install -y gcc gcc-c++ make openssl-devel pcre-devel wget
wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz
tar zxf nginx-${NGINX_V}.tar.gz
cd nginx-${NGINX_V}
./configure --prefix=$INSTALL_DIR/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-stream
command_status_check "Nginx - 平台环境检查失败!"
make && make install
command_status_check "Nginx - 安装失败!"
rm -rf $INSTALL_DIR/nginx/html/*
echo "ok" > $INSTALL_DIR/nginx/html/status.html
cat > $INSTALL_DIR/nginx/html/status.php <<EOF
<?php
phpinfo();
?>
EOF
sed -i '40,50s/index index.html index.htm/index status.php/g' $INSTALL_DIR/nginx/conf/nginx.conf
$INSTALL_DIR/nginx/sbin/nginx
command_status_check "Nginx - 启动失败!"
cat /etc/profile | awk 'END{print $0}' | grep "="
if [ $? -eq 1 ];then
echo 'export PATH=$PATH:'$INSTALL_DIR'/nginx/sbin:$PATH:/etc/init.d/' >> /etc/profile
else
echo ':'$INSTALL_DIR'/nginx/sbin:$PATH:/etc/init.d/' >> /etc/profile
fi
source /etc/profile
echo "Nginx配置完毕"
}
function install_php() {
cd $TMP_DIR
yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel \
libmcrypt-devel libxslt-devel libtidy-devel
wget http://docs.php.net/distributions/php-${PHP_V}.tar.gz
tar zxf php-${PHP_V}.tar.gz
cd php-${PHP_V}
./configure --prefix=$INSTALL_DIR/php \
--exec-prefix=$INSTALL_DIR/php \
--with-mysqli --with-pdo-mysql \
--with-gd --bindir=$INSTALL_DIR/php/bin \
--sbindir=$INSTALL_DIR/php/sbin \
--includedir=$INSTALL_DIR/php/include \
--libdir=$INSTALL_DIR/php/lib/php \
--mandir=$INSTALL_DIR/php/php/man \
--with-config-file-path=$INSTALL_DIR/php/etc \
--with-openssl --enable-mbstring --enable-fpm
command_status_check "PHP - 平台环境检查失败!"
make && make install
command_status_check "PHP - 安装失败!"
cp php.ini-production $INSTALL_DIR/php/etc/php.ini
cp $INSTALL_DIR/php/etc/php-fpm.conf.default $INSTALL_DIR/php/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
command_status_check "PHP - 启动失败!"
cat /etc/profile | awk 'END{print $0}' | grep "="
if [ $? -eq 1 ];then
echo 'export PATH=$PATH:'$INSTALL_DIR'/php/bin:$PATH' >> /etc/profile
else
echo ':'$INSTALL_DIR'/php/bin:$PATH' >> /etc/profile
fi
echo "source /etc/profile" >> ~/.bashrc
source /etc/profile
command_status_check "PHP - 环境变量刷新失败!"
echo "PHP配置完毕"
}
read -p "请输入编号:" number
case $number in
1)
install_nginx;;
2)
install_php;;
3)
install_mysql;;
4)
install_php
install_nginx
;;
9)
exit;;
esac
6、批量解压 使用for循环实现
for i in `ls`;do tar -zxvf $i;done
7、监控2台服务器硬盘利用率脚本实战
IP="127.0.0.1"
TMP_FILE=/tmp/disk.tmp
df -h > $TMP_FILE
USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
for USE_RATE in $USE_RATE_LIST; do
PART_NAME=${USE_RATE%=*}
USE_RATE=${USE_RATE#*=}
echo "$IP $PART_NAME Partition usage $USE_RATE%!"
done
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
TMP_FILE=/tmp/disk.tmp
ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
for USE_RATE in $USE_RATE_LIST; do
PART_NAME=${USE_RATE%=*}
USE_RATE=${USE_RATE#*=}
echo "$IP $PART_NAME Partition usage $USE_RATE%!"
#if [ $USE_RATE -ge 80 ]; then
# echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
#fi
done
done
8、批量检查 5个网站域名是否正常
#!/bin/bash
url_list="www.baidu.com jd.com cao.com qq.com taobao.com"
for url in $url_list;do
num=0
for ((i=1;i<=3;i++));do
http_code=$(curl -I -o /dev/null --connect-timeout 3 -s -w %{http_code} $url)
if [ $http_code -eq 200 ];then
echo "$url is ok"
break
else
echo "$url full is $num"
let num++
fi
done
if [ $num -eq 3 ];then
echo "$url is loser"
fi
done
9、统计磁盘、内存使用率,使用率大于%5 就打印mail 小于则正常
DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`
function check_disk(){
for I in $DEV;do
dev=`df -Ph | grep $I | awk '{print $1}'`
size=`df -Ph | grep $I | awk '{print $2}'`
used=`df -Ph | grep $I | awk '{print $3}'`
free=`df -Ph | grep $I | awk '{print $4}'`
rate=`df -Ph | grep $I | awk '{print $5}'`
mount=`df -Ph | grep $I | awk '{print $6}'`
echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate\tmount:$mount"
F=`echo $rate | awk -F% '{print $1}'`
if [ $F -ge 5 ];then
echo "$mount mail"
else
echo "It's OK"
fi
done
}
function check_ram(){
DATE=$(date +%F" "%H:%M)
MAIL="example@mail.com"
#TOTAL 内存总的大小
TOTAL=$(free -m |awk '/Mem/{print $2}')
#USE 使用的物理内存大小(used - buff-cache)
USE=$(free -m |awk '/Mem/{print $3-$6}')
#FREE 内存剩余量
FREE=$(($TOTAL-$USE))
if [ $USE -lt 0 ];then
echo " Date: $DATE
Total=$TOTAL,
Use=$USE,
Free=$FREE "
elif [ $FREE -lt 1024 ]; then
echo " Date: $DATE
Total=$TOTAL,
Use=$USE,
Free=$FREE "
fi
}
echo "--------------------check_disk--------------------"
check_disk;
echo "--------------------check_ram--------------------"
check_ram;
echo "--------------------End--------------------"
10、批量检查ip 是否在线是否能ping 通 使用for循环实现
#!/bin/bash
ip_list="192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5"
for i in $ip_list;do
for ((n=1;n<=3;n++));do
ping -c 1 $i &> /dev/null
if [ $? -eq 0 ];then
echo "$i is success"
break
else
echo "$i louser $n"
fi
done
done
11、批量管理redis脚本
#!/bin/bash
USAG(){
echo "sh $0 {start|stop|restart|login|ps|tail} PORT"
}
if [ "$#" = 1 ]
then
REDIS_PORT='6379'
elif
[ "$#" = 2 -a -z "$(echo "$2"|sed 's#[0-9]##g')" ]
then
REDIS_PORT="$2"
else
USAG
exit 0
fi
REDIS_IP=$(hostname -I|awk '{print $1}')
PATH_DIR=/opt/redis_cluster/redis_${REDIS_PORT}/
PATH_CONF=/opt/redis_cluster/redis_${REDIS_PORT}/conf/redis_${REDIS_PORT}.conf
PATH_LOG=/opt/redis_cluster/redis_${REDIS_PORT}/logs/redis_${REDIS_PORT}.log
CMD_START(){
redis-server ${PATH_CONF}
}
CMD_SHUTDOWN(){
redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT} shutdown
}
CMD_LOGIN(){
redis-cli -c -h ${REDIS_IP} -p ${REDIS_PORT}
}
CMD_PS(){
ps -ef|grep redis
}
CMD_TAIL(){
tail -f ${PATH_LOG}
}
case $1 in
start)
CMD_START
CMD_PS
;;
stop)
CMD_SHUTDOWN
CMD_PS
;;
restart)
CMD_START
CMD_SHUTDOWN
CMD_PS
;;
login)
CMD_LOGIN
;;
ps)
CMD_PS
;;
tail)
CMD_TAIL
;;
*)
USAG
esac
12、快速回滚nginx业务
#!/bin/bash
cd /usr/local/nginx
ls | grep "^web"
read -p "请输入你要回滚的目录版本:" ver
rm -rf html
ln -s $ver html