首页 > 代码库 > Linux Shell 运维脚本功底积累

Linux Shell 运维脚本功底积累

1、删除Linux远程用户连接会话

[root@tradx24 logs]# w 10:45:28 up 15 days, 16:23,  4 users,  load average: 0.00, 0.00, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHATroot     tty1     -                Sun21    4days  0.00s  0.00s -bashroot     pts/0    192.168.1.2    09:11    0.00s  0.07s  0.00s wroot     pts/2    192.168.1.2    09:45   30:53   0.07s  0.07s -bashroot     pts/3    192.168.1.8    10:20   22:05   0.02s  0.00s vi operation.log[root@tradx24 logs]# skill -KILL -v pts/3pts/3    root     21217 vi              pts/3    root     24331 bash            

2、通过ssh远程执行命令

[root@tradx24 bin]# iip="192.168.1.10"[root@tradx24 bin]# ssh $iip w 10:54:13 up 125 days, 20:17,  1 user,  load average: 0.00, 0.00, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHATroot     tty1     -                21Mar14  4days  0.09s  0.09s -bash

3、通过ssh远程批量执行MySQL脚本

#iplist1=(`cat ../tools/dblist | grep -v \# | awk {print $1} | sed "1,1d"`)#userlist2=(`cat ../tools/dblist | grep -v \# | awk {print $2} | sed "1,1d"`)#passwordlist3=(`cat ../tools/dblist | grep -v \# | awk {print $3} | sed "1,1d"`)for((i=0;i<=${#list1[@]};i++));do        iip=`echo ${list1[i]} | awk -F "." {print $4}`        ssh ${list1[i]}  mkdir /home/sqlScrip/        ssh ${list1[i]}  mkdir /home/sqlScrip/$iip        clear        scp ../sqlScrip/$iip/* ${list1[i]}:/home/sqlScrip/$iip        clear        #path        list4=(`ls ../sqlScrip/$iip`)        for((w=0;w<=${#list4[@]};w++));        do                if [ "117" == "$iip" ]                then                        ssh ${list1[i]}  "mysql -h ${list1[i]} -u ${list2[i]} -p${list3[i]} < /home/sqlScrip/${iip}/${list4[w]};"                        clear                fi        done        ssh ${list1[i]} ‘rm -rf /home/sqlScrip‘done

4、通过svn下载war包

#list第五行为下载地址list=(`cat ../tools/list  | awk {print $5} | sed "1,1d"`)for((i=0;i<${#list[@]};i++));do        curl -u wangyong:Abcd1234 -O ${list[i]}        war=`echo ${list[i]} | awk -F "/" {print $NF}`        mv $war ../workapp/done

5、解压war包到指定目录

ssh ${ip} unzip ${tomcat目录}/webapps/${war包名称}.war -d  ${tomcat目录}/webapps/${指定目录}

6、多Linux服务器建立信任关系

ssh-keygen -t rsacd ~/.sshscp -r id_rsa.pub $ip:/root/.ssh/$ipssh $ip touch ~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keysssh $ip "cat ~/.ssh/$ip >> ~/.ssh/authorized_keys"

7、删除某目录下空文件

find ../new/$serve/properties -type f -size 0 -exec rm -f {} \;

8、删除某目录下小于10b的文件

#Delete the files smaller than 10B        for i in `ls -l ../new/$serve/properties/ | sed -e 1d | awk $5 < 10 {print$NF}`        do                rm -rf ../new/$serve/properties/$i        done

9、Linux通过alias设置快捷命令

command(){list1=(`cat ~/.bashrc | grep custom`)for((i=0;i<=${#list1[@]};i++));do        if [[ "${list1[i]}" = "#custom" ]]        then                echo "[warn] The command has already been initialized,there‘s no need to repeat the operation, thank you!"                break        else                path=`pwd`                #初始命令                #Initialization commands.                echo "#custom" >> ~/.bashrc                echo "alias l=‘ll -al‘" >> ~/.bashrc                echo "alias conf=‘cd `echo $path`/../conf‘" >> ~/.bashrc                echo "alias bin=‘cd `echo $path`/../bin‘" >> ~/.bashrc                echo "alias new=‘cd `echo $path`/../new‘" >> ~/.bashrc                echo "alias too=‘cd `echo $path`/../tools‘" >> ~/.bashrc                echo "alias hostlist=‘cat `echo $path`/../tools/.hostlist‘" >> ~/.bashrc                echo "alias un=‘cd `echo $path`/../‘" >> ~/.bashrc                echo "alias bak=‘cd `echo $path`/../bak‘" >> ~/.bashrc                echo "alias app=‘cd `echo $path`/../workapp‘" >> ~/.bashrc                echo "alias ..=‘cd ../‘" >> ~/.bashrc                source ~/.bashrc        fidone}

10、引用其他shell脚本方法

## ====================================================#### auth:wonter                                         #### date:2014-06-12                                     #### path:javame.cnblogs.com                             #### ====================================================###!/bin/sh#引用tool.sh脚本中方法. ../tools/tool.sh#Parameterspar#Capture ip#Capture pathconfig

11、获取脚本自身名称

#Automatically obtain configuration filesname=`echo $0`uname=`echo $name | awk -F "/" {print $2} | awk -F "." {print $1}`#Uniform configurationserve="$uname"

12、检查是否存在文件夹,没有则新建文件夹

bak_mk="../bak/$serve"new_mk="../new/$serve"if [ ! -d $bak_mk ]; thenmkdir $bak_mkfiif [ ! -d $new_mk ]; thenmkdir $new_mkfi

13、统计脚本执行时间

     st=`date +"%Y-%m-%d %H:%M:%S"`
   #执行方法
start en=`date +"%Y-%m-%d %H:%M:%S"` st_c=`date -d "$st" +%s` en_c=`date -d "$en" +%s` interval=`expr $en_c - $st_c` echo "Start start-up time :${st}" echo "Start end time :${en}" echo "total consuming time :${interval} 秒"

14、shell定义集合

#namelist1=(`cat ../tools/list | grep -v \# | awk {print $1} | sed "1,1d"`)#iplist2=(`cat ../tools/list | grep -v \# | awk {print $2} | sed "1,1d"`)#servelist3=(`cat ../tools/list | grep -v \# | awk {print $3} | sed "1,1d"`)for((i=0;i<=${#list1[@]};i++));do        cp ../conf/.model ../conf/${list1[i]}.conf        cp ../bin/.model.wy ../bin/${list1[i]}.wy        cp ../bin/.model.yw ../bin/${list1[i]}.ywdone

15、记录脚本执行者用户与IP

#Perform operationecho "operator:"read mea=`tty | awk -F "/dev/" {print $2}`date "+%Y-%m-%d %H:%M:%S" >> ../logs/operation.logecho `w | grep $a | awk {print $3}` "${me} > Perform operation: one" >> ../logs/operation.log

16、shell中的正则语句

for i in `cat ../conf/$confs  | grep start | awk -F ">>" {print $2} | awk -F "_" {print $1}`do        if [[ "${i}" == "shiro" ]]        then                sed -n -e />>shiro/,/>>shiro/p  $confs |grep -v >>shiro > $pro_shi                cat $pro_shi | col -b > ../temp/m1                cat ../temp/m1 > $pro_shi        elif [[ "${i}" == "jdbc" ]]        then                sed -n -e />>jdbc/,/>>jdbc/p  $confs |grep -v >>jdbc > $pro_jdbc                cat $pro_jdbc | col -b > ../temp/m1                cat ../temp/m1 > $pro_jdbc        fi
done

17、shell写个定时进程(不推荐我这个写法,应该有更好!)

## ====================================================#### auth:wonter                                         #### date:2014-06-12                                     #### to  :timing.sh                                      #### ====================================================###!/bin/shecho "请输入定时阀值(格式:`date +"%Y-%m-%d %H:%M"` )"        read timingecho "定时任务设置完毕,请安心等待..."ti(){while truedo        time=`date +"%Y-%m-%d %H:%M"`        if [[ "${time}" == "${timing}" ]]        then                echo "定时任务:"                #定时清理日志                rm -rf ../logs/*                #定时备份日志                #定时执行脚本                break        fidone}ti&

18、通过参数方式执行脚本 tools.sh

#!/bin/shtrust(){#建立信任echo "请输入信任机ip:(提示:需输入三次密码!)"read iptrusts}command(){#初始化命令command}case "$*" in        trust)        trust        ;;        command)        command        ;;        *)        echo "----------------------------------------"        echo "pls : http://javame.cnblogs.com  welcome"        echo "----------------------------------------"        echo "  主机信任     | or | 初始化命令(初始)  "        echo "tools.sh trust | or | tools.sh command"        echo "----------------------------------------"        ;;esac

19、终端发送回会话给其他用户 限root用户

echo "输入您想说的话吧:"
read spkecho $spk
>/dev/stdin >/dev/pts/2

20、某目录下所有文件统一批量修改ip

sed -i  "s/10.0.0.1/10.0.1.2/g" `grep "10.0.0.1" -rl /opt/uer/wy`

21、链接数与进程数

#链接数netstat -n | awk /^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}#统计httpd协议连接数进程数ps -ef|grep httpd|wc -lps aux|grep httpd|wc -l#句柄数lsof -n|awk {print $2}|sort|uniq -c|sort -nr|more #查看进程占用内存ps aux |awk ($1 ~apache) && ($6>50) {print $0}

22、Linux查看线程的三种方法

1、top -H手册中说:-H : Threads toggle加上这个选项启动top,top一行显示一个线程。否则,它一行显示一个进程。2、ps xH手册中说:H Show threads as if they were processes这样可以查看所有存在的线程。3、ps -mp <PID>手册中说:m Show threads after processes这样可以查看一个进程起的线程数。

23、查看连接某服务端口最多的的IP地址

netstat -nat | grep "192.168.1.20:8443" |awk {print $5}|awk -F: {print $4}|sort|uniq -c|sort -nr|head -20

24、常见压缩解压命名

.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!).gz解压1:gunzip FileName.gz解压2:gzip -d FileName.gz压缩:gzip FileName.tar.gz 和 .tgz解压:tar zxvf FileName.tar.gz压缩:tar zcvf FileName.tar.gz DirName.bz2解压1:bzip2 -d FileName.bz2解压2:bunzip2 FileName.bz2压缩: bzip2 -z FileName.tar.bz2解压:tar jxvf FileName.tar.bz2压缩:tar jcvf FileName.tar.bz2 DirName.bz解压1:bzip2 -d FileName.bz解压2:bunzip2 FileName.bz压缩:未知.tar.bz解压:tar jxvf FileName.tar.bz压缩:未知.Z解压:uncompress FileName.Z压缩:compress FileName.tar.Z解压:tar Zxvf FileName.tar.Z压缩:tar Zcvf FileName.tar.Z DirName.zip解压:unzip FileName.zip压缩:zip FileName.zip DirName.rar解压:rar x FileName.rar压缩:rar a FileName.rar DirName 

25、厚积薄发,持续更新积累,请关注javame.cnblogs.com

 

注:转载请添加出处