找文件差异

grep -f 选项可以匹配到文件a在文件b中所有相关的行(取a中有b中有的)

  1. [root@centos-04 tmp]# vim b.txt
  2. vvvv
  3. root
  4. [root@centos-04 tmp]# grep -f b.txt /etc/passwd
  5. root:x:0:0:root:/root:/bin/bash
  6. operator:x:11:0:operator:/root:/sbin/nologin
  7. dockerroot:x:994:991:Docker User:/var/lib/docker:/sbin/nologin
  8. [root@centos-04 tmp]#

取passwd中有b文件中没有的

  1. [root@centos-04 tmp]# grep -vf b.txt /etc/passwd
  1. [root@centos-04 tmp]# vim 61.sh
  2. #!/bin/bash
  3. #这个脚本用来比较文件差异
  4. #作者:SYJ
  5. #日期:2019-03-13
  6.  
  7. cat a.txt|while read line
  8. do
  9. if ! grep -q "$line" b.txt
  10. then
  11. echo $line
  12. fi
  13. done > c.txt
  14. wc -l c.txt
  15. [root@centos-04 tmp]# sh 61.sh
  16. 3 c.txt
  17. [root@centos-04 tmp]# cat c.txt
  18. aaa
  19. 1111
  20. 2222
  21. [root@centos-04 tmp]#

杀进程

  1. [root@centos-04 tmp]# ps -u $USER|awk '$NF ~ /kworker/ {print $1}' |xargs kill
  1. [root@centos-04 tmp]# vim 62.sh
  2. #!/bin/bash
  3. #这个脚本用来杀进程
  4. #作者:SYJ
  5. #日期:2019-03-13
  6. ps -u $USER|awk '$NF ~ /kworker/ {print $1}' |xargs kill

并发备份数据库

split切割文件,默认文件名是aa、ab、ac这种,可以自定义名,我们下面例子用test为文件名前缀

  1. [root@centos-04 tmp]# cat 1.txt
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. [root@centos-04 tmp]# split -l 2 1.txt
  9. [root@centos-04 tmp]# wc -l xa*
  10. 2 xaa
  11. 2 xab
  12. 2 xac
  13. 6 总用量
  14. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# cat xaa
  2. 1
  3. 2
  4. [root@centos-04 tmp]# cat xab
  5. 3
  6. 4
  7. [root@centos-04 tmp]# cat xac
  8. 5
  9. 6
  10. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# split -l 2 1.txt test
  2. [root@centos-04 tmp]# wc -l testa*
  3. 2 testaa
  4. 2 testab
  5. 2 testac
  6. 6 总用量
  7. [root@centos-04 tmp]#  

scale运算结果保留几位小数

  1. [root@centos-04 tmp]# echo "scale=3;1000/265"|bc -l
  2. 3.773

wait命令会等后台运行的进程执行完再执行后面的命令

  1. [root@centos-04 tmp]# sleep 10 &
  2. [1] 38044
  3. [root@centos-04 tmp]# wait
  4. [1]+ 完成 sleep 10
  5. [root@centos-04 tmp]#   

jobs命令查看后台进程

  1. [root@centos-04 tmp]# jobs
  2. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# sleep 10 &
  2. [1] 38136
  3. [root@centos-04 tmp]# jobs
  4. [1]+ 运行中 sleep 10 &
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 63.sh
  2. #!/bin/bash
  3. #这个脚本用来并发备份数据库
  4. #作者:SYJ
  5. #日期:2019-03-14
  6.  
  7. N=2
  8. mysql -uroot -p -h10.21.95.237 big -e "show tables"|sed '1d' > /tmp/table.txt
  9.  
  10. n=`wc -l /tmp/table.txt|awk '{print $1}'`
  11.  
  12. div()
  13. {
  14. n=`echo "scale=1;$1/$2"|bc`
  15. n1=`echo "scale=1;$n+0.5"|bc`
  16. echo $n1|cut -d. -f1
  17. }
  18.  
  19. n1=`div $n $N`
  20.  
  21. split -l $n1 /tmp/table.txt
  22.  
  23. myd(){
  24. for t in `cat $1`
  25. do
  26. mysqldump -uroot -p -h10.21.95.237 big $t > $t.sql
  27. done
  28. }
  29.  
  30. for f in xaa xab
  31. do
  32. myd $f &
  33. done
  34. wait
  35.  
  36. tar zcf mydb.tar.gz *.sql
  37. rm -f $.sql
  1. [root@centos-04 tmp]# sh 63.sh

监控CDN节点  

cdn其实就是一个代理服务器,只不过这个代理服务器有缓存功能。用户访问的时候加入访问的是图片,用户直接可以在最近的cdn服务器上访问图片。

  1. [root@centos-04 tmp]# vim 64.sh
  2. #这个脚本用来监控CDN节点
  3. #作者:SYJ
  4. #!/bin/bash
  5. #这个脚本用来监控CDN节点
  6. #作者:SYJ
  7. #日期:2019-04-08
  8. s_ip=88.88.88.88
  9. url=www.syj.com/index.php
  10. ipf=/data/ip.list
  11. curl -x$s_ip:80 $url 2>/dev/null >/tmp/source.txt
  12. for ip in `cat $ipf`
  13. do
  14. curl -x$ip:80 $url 2>/dev/null >/tmp/$ip.txt
  15. diff /tmp/source.txt /tmp/$ip.txt > /tmp/$ip.diff
  16. n=`wc -l /tmp/$ip.diff|awk '{print $1}'`
  17. if [ $n -gt 0 ]
  18. then
  19. echo "节点$ip有异常。"
  20. fi
  21. done

破解字符串

  1. [root@centos-04 tmp]# vim 65.sh
  2. #!/bin/bash
  3. #这个脚本用来破解字符串
  4. #作者:SYJ
  5. #日期:2019-04-08
  6. for i in `seq 0 32767`
  7. do
  8. m=`echo $i|md5sum |cut -c 1-8 `
  9. echo $i $m
  10. done > /tmp/md5.txt
  11.  
  12. cat > c.txt <<EOF
  13. 21029299
  14. 00205d1c
  15. EOF
  16.  
  17. grep -f c.txt /tmp/md5.txt
  1. [root@centos-04 tmp]# sh 65.sh
  2. 1346 00205d1c
  3. 25667 21029299

判断cpu厂商  

  1. [root@centos-04 tmp]# cat /proc/cpuinfo
  2. processor : 0
  3. vendor_id : GenuineIntel
  4. cpu family : 6
  5. model : 58
  6. model name : Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz
  7. stepping : 9
  8. microcode : 0x16
  9. cpu MHz : 3292.569
  10. cache size : 3072 KB
  11. physical id : 0
  12. siblings : 1
  13. core id : 0
  14. cpu cores : 1
  15. apicid : 0
  16. initial apicid : 0
  17. fpu : yes
  18. fpu_exception : yes
  19. cpuid level : 13
  20. wp : yes
  21. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl
    xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx f16c hypervisor lahf_lm epb fsgsbase
    tsc_adjust smep dtherm arat pln pts
  22. bogomips : 6585.13
  23. clflush size : 64
  24. cache_alignment : 64
  25. address sizes : 42 bits physical, 48 bits virtual
  26. power management:
  27.  
  28. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# grep '^vendor_id' /proc/cpuinfo |head -1
  2. vendor_id : GenuineIntel
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# grep '^vendor_id' /proc/cpuinfo |head -1 |awk -F ': ' '{print $2}'
  2. GenuineIntel
  3. [root@centos-04 tmp]#   

同步时间

  1. [root@centos-04 tmp]# ntpdate time.windows.com
  2. 8 Apr 15:59:35 ntpdate[8808]: step time server 13.65.245.138 offset -28350.105140 sec
  3. [root@centos-04 tmp]# date
  4. 2019 04 08 星期一 15:59:41 CST
  5. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 66.sh
  2. #company[GenuineIntel]='CPU厂商是Intel'
  3. #for name in GenuineIntel AMD
  4. #do
  5. # if [ $name == $cpu ]
  6. # then
  7. # echo ${company[$name]}
  8. # else
  9. # echo "CPU厂商是非主流厂商"
  10. # fi
  11. #done
  12.  
  13. #if [ $cpu == "AMD" ]
  14. #then
  15. # echo "CPU厂商是AMD"
  16. #elif [ $cpu == "GenuineIntel" ]
  17. #then
  18. # echo "CPU厂商是Intel"
  19. #else
  20. # echo "CPU厂商是非主流厂商"
  21. #fi
  22.  
  23. case $cpu in
  24. AMD)
  25. echo "CPU厂商是AMD"
  26. ;;
  27. GenuineIntel)
  28. echo "CPU厂商是Intel"
  29. ;;
  30. *)
  31. echo "CPU厂商是非主流厂商"
  32. ;;
  33. esac
  1. [root@centos-04 tmp]# sh 66.sh
  2. CPU厂商是Intel
  3. [root@centos-04 tmp]#

监控CPU使用率  

用空白字符和百分号分割字符

  1. [root@centos-04 tmp]# top -bn1 |grep 'Cpu(s):'|sed 's/^%//'|awk -F ' +|%' '{print $8}'
  2. 93.8
  1. [root@centos-04 tmp]# vim 67.sh
  2. #!/bin/bash
  3. #这个脚本用来计算CPU使用率
  4. #作者:SYJ
  5. #日期:2019-04-08
  6.  
  7. while :
  8. do
  9.  
  10. cpu_i=`top -bn1 |grep 'Cpu(s):'|sed 's/^%//'|awk -F ' +|%' '{print $8}'`
  11. cpu_u=`echo 100-$cpu_i|bc`
  12.  
  13. if [ $cpu_u gt 90 ]
  14. then
  15. python mail.py xxx@xxx.com "CPU使用率偏高" "`top -bn1`"
  16. fi
  17.  
  18. sleep 10
  19. done

打印子进程  

查看父进程ID是7046的子进程

  1. [root@centos-04 tmp]# ps -elf |awk '$5==7046 {print $4}'
  2. 7052
  3. 7573
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 68.sh
  2. #!/bin/bash
  3. #这个脚本用来打印子进程
  4. #作者:SYJ
  5. #日期:2019-04-08
  6.  
  7. ps -elf > /tmp/pid.txt
  8. read -p "Please input a pid: " p
  9.  
  10. if [ -z "$p" ]
  11. then
  12. echo "Please input a pid"
  13. exit
  14. fi
  15.  
  16. if ! grep -qw "$p" /tmp/pid.txt
  17. then
  18. echo "你输入的pid不存在"
  19. exit
  20. fi
  21.  
  22. get_cpid()
  23. {
  24. p1=$1
  25. ps -elf |awk -v p2=$p1 '$5==p2 {print $4}' > /tmp/$p1.txt
  26. n=`wc -l /tmp/$p1.txt|awk '{print $1}'`
  27. if [ $n -eq 0 ]
  28. then
  29. echo "进程$1下没有子进程"
  30. else
  31. echo "进程$1下的子进程是:"
  32. cat /tmp/$p1.txt
  33. fi
  34. }
  35.  
  36. get_cpid $p
  37.  
  38. for c_p in `cat /tmp/$p.txt`
  39. do
  40. get_cpid $c_p
  41. done
  1. [root@centos-04 tmp]# sh 68.sh
  2. Please input a pid: 1
  3. 进程1下的子进程是:
  4. 3161
  5. 3184
  6. 3191
  7. 6542
  8. 6564
  9. 6565
  10. 6567
  11. 6568
  12. 6569
  13. 6598
  14. 6606
  15. 6610
  16. 6835
  17. 6836
  18. 6838
  19. 6969
  20. 7046
  21. 7772
  22. 进程3161下没有子进程
  23. 进程3184下没有子进程
  24. 进程3191下没有子进程
  25. 进程6542下没有子进程
  26. 进程6564下没有子进程
  27. 进程6565下没有子进程
  28. 进程6567下没有子进程
  29. 进程6568下没有子进程
  30. 进程6569下的子进程是:
  31. 6652
  32. 进程6598下没有子进程
  33. 进程6606下没有子进程
  34. 进程6610下没有子进程
  35. 进程6835下没有子进程
  36. 进程6836下的子进程是:
  37. 7189
  38. 进程6838下没有子进程
  39. 进程6969下的子进程是:
  40. 6970
  41. 6971
  42. 进程7046下的子进程是:
  43. 7052
  44. 7573
  45. 进程7772下没有子进程
  46. [root@centos-04 tmp]#

给lamp环境增加项目  

  1. [root@centos-04 tmp]# vim 69.sh
  2. #!/bin/bash
  3. #本脚本的功能是在LAMP环境中增加站点,包括apache配置、FTP增加用户、Mysql增加库和用户
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. #网站目录
  8. webdir=/data/wwwroot
  9.  
  10. #ftp的虚拟用户配置文件目录
  11. ftpudir=/etc/vsftpd/vuuser
  12.  
  13. #ftp虚拟用户密码文件
  14. ftpuserfile=/root/login
  15.  
  16. #mysql命令行登录root
  17. mysqlc="/usr/local/mysql/bin/mysql -uroot -proot"
  18.  
  19. #apache虚拟主机配置文件
  20. httpd_config_f="/usr/local/apache2/conf/extra/httpd-vhosts.conf"
  21.  
  22. #定义增加Mysql库和用户的函数
  23. add_mysql_user()
  24. {
  25. #生成随机密码
  26. mysql_p=`mkpasswd -s 0 -l 12`
  27.  
  28. #将密码保存到临时文件里,这里的$pro为用户自定义的项目名字
  29. echo "$pro $mysql_p" >/tmp/$pro.txt
  30.  
  31. #这里使用嵌入文档的形式(需顶格),将创建用户并授权的命令传递给mysql
  32. $mysqlc <<EOF
  33. create database $pro;
  34. grant all on $pro.* to "$pro"@'127.0.0.1' identified by "$mysql_p";
  35. #下面这个EOF必须要顶格
  36. EOF
  37. }
  38.  
  39. #定义增加FTP用户的函数
  40. add_ftp_user()
  41. {
  42. ftp_P=`mkpasswd -s 0 -l 12`
  43. echo "$pro" >> $ftpuserfile
  44. echo "$ftp_p" >> $ftpuserfile
  45.  
  46. #将用户、密码文件转换为密码db文件
  47. db_load -T -t hash -f $ftpuserfile /etc/vsftpd/vsftpd_login.db
  48. cd $ftpudir
  49.  
  50. #这里的aaa是一个文件,是之前的一个项目,可以作为配置模板
  51. cp aaa $pro
  52.  
  53. #把里面的aaa改为新的项目名字
  54. sed -i "s/aaa/$pro/" $pro
  55.  
  56. #重启vsftpd服务
  57. /etc/init.d/vsftpd restart
  58.  
  59. }
  60.  
  61. #定义增加apache虚拟主机的函数
  62. config_httpd()
  63. {
  64. #增加网站根目录,和域名保持一致,这里的$dom为用户自定义的域名
  65. mkdir $webdir/$dom
  66.  
  67. #将网站根目录属主和属组设置为ftp用户
  68. chown vsftpd:vsftpd $webdir/$dom
  69.  
  70. #用嵌入文档(需顶格),把虚拟主机配置写入到配置文件里
  71. cat >> $httpd_config_f <<EOF
  72. <VirtualHost *.80>
  73. DocumentRoot $webdir/$dom
  74. ServerName $dom
  75. <Directory $webdir/$dom>
  76. AllowOverride none
  77. Require all granted
  78. </Directory>
  79. EOF
  80.  
  81. #重载apache服务
  82. /usr/local/apache2/bin/apachectl graceful
  83. }
  84.  
  85. read -p "input the project name: " pro
  86. read -p "input the domain: " dom
  87.  
  88. add_mysql_user
  89. add_ftp_user
  90. config_httpd

简易计算器   

  1. [root@centos-04 tmp]# vim 70.sh
  2. #!/bin/bash
  3. #这个脚本用来实现简易计算器
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. if [ $# -ne 3 ]
  8. then
  9. echo "你输出的参数个数不对,应该给3个参数"
  10. exit
  11. fi
  12.  
  13. if_number()
  14. {
  15. n1=`echo $1|sed 's/[0-9.]//g'`
  16. if [ -n "$n1" ]
  17. then
  18. echo "$1不是数字"
  19. exit
  20. fi
  21.  
  22. if echo $1|grep -q '^\.'
  23. then
  24. echo "数字$1不合法"
  25. exit
  26. fi
  27. }
  28.  
  29. if_number $1
  30. if_number $3
  31.  
  32. case $2 in
  33. +)
  34. echo "$1+$3"|bc
  35. ;;
  36. -)
  37. echo "$1-$3"|bc
  38. ;;
  39. \*)
  40. echo "$1*$3"|bc
  41. ;;
  42. /)
  43. echo "scale=2;$1/$3"|bc
  44. ;;
  45. *)
  46. echo "你给出的格式不对,第二个参数只能是+,-,*,/"
  47. ;;
  48. esac
  49.  
  50. [root@centos-04 tmp]# sh 70.sh 1 2 3
  51. 你给出的格式不对,第二个参数只能是+,-,*,/
  52. [root@centos-04 tmp]# sh 70.sh 1 + 3
  53. 4
  54. [root@centos-04 tmp]# sh 70.sh 1 - 3
  55. -2
  56. [root@centos-04 tmp]# sh 70.sh 1 '*' 3
  57. 3
  58. [root@centos-04 tmp]# sh 70.sh 1 \* 3
  59. 3
  60. [root@centos-04 tmp]# sh 70.sh 1 / 3
  61. .33
  62. [root@centos-04 tmp]#

判断文件并计算单词

  1. [root@centos-04 tmp]# vim 71.sh
  2. #!/bin/bash
  3. #这个脚本用来判断文件是否存在并计算单词个数
  4. #作者:SYJ
  5. #日期2019-04-09
  6.  
  7. if [ $# -ne 2 ]
  8. then
  9. echo "请提供两个参数,第一个参数是目录名字,第二个参数是单词"
  10. exit
  11. fi
  12.  
  13. cd $1
  14. for f in `ls .`
  15. do
  16. if [ -d $f ]
  17. then
  18. if [ -f $f/test.txt ]
  19. then
  20.  
  21. n=`grep -cw "$2" $f/test.txt`
  22. echo "$1/$f目录下面有test.txt, 该test.txt里面有$n个$2"
  23. fi
  24. fi
  25. done

打印正方形

  1. [root@centos-04 tmp]# for i in `seq 1 3`; do for j in `seq 1 3`; do echo -n "口 "; done; echo ; done



  2. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 72.sh
  2. #!/bin/bash
  3. #这个脚本用来打印正方形
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. while :
  8. do
  9.  
  10. read -p "Please input a number: " n
  11. n1=`echo $n|sed 's/[0-9]//g'`
  12. if [ -n "$n1" ]
  13. then
  14. echo "$n is not a number"
  15. continue
  16. else
  17. break
  18. fi
  19. done
  20.  
  21. for i in `seq 1 $n`
  22. do
  23. for j in `seq 1 $n`
  24. do
  25. echo -n "口 "
  26. done
  27. echo
  28. done
  29.  
  30. [root@centos-04 tmp]# sh 72.sh
  31. Please input a number: 4




  32. [root@centos-04 tmp]# sh 72.sh
  33. Please input a number: 5





  34. [root@centos-04 tmp]#

问候用户

  1. [root@centos-04 tmp]# awk -F ':' '{print "Hello, "$1", your uid is "$3"."}' /etc/passwd
  2. Hello, root, your uid is 0.
  3. Hello, bin, your uid is 1.
  4. Hello, daemon, your uid is 2.
  5. Hello, adm, your uid is 3.
  6. Hello, lp, your uid is 4.
  7. Hello, sync, your uid is 5.
  1. [root@centos-04 tmp]# vim 73.sh
  2. #!/bin/bash
  3. #这个脚本用来问候用户
  4. #作者:SYJ
  5. #日期:2019-04-09
  6. cat /etc/passwd |while read line
  7. do
  8. username=`echo $line|awk -F ':' '{print $1}'`
  9. uid=`echo $line|awk -F ':' '{print $3}'`
  10. echo "Hello,$username,your uid is $uid"
  11. done
  1. [root@centos-04 tmp]# sh 73.sh
  2. Hello,root,your uid is 0
  3. Hello,bin,your uid is 1
  4. Hello,daemon,your uid is 2
  5. Hello,adm,your uid is 3

格式化输出xml

  1. [root@centos-04 tmp]# vim 74.sh
  2. #!/bin/bash
  3. #这个脚本用来格式化xml文件
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. sed '/<!--.*-->/d' test.xml > test2.xml #注释在一行的情况
  8. egrep -n '<!--|\-\->' test2.xml |awk -F ':' '{print $1}' > /tmp/line_number1.txt #注释在两行的情况计算行号
  9. n=`wc -l /tmp/line_number1.txt|awk '{print $1}'`
  10. n1=$[$n/2]
  11. for i in `seq 1 $n1`
  12. do
  13. j=$[$i*2]
  14. k=$[$j-1]
  15. x=`sed -n "$k"p /tmp/line_number1.txt`
  16. y=`sed -n "$j"p /tmp/line_number1.txt`
  17. sed -i "$x,$y"d test2.xml
  18. done
  19.  
  20. grep -n 'artifactItem>' test2.xml |awk '{print $1}' |sed 's/://' > /tmp/line_number2.txt
  21. n2=`wc -l /tmp/line_number2.txt|awk '{print $1}'`
  22.  
  23. get_value()
  24. {
  25. sed -n "$1,$2"p test2.xml|awk -F '<' '{print $2}' |awk -F '>' '{print $1,$2}' > /tmp/value.txt
  26.  
  27. cat /tmp/value.txt|while read line
  28. do
  29. x=`echo $line|awk '{print $1}'`
  30. y=`echo $line|awk '{print $2}'`
  31. echo artifactItem:$x:$y
  32. done
  33. }
  34.  
  35. n3=$[$n2/2]
  36. for j in `seq 1 $n3`
  37. do
  38. m1=$[$j*2-1]
  39. m2=$[$j*2]
  40. nu1=`sed -n "$m1"p /tmp/line_number2.txt`
  41. nu2=`sed -n "$m2"p /tmp/line_number2.txt`
  42. nu3=$[$nu1+1]
  43. nu4=$[$nu2-1]
  44. get_value $nu3 $nu4
  45. done

小函数

  1. [root@centos-04 tmp]# vim 75.sh
  2. #!/bin/bash
  3. #这个脚本用来写一个小函数
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. f_judge()
  8. {
  9. if [ -d /home/log ]
  10. then
  11. #find /home -name "tmp*" |xargs -i mv {} /home/log/
  12. find /home -name "tmp*" -exec mv {} /home/log/ \;
  13. else
  14. mkdir /home/log
  15. exit
  16. fi
  17.  
  18. }
  19.  
  20. f_judge

批量杀进程

殺死tomcat進程

  1. [root@centos-04 tmp]# ps aux |grep tomcat |awk '{print $2}' |xargs kill
  1. [root@centos-04 tmp]# vim 76.sh
  2. #!/bin/bash
  3. #这个脚本用来批量杀tomcat进程
  4. #作者:SYJ
  5. #日期:2019-04-09
  6. cat > kill_tomcat.expect <<EOF
  7. #!/usr/bin/expect
  8. set passwd [lindex \$argv 0]
  9. set host [lindex \$argv 1]
  10. spawn ssh root@\$host
  11.  
  12. expect {
  13. "yes/no" { send "yes\r"; exp_continue}
  14. "password:" { send "\$passwd\r" }
  15.  
  16. }
  17.  
  18. expect "]*"
  19. send "killall java\r"
  20. expect "]*"
  21. send "exit\r"
  22. EOF
  23.  
  24. chmod a+x kill_tomcat.expect
  25.  
  26. cat ip-pwd.ini|while read line
  27. do
  28. ip=`echo $line |awk -F ',' '{print $1}'`
  29. pw=`echo $line |awk -F ',' '{print $3}'`
  30. ./kill_tomcat.expect $pw $ip
  31. done

查找老日志打包  

  1. [root@centos-04 tmp]# vim 77.sh
  2. #!/bin/bash
  3. #这个脚本用来查找老日志打包
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. cd /data/log
  8. fin . -type f -name "*.log" -mtime +3 > /tmp/old_log
  9.  
  10. d=`date +%F`
  11. tar czf $d.tar.gz `cat /tmp/old_log|xargs`
  12. rsync -a $d.tar.gz 192.168.1.2:/data/log/
  13. cat /tmp/old_log|xargs rm

处理文本

  1. [root@centos-04 tmp]# vim 78.sh
  2. #!/bin/bash
  3. #这个脚本用来处理文本
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. for w in `awk -F ':' '{print $1}' 3.txt |sort |uniq`
  8. do
  9. echo "[$w]"
  10. awk -v w2=$w -F ':' '$1==W2 {print $2}' 3.txt
  11. done

批量删除日志

  1. [root@centos-04 tmp]# vim 79.sh
  2. #!/bin/bash
  3. #这个脚本用来删除老日志
  4. #作者:SYJ
  5. #日期:2019-04-09
  6.  
  7. dir1=/opt/cloud/log/
  8. dir2=/opt/cloud/instance/
  9.  
  10. if [ -d $dir1 ]
  11. then
  12. find $dir1 -type f -mtime +7 |xargs rm
  13. elif [ -d $dir2 ]
  14. then
  15. find $dir2 -name "*.log" -type f -mtime +15 |xargs rm
  16. fi

房贷计算器

  1. #!/bin/bash
  2. #这个脚本用来实现简易的房贷计算器
  3. #作者:猿课-阿铭 www.apelearn.com
  4. #日期:2018-12-12
  5.  
  6. read -p "请输入贷款总额(单位:万元):" sum_w
  7. read -p "请输入贷款年利率(如年利率为6.5%,直接输入6.5):" y_r
  8. read -p "请输入贷款年限(单位:年):" y_n
  9. echo "贷款计算方式:"
  10. echo "1)等额本金计算法"
  11. echo "2)等额本息计算法"
  12. read -p "请选择贷款方式(1|2)" type
  13. #贷款总额
  14. sum=`echo "scale=2;$sum_w*10000 " | bc -l`
  15. #年利率
  16. y_r2=`echo "scale=6;$y_r/100 " | bc -l`
  17. #月利率
  18. m_r=`echo "scale=6;$y_r2/12 " | bc -l`
  19. #期数
  20. count=$[$y_n*12]
  21. echo "期次 本月还款额 本月利息 未还款额"
  22.  
  23. jin()
  24. {
  25. #月还款本金m_jin=贷款总额sum/期数count
  26. m_jin=`echo "scale=2;($sum/$count)/1 " | bc -l`
  27. #定义未还本金r_jin(第一期应该是贷款总额)
  28. r_jin=$sum
  29. for((i=1;i<=$count;i++))
  30. do
  31. #本月利息m_xi=剩余本金*月利率
  32. m_xi=`echo "scale=2;( $r_jin*$m_r)/1"|bc -l`
  33. #本月还款m_jinxi=月还本金m_jin+本月利息m_xi
  34. m_jinxi=`echo "scale=2;( $m_jin+$m_xi)/1"|bc -l`
  35. #已还款本金jin=月还本金m_jin*期数i
  36. jin=`echo "scale=2;( $m_jin*$i)/1"|bc -l`
  37. #剩余本金r_jin=贷款总额sum-已还本金jin
  38. r_jin=`echo "scale=2;( $sum-$jin)/1"|bc -l`
  39. if [ $i -eq $count ]
  40. then
  41. #最后一月的还款额应该是每月还款本金+本月利息+剩余本金
  42. m_jinxi=`echo "scale=2;( $m_jin+$r_jin+$m_xi)/1"|bc -l`
  43. #最后一月的剩余本金应该是0
  44. r_jin=0
  45. fi
  46. echo "$i $m_jinxi $m_xi $r_jin"
  47. done
  48. }
  49.  
  50. xi()
  51. {
  52. #每期还款m_jinxi=(贷款总额sum*月利率m_r*((1+月利率m_r)^期数count))/(((1+月利率m_r)^期数count)-1)
  53. m_jinxi=`echo "scale=2;(($sum*$m_r*((1+$m_r)^$count))/(((1+$m_r)^$count)-1))/1 " | bc -l`
  54. #定义未还本金r_jin(第一期应该是贷款总额)
  55. r_jin=$sum
  56. for((i=1;i<=$count;i++))
  57. do
  58. #本期利息m_xi=剩余本金r_jin*月利率m_r
  59. m_xi=`echo "scale=2;( $r_jin*$m_r)/1"|bc -l`
  60. #本期本金m_jin=本期本息m_jinxi-本期利息m_xi
  61. m_jin=`echo "scale=2;($m_jinxi-$m_xi)/1 " | bc -l`
  62. #未还本金r_jin=上期未还本金r_jin-本期应还本金m_jin
  63. r_jin=`echo "scale=2;($r_jin-$m_jin)/1 " | bc -l`
  64. if [ $i -eq $count ]
  65. then
  66. #最后一月本息m_jinxi=本期本金m_jin+未还本金r_jin
  67. m_jinxi=`echo "scale=2;($m_jin+$r_jin)/1 " | bc -l`
  68. #最后一月的剩余本金应该是0
  69. r_jin="0.00"
  70. fi
  71. echo "$i $m_jinxi $m_xi $r_jin"
  72. done
  73. }
  74.  
  75. case $type in
  76. 1)
  77. jin
  78. ;;
  79. 2)
  80. xi
  81. ;;
  82. *)
  83. exit 1
  84. ;;
  85. esac

监控磁盘  

  1. [root@centos-04 tmp]# vim 81.sh
  2. #!/bin/bash
  3. #这个脚本用来监控磁盘IO
  4. #作者:SYJ
  5. #日期:2019-04-10
  6.  
  7. if ! while iostat &>/dev/null
  8. then
  9. yum install -y sysstat
  10. fi
  11.  
  12. while :
  13. do
  14. t=`date +%T`
  15. iostat -xd 1 5 |grep '^sda' > /tmp/io.log
  16. sum=`awk '{sum=sum+$NF} END {print sum}' /tmp/io.log`
  17. a=`echo "scale=2;$sum/5" |bc`
  18. b=`echo $a|cut -d . -f 1`
  19. if [ $b -gt 90 ]
  20. then
  21. mysql -uroot -pxxx -e "show processlist" > mysql_$t.log
  22. fi
  23. sleep 1
  24. done

查看tomcat日志

  1. #!/bin/bash
  2. #这个脚本用来查看Tomcat日志
  3. #作者:猿课-阿铭 www.apelearn.com
  4. #日期:2018-12-12
  5.  
  6. LANG=en
  7. logfile="/opt/TOM/$1/logs/catalina.out"
  8.  
  9. #将当天的英文月、数字日期、数字年作为变量赋值给d_mdy
  10. d_mdy=`date "+%b %d, %Y"`
  11.  
  12. #判断参数个数
  13. if [ $# -ne 2 ] && [ $# -ne 3 ]
  14. then
  15. echo "你提供的参数个数不对,请提供2个或者3个参数。例:sh $0 t1 08:01:00 14:00:00"
  16. exit 1
  17. fi
  18.  
  19. #判断第一个参数是否符合要求
  20. if ! echo $1|grep -qE '^t1$|^t2$|^t3$|^t4$'
  21. then
  22. echo "第一个参数必须是t1、t2、t3或t4"
  23. exit 1
  24. fi
  25.  
  26. #判断时间有效性
  27. judge_time()
  28. {
  29. date -d "$1" +%s &>/dev/null
  30. if [ $? -ne 0 ]
  31. then
  32. echo "你提供的时间$1格式不正确"
  33. exit 1
  34. fi
  35. }
  36.  
  37. #将24小时制时间转换为12小时
  38. tr_24_12()
  39. {
  40. date -d "$1" +%r
  41. }
  42.  
  43. #判断提供的时间点是否在日志中出现
  44. judge_time_in_log()
  45. {
  46. if ! grep -q "$d_mdy $(tr_24_12 $1)" $logfile
  47. then
  48. echo "你提供的时间$1在日志$logfile中不曾出现,请换一个时间点"
  49. exit 1
  50. fi
  51. }
  52.  
  53. #判断第2个参数是否合法
  54. judge_time $2
  55.  
  56. #判断起始时间点是否出现在日志里
  57. judge_time_in_log $2
  58.  
  59. #如果提供第3个参数
  60. if [ $# -eq 3 ]
  61. then
  62. #判断第3个参数是否合法
  63. judge_time $3
  64.  
  65. #判断起始时间是否早于结束时间
  66. t1=`date -d "$2" +%s`
  67. t2=`date -d "$3" +%s`
  68. if [ $t2 -lt $t1 ]
  69. then
  70. echo "你提供的时间$2比$3要晚,应该把早的时间放到前面"
  71. exit
  72. fi
  73.  
  74. #判断提供的结束时间点是否出现在日志中
  75. judge_time_in_log $3
  76. fi
  77.  
  78. #取起始时间所在行行号
  79. begin_n=`grep -n "$d_mdy $(tr_24_12 $2)" $logfile|head -1|awk -F ':' '{print $1}'`
  80.  
  81. #取结束时间所在行行号,并用sed截取日志内容
  82. if [ $# -eq 3 ]
  83. then
  84. n=`grep -n "$d_mdy $(tr_24_12 $3)" $logfile|tail -1|awk -F ':' '{print $1}'`
  85. #结束日期所在行的下一行才是日志的内容
  86. end_n=$[$n+1]
  87. sed -n "$begin_n,$end_n"p $logfile
  88. else
  89. sed -n "$begin_n,$"p $logfile
  90. fi

打印城市名字

打印数组

  1. [root@centos-04 tmp]# a=(1,2,3,4,5)
  2. [root@centos-04 tmp]# echo ${a[@]}
  3. 1,2,3,4,5
  4. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# a="1 2 3"
  2. [root@centos-04 tmp]# echo $a
  3. 1 2 3
  4. [root@centos-04 tmp]# b=($a)(把带空格的字符串变成数组加一个()就行了,这样b就变成一个数组了)
  5. [root@centos-04 tmp]# echo $b
  6. 1
  7. [root@centos-04 tmp]# echo ${b[@]}
  8. 1 2 3
  9. [root@centos-04 tmp]#   

获取数组的个数

  1. [root@centos-04 tmp]# echo ${#b[@]}
  2. 3
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 83.sh
  2. #!/bin/bash
  3. #这个脚本用来打印城市名字
  4. #作者:SYJ
  5. #日期:2019-04-10
  6. read -p "输入不少于5个城市的名字,用空格分隔开。" name
  7.  
  8. n=`echo $name|awk '{print NF}'`
  9. if [ $n -lt 5 ]
  10. then
  11. echo "请输入至少5个城市的名字。"
  12. exit
  13. fi
  14.  
  15. city=($name)
  16.  
  17. #echo ${city[@]}
  18.  
  19. for i in `seq 0 $[${#city[@]}-1]`
  20. do
  21. echo ${city[$i]}
  22. done
  1. [root@centos-04 tmp]# sh 83.sh
  2. 输入不少于5个城市的名字,用空格分隔开。1 2 3 4 5
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. [root@centos-04 tmp]#

代码上线  

  1. [root@centos-04 tmp]# vim 84.sh
  2. #!/bin/bash
  3. #这个脚本用来代码上线
  4. #作者:SYJ
  5. #日期:2019-04-10
  6. dir=/data/wwwroot/www.aaa.com
  7. B_Ip=1.1.1.1
  8. C_Ip=2.2.2.2
  9.  
  10. rs()
  11. {
  12. rsync -azP --exclude="logs" \
  13. --exclude="tmp" --exclude="upload" \
  14. --exclude="caches" $dir/ $1:$dir/
  15.  
  16. }
  17.  
  18. read -p "该脚本将会把本机的$dir下的文件同步到$B_IP和$c_ip上,是否要继续?y|n" c
  19.  
  20. case $c in
  21. y|Y)
  22. rs B_IP
  23. rs C_IP
  24. ;;
  25. n|N)
  26. exit
  27. ;;
  28. *)
  29. echo "你只能输入y或者n。"
  30. ;;
  31. esac

统计并发量  

  1. [root@centos-04 tmp]# date -d "-1 second" +%d/%b/%Y:%T
  2. 10/4月/2019:23:15:59
  3. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 85.sh
  2. #!/bin/bash
  3. #这个脚本用来计算网站并发量
  4. #作者:SYJ
  5. #日期:2019-04-10
  6.  
  7. LANG=en
  8. t=`date -d "-1 second" + %d/%b/%Y:%T`
  9. log=/data/logs/www.aaa.com_access.log
  10.  
  11. tail -1000 $log |grep -c "$t"

关闭服务

chkconfig --list查看到服务的状态(查看第三列)

  1. [root@centos-04 tmp]# chkconfig --list
  2.  
  3. 注:该输出结果只显示 SysV 服务,并不包含
  4. 原生 systemd 服务。SysV 配置数据
  5. 可能被原生 systemd 配置覆盖。
  6.  
  7. 要列出 systemd 服务,请执行 'systemctl list-unit-files'
  8. 查看在具体 target 启用的服务请执行
  9. 'systemctl list-dependencies [target]'
  10.  
  11. netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
  12. network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
  13. nginx 0:关 1:关 2:开 3:开 4:开 5:开 6:关
  14. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# LANG=en
  2. [root@centos-04 tmp]# chkconfig --list
  3.  
  4. Note: This output shows SysV services only and does not include native
  5. systemd services. SysV configuration data might be overridden by native
  6. systemd configuration.
  7.  
  8. If you want to list systemd services use 'systemctl list-unit-files'.
  9. To see services enabled on particular target use
  10. 'systemctl list-dependencies [target]'.
  11.  
  12. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  13. network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  14. nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  15. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# chkconfig --list |grep '3:on' |awk '{print $1}'
  2.  
  3. Note: This output shows SysV services only and does not include native
  4. systemd services. SysV configuration data might be overridden by native
  5. systemd configuration.
  6.  
  7. If you want to list systemd services use 'systemctl list-unit-files'.
  8. To see services enabled on particular target use
  9. 'systemctl list-dependencies [target]'.
  10.  
  11. network
  12. nginx
  13. [root@centos-04 tmp]#
  1. [root@centos-04 tmp]# vim 86.sh
  2. #!/bin/bash
  3. #这个脚本用来关闭服务
  4. #作者:SYJ
  5. #日期:2019-04-10
  6.  
  7. LANG=en
  8.  
  9. while :
  10. do
  11. chkconfig --list 2>/dev/null|grep '3:on' |awk '{print $1}' > /tmp/on_sev.txt
  12. echo -e "\033[32m系统里开启了这些服务: \033[0m"
  13. cat /tmp/on_sev.txt
  14. echo
  15. read -p "Please select a service from this list." s
  16.  
  17. if ! grep -qw "$s" /tmp/on_sev.txt
  18. then
  19. echo -e "\033[31m The service is not in the service list.\33[0m"
  20. continue
  21. fi
  22. chkconfig $s off
  23. break
  24. done
  25. ~
  26. ~
  27. ~
  28. ~
  29. ~
  30. ~
  31. ~
  32. ~
  33. ~
  34. ~
  35. "86.sh" 23L, 474C 已写入
  36. [root@centos-04 tmp]# sh 86.sh
  37. 系统里开启了这些服务:
  38. network
  39. nginx
  40.  
  41. Please select a service from this list.nginx
  42. [root@centos-04 tmp]# sh 86.sh
  43. 系统里开启了这些服务:
  44. network
  45.  
  46. Please select a service from this list.

彻底关闭tomcat服务

  1. [root@centos-04 tmp]# vim 87.sh
  2. #!/bin/bash
  3. #这个脚本用来彻底杀死Tomcat进程
  4. #作者:SYJ
  5. #日期:2019-04-10
  6.  
  7. dir=/usr/local/tomcat/bin/
  8.  
  9. java_pc()
  10. {
  11. pgrep java|wc -l
  12. }
  13.  
  14. cd $dir
  15. ./shutdown.sh
  16.  
  17. count=0
  18.  
  19. while [ $count -lt 5 ]:
  20. do
  21. n=`java_pc`
  22. if [ $n -gt 0 ]
  23. then
  24. killall java
  25. count=$[$count+1]
  26. sleep 1
  27. else
  28. break
  29. fi
  30. done
  31.  
  32. n=`java_pc`
  33. if [ $n -gt 0 ]
  34. then
  35. killall -9 java
  36. fi
  37.  
  38. n=`java_pc`
  39. if [ $n -gt 0 ]
  40. then
  41. echo "Tomcat can not killed"
  42. exit
  43. fi
  44.  
  45. cd $dir
  46. ./startup.sh

去掉文件名后缀  

  1. [root@centos-04 tmp]# vim 88.sh
  2. #!/bin/bash
  3. #这个脚本用来去掉文件名后缀
  4. #作者:SYJ
  5. #日期:2019-04-10
  6.  
  7. for f in `ls -d ./*.bak`
  8. do
  9. # mv $f `echo $f|sed 's/.bak$//'`
  10. f1=`echo $f|awk -F '.bak$' {print $1}`
  11. mv $f $f1
  12. done

检查域名是否到期  

  1. [root@centos-04 tmp]# whois baidu.com |grep 'Expiration Date'
  2. Registrar Registration Expiration Date: 2026-10-11T00:00:00-0700
  3. [root@centos-04 tmp]#
  1. #!/bin/bash
  2. #这个脚本用来检查域名是否到期
  3. #作者:猿课-阿铭 www.apelearn.com
  4. #日期:2018-12-14
  5.  
  6. mail_u=admin@admin.com
  7. #当前日期时间戳,用于和域名的到期时间做比较
  8. t1=`date +%s`
  9.  
  10. #检测whois命令是否存在,不存在则安装jwhois包
  11. is_install_whois()
  12. {
  13. which whois >/dev/null 2>/dev/null
  14. if [ $? -ne 0 ]
  15. then
  16. yum install -y epel-release
  17. yum install -y jwhois
  18. fi
  19. }
  20.  
  21. notify()
  22. {
  23. #e_d=`whois $1|grep 'Expiry Date'|awk '{print $4}'|cut -d 'T' -f 1`
  24. e_d=`whois $1|grep 'Expiration'|tail -1 |awk '{print $5}' |awk -F 'T' '{print $1}'`
  25. #如果e_d的值为空,则过滤关键词'Expiration Time'
  26. if [ -z "$e_d" ]
  27. then
  28. e_d=`whois $1|grep 'Expiration Time'|awk '{print $3}'`
  29. fi
  30. #将域名过期的日期转化为时间戳
  31. e_t=`date -d "$e_d" +%s`
  32. #计算一周一共有多少秒
  33. n=`echo "86400*7"|bc`
  34. e_t1=$[$e_t-$n]
  35. e_t2=$[$e_t+$n]
  36. if [ $t1 -ge $e_t1 ] && [ $t1 -lt $e_t ]
  37. then
  38. python mail.py $mail_u "Domain $1 will to be expired." "Domain $1 expire date is $e_d."
  39. fi
  40. if [ $t1 -ge $e_t ] && [ $t1 -lt $e_t2 ]
  41. then
  42. python mail.py $mail_u "Domain $1 has been expired" "Domain $1 expire date is $e_d."
  43. fi
  44. }
  45.  
  46. #检测上次运行的whois查询进程是否存在
  47. #若存在,需要杀死进程,以免影响本次脚本执行
  48. if pgrep whois &>/dev/null
  49. then
  50. killall -9 whois
  51. fi
  52.  
  53. is_install_whois
  54.  
  55. for d in aaa.com bbb.com aaa.cn
  56. do
  57. notify $d
  58. done

自动密钥认证  

  1. #!/bin/bash
  2. #这个脚本用来自动配置密钥认证
  3. #作者:猿课-阿铭 www.apelearn.com
  4. #日期:2018-12-14
  5.  
  6. read -p "输入一个IP地址: " ip
  7. read -p "输入此机器的root密码: " pasd
  8.  
  9. is_install()
  10. {
  11. if ! rpm -q $1 &>/dev/null
  12. then
  13. yum install -y $1
  14. fi
  15. }
  16.  
  17. is_install openssh-clients
  18. is_install expect
  19.  
  20. if [ ! -f ~/.ssh/id_rsa.pub ]
  21. then
  22. echo -e "\n" |ssh-keygen -P ''
  23. fi
  24.  
  25. cat > key.expect <<EOF
  26. #!/usr/bin/expect
  27. set host [lindex \$argv 0]
  28. set passwd [lindex \$argv 1]
  29. spawn ssh-copy-id root@\$host
  30. expect {
  31. "yes/no" { send "yes\r"; exp_continue}
  32. "password:" { send "\$passwd\r" }
  33. }
  34. expect eof
  35. EOF
  36.  
  37. chmod a+x key.expect
  38.  
  39. ./key.expect $ip $pasd  

普通我们将本机的ssh公钥放到另一台机器的方法(id_rsa.pub文件内容放到对方机器的authorized_keys里面)

  1. root@centos-04 tmp]# cat ~/.ssh/id_rsa.pub
  2. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6
    rBeLOw210kG7r9cfU/yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb
    7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04
  3. [root@centos-04 tmp]# vim ~/.ssh/authorized_keys
  4. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6
  5. rBeLOw210kG7r9cfU/yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb
  6. 7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 roo
  7. t@centos-04  

我们也可以用ssh-copy-id命令实现,输入对方的用户名和ip

  1. [root@centos-04 tmp]# ssh-copy-id root@1.1.1.1
  1. [root@centos-04 tmp]# vim 1.expect
  2. #!/usr/bin/expect
  3. set host [lindex $argv 0]
  4. set passwd [lindex $argv 1]
  5. spawn ssh-copy-id root@$host
  6. expect {
  7. "yes/no" { send "yes\r"; exp_continue}
  8. "password:" { send "$passwd\r" }
  9. }
  10. expect eof
  11. [root@centos-04 tmp]# ./1.expect 127.0.0.1 root
  12. spawn ssh-copy-id root@127.0.0.1
  13. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  14. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  15.  
  16. /usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
  17. (if you think this is a mistake, you may want to use -f option)
  18.  
  19. expect: spawn id exp6 not open
  20. while executing
  21. "expect eof"
  22. (file "./1.expect" line 9)
  23. [root@centos-04 tmp]#
  24. [root@centos-04 tmp]# ssh-copy-id root@127.0.0.1
  25. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  26. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  27.  
  28. /usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
  29. (if you think this is a mistake, you may want to use -f option)
  30.  
  31. [root@centos-04 tmp]# ssh-copy-id -f root@127.0.0.1
  32. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  33.  
  34. Number of key(s) added: 1
  35.  
  36. Now try logging into the machine, with: "ssh 'root@127.0.0.1'"
  37. and check to make sure that only the key(s) you wanted were added.
  38.  
  39. [root@centos-04 tmp]#
  40. [root@centos-04 tmp]# cat /root/.ssh/id_rsa.pub
  41. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6rBeLOw210kG7r9cfU/
    yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY
    7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04
  42. [root@centos-04 tmp]#
  43. [root@centos-04 tmp]# cat /root/.ssh/authorized_keys
  44. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6rBeLOw210kG7r9cfU/
    yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY
    7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04
  45. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6rBeLOw210kG7r9cfU/
    yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY
    7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04
  46. [root@centos-04 tmp]#   

查看一个命令由那个包装的

  1. [root@centos-04 tmp]# which ssh-copy-id
  2. /usr/bin/ssh-copy-id
  3. [root@centos-04 tmp]# rpm -qf /usr/bin/ssh-copy-id
  4. openssh-clients-7.4p1-16.el7.x86_64
  5. [root@centos-04 tmp]#

部署MySQL主从

  1. [root@centos-04 tmp]# vim 91.sh
  2. #!/bin/bash
  3. #这个脚本用来配置MySQL主从同步
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-17
  6.  
  7. #!/bin/bash
  8. master_ip=192.168.100.12
  9. slave_ip=192.168.100.13
  10. mysqlc="mysql -uroot -paminglinux"
  11.  
  12. check_ok()
  13. {
  14. if [ $? -ne 0 ]
  15. then
  16. echo "$1 出错了。"
  17. exit 1
  18. fi
  19. }
  20.  
  21. f_exist()
  22. {
  23. d=`date +%F%T`
  24. if [ -f $1 ]
  25. then
  26. mv $1 $1_$d
  27. fi
  28. }
  29.  
  30. ## 设置主mysql配置
  31. if ! grep '^server-id' /etc/my.cnf
  32. then
  33.  
  34. if ! grep '^log-bin.*=.*' /etc/my.cnf
  35. then
  36. sed -i '/^\[mysqld\]$/a\log-bin = aminglinux' /etc/my.cnf
  37. fi
  38.  
  39. sed -i '/^log-bin.*/a\binlog-ignore-db = mysql ' /etc/my.cnf
  40.  
  41. /etc/init.d/mysqld restart
  42. check_ok "主上重启mysql"
  43.  
  44. ## 登录mysql,授权用户、锁表以及show master status。
  45. $mysqlc <<EOF
  46. grant replication slave on *.* to 'repl'@$slave_ip identified by 'yourpassword';
  47. flush tables with read lock;
  48. EOF
  49. $mysqlc -e "show master status" > /tmp/master.log
  50. file=`tail -1 /tmp/master.log|awk '{print $1}'`
  51. pos=`tail -1 /tmp/master.log|awk '{print $2}'`
  52.  
  53. ## 创建在从上配置和操作的脚本
  54. f_exist /tmp/slave.sh
  55.  
  56. cat > /tmp/slave.sh << EOF
  57. #!/bin/bash
  58. if ! grep '^server-id' /etc/my.cnf
  59. then
  60. sed -i '/^\[mysqld\]$/a\server-id = 1002' /etc/my.cnf
  61. fi
  62.  
  63. /etc/init.d/mysqld restart
  64. check_ok "从上重启mysql"
  65.  
  66. $mysqlc <<EOF
  67. stop slave;
  68. start slave;
  69. EOF
  70. EOF
  71.  
  72. ## 创建传输slave.sh的expect脚本
  73. f_exist /tmp/rs_slave.expect
  74.  
  75. cat > /tmp/rs_slave.expect <<EOF
  76. #!/usr/bin/expect
  77. set passwd "aminglinux"
  78. spawn rsync -a /tmp/slave.sh root@$slave_ip:/tmp/slave.sh
  79. expect {
  80. "yes/no" { send "yes\r"}
  81. "password:" { send "\$passwd\r" }
  82. }
  83. expect eof
  84. EOF
  85.  
  86. ## 执行expect脚本
  87. chmod +x /tmp/rs_slave.expect
  88. /tmp/rs_slave.expect
  89. check_ok "传输slave.sh"
  90.  
  91. ## 创建远程执行命令的expect脚本
  92. f_exist /tmp/exe.expect
  93.  
  94. cat > /tmp/exe.expect <<EOF
  95. #!/usr/bin/expect
  96. set passwd "aminglinux"
  97. spawn ssh root@$slave_ip
  98. expect {
  99. "yes/no" { send "yes\r"}
  100. "password:" { send "\$passwd\r" }
  101. }
  102. expect "]*"
  103. send "/bin/bash /tmp/slave.sh\r"
  104. expect "]*"
  105. send "exit\r"
  106. EOF
  107.  
  108. ## 执行expect脚本
  109. chmod +x /tmp/exe.expect
  110. /tmp/exe.expect
  111. check_ok "远程执行slave.sh"
  112.  
  113. ## 主上解锁表
  114. $mysqlc -e "unlock tables"

管理docker  

  1. [root@centos-04 tmp]# vim 92.sh
  2. #!/bin/bash
  3. #这个脚本用来管理docker容器
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-17
  6.  
  7. while true
  8. do
  9. read -p "请输入你要执行的操作:(stop/start/rm) " opt
  10. if [ -z "$opt" ]
  11. then
  12. echo "请输入要执行的操作。"
  13. continue
  14. else
  15. break
  16. fi
  17. done
  18.  
  19. docker ps -a |awk '{print $1}' > /tmp/id.txt
  20. case $opt in
  21. stop)
  22. for id in `cat /tmp/id.txt`
  23. do
  24. docker stop $id
  25. done
  26. ;;
  27. start)
  28. for id in `cat /tmp/id.txt`
  29. do
  30. docker start $id
  31. done
  32. rm)
  33. for id in `cat /tmp/id.txt`
  34. do
  35. read -p "将要删除容器$id,是否继续?(y|n)" c
  36. case $c in
  37. y|Y)
  38. docker rm -f $id
  39. ;;
  40. n|N)
  41. echo "容器$id不会被删除。"
  42. ;;
  43. *)
  44. echo "你只能输入'y'或者'n'。"
  45. ;;
  46. esac
  47. done
  48. *)
  49. echo "你只能输入start/stop/rm。"
  50. ;;
  51. esac

安装配置samba  

  1. [root@centos-04 tmp]# vim 93.sh
  2. #!/bin/bash
  3. #这个脚本用来一键安装并配置samba
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-17
  6.  
  7. if [ "$#" -ne 1 ]
  8. then
  9. echo "运行脚本的格式为:$0 /dir/"
  10. exit 1
  11. else
  12. if ! echo $1 |grep -q '^/.*'
  13. then
  14. echo "请提供一个绝对路径。"
  15. exit 1
  16. fi
  17. fi
  18.  
  19. if ! rpm -q samba >/dev/null
  20. then
  21. echo "将要安装samba"
  22. sleep 1
  23. yum install -y samba
  24. if [ $? -ne 0 ]
  25. then
  26. echo "samba安装失败"
  27. exit 1
  28. fi
  29. fi
  30.  
  31. cnfdir="/etc/samba/smb.conf"
  32. cat >> $cnfdir <<EOF
  33. [share]
  34. comment = share all
  35. path = $1
  36. browseable = yes
  37. public = yes
  38. writable = no
  39. EOF
  40.  
  41. if [ ! -d $1 ]
  42. then
  43. mkdir -p $1
  44. fi
  45.  
  46. chmod 777 $1
  47. echo "test" > $1/test.txt
  48.  
  49. #假设系统为CentOS7
  50. systemctl start smb
  51. if [ $? -ne 0 ]
  52. then
  53. echo "samba服务启动失败,请检查配置文件是否正确。"
  54. else
  55. echo "samba配置完毕,请验证。"
  56. fi

批量查看多台机器负载  

指定文件名字(aaa)的秘钥

  1. [root@centos-04 tmp]# ssh-keygen -f /tmp/aaa
  2. Generating public/private rsa key pair.
  3. Enter passphrase (empty for no passphrase):
  4. Enter same passphrase again:
  5. Your identification has been saved in /tmp/aaa.
  6. Your public key has been saved in /tmp/aaa.pub.
  7. The key fingerprint is:
  8. SHA256:1P7xXAJP++vRPNHXETWP/3LZDHJRHVHdxoNUewrA868 root@centos-04
  9. The key's randomart image is:
  10. +---[RSA 2048]----+
  11. | .. ..oB%|
  12. | .o. . +@|
  13. | . .oo +++|
  14. | . . .= ==|
  15. | S . o.B.*|
  16. | . *.BB|
  17. | ..++O|
  18. | E o+|
  19. | .o |
  20. +----[SHA256]-----+
  21. [root@centos-04 tmp]# ls
  22. 1.expect 3191.txt 6542.txt 6569.txt 66.sh 68.sh 71.sh 76.sh 80.sh 86.sh 93.sh nginx_proxy_tmp
  23. 1.txt 3.txt 6564.txt 6598.txt 67.sh 6969.txt 72.sh 7772.txt 81.sh 87.sh aaa on_sev.txt
  24. 2.txt 456.log 6565.txt 65.sh 6835.txt 69.sh 73.sh 77.sh 83.sh 88.sh aaa.pub pid.txt
  25. 3161.txt 4.txt 6567.txt 6606.txt 6836.txt 7046.txt 74.sh 78.sh 84.sh 91.sh c.txt proxy.log
  26. 3184.txt 64.sh 6568.txt 6610.txt 6838.txt 70.sh 75.sh 79.sh 85.sh 92.sh md5.txt
  27. [root@centos-04 tmp]# cat aaa(私钥)
  28. -----BEGIN RSA PRIVATE KEY-----
  29. Proc-Type: 4,ENCRYPTED
  30. DEK-Info: AES-128-CBC,6471B3E0E269F92672EABB4201EC8314
  31.  
  32. PF7gcBUGljlSYCX1IazbSPaqRkqA3M5BuDiBe9YkUFXiTTWOcx1eo0aus/GGK4P1
  33. kb1wJEoa7kVjSlB6h0Ed9d2oB08faRKj6yzLGdpQp4I+ZKQMYsXii+QhDT3R0c2c
  34. c9zImVq5Jub6DZtYP7LTs7k2A7Sb1GIio+S3cEEZ1hun84HriFWfViiLKD43crDb
  35. jHTuHv7f4V+J85XHjljB+k7DgZxGx7N+F+/DNjacR/AXP48rq5SD3DWRk0IOmS9T
  36. Iihf7v6euKG3SlKeqRCuJbseeCDVIRNcOuMzrHCzYdj2zAOfNTYNtB9MdEa3rucT
  37. Cg4xaMyJDwDMtGCI/swQ/vTDV/Of/nOC359HHFdFOEdurXULH3nPsU1/qfElLpYe
  38. ims5JYnA5cfvo75iMQowzYHv+VZaKl7lUYldfE5z/n/tgcd5O8HxnzEm1pV1SC6L
  39. dkNM2bjMxlIcpW+nZGLztcF9A5MUy4lRTcjYhgjqTM9LPMpDA1rnQ9FajxQoP1IU
  40. hYsGYCjbjkvy41xtmDH2EgaIoZBI+VDmE6EKlgh4E2H0xROvg1bgYjauF2m2ZVrS
  41. A8tSy7/ox6JB6imUoc8taxGAUP9Cr8Nbe3l7iFkAtqeW+MSFZ7ho53iupUFirmYO
  42. TwDzro9A1z8n4lMc0hn+ZMhVnhSGwr8N+ON7U4puBVRwoM3jyH7HlTbWxsGlDGqG
  43. 4A19p8SgJKStfbDSgIICvGE7hPeegpEMeo/syWXAKj8jNBGmvKg5dG4rPMPGKecI
  44. YTETnYO0Q5a5YyEw8gXu16R7Zbs9YYYbq+pKTQjQC5AeW+kokl416/qG1oF7Yizi
  45. /JZ6lVBTLw3z96/xvd6VW0ZpVCshGL1iZr1k3R4mmO+brNyuCgKjzaS6iOCIc4Xz
  46. doZdEEJsJqiEDbo1lzMc5cZGhNVVdcGiN0BmnaoYWl4OtMX00vAz/ECTyiZt+en+
  47. y1sLR/RpCVzSGACLYwgPHJY/2FQWAN6RDhmQQcCQTWPo7SZdY6j2lM3fmG1MV6Lc
  48. Phb/xUXwm/zsCqSr6Lb0RPL36C/UPBJKz2i8do/xADEaqSardV+WIHWIkz1bSdVo
  49. JRPmW517m/SgzCI74jZ8mW7//DdQ0ciXUd+23DRGcDW+2TqC78vxZmfr7FdzUTZZ
  50. RgVX9TC8XnCxUA4ZiMOpaLdzHDfjllAul3ns6sB/UNQ0ckVX61Wsb81WFoj0VLuN
  51. F5wS4sS3NTYhZ5qqkI0eejkiNraSPtYWd0JTNYuKYPXhiJn3beW3XuWy3AdPannD
  52. Pw0y1RXcwKd79ls+/OvFV/UvWRrSEzwh5qsNZE7gfo9JzHCNaOuHUvLcsrh50I1P
  53. 1hEnMYCpfCPCJBNd0d7JE0bNICTPE6W/7uypxp3POkfi2dqICEhjMUAJ77rhx3ub
  54. X2/Y6NCkZqHfpV8aE1wHKCVHrb5Fj/KP3FZ9xt0eojcp196ZSvNhfO5wrJafRHaE
  55. u56tHnYbtAeKwVp+6NUiv00RjIrsYLtwgAID8OSmL02+E1QlemnU61BrbEXFDUcM
  56. LHjNvSSyG+h98+S5NF7RpXK8DjF6mI+T6u3pVNdjPpZwzXgxiZAsRs8Ss9rld5eD
  57. -----END RSA PRIVATE KEY-----
  58. [root@centos-04 tmp]# cat aaa.pub (公钥)
  59. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWPuKc4lKJzZ+Zunq2RTi7IUP2FkhzZ7fXfH0lUvzom9+3wsarU1cCC5UU1JibgmhuU68
    /yTlj/ZAHa80Sgdyxn2/ZBJFMJcIKyHk5XyZJhhfdLvEB1QORHBYn0rcbPkqpCJcciZLNx1N0soSRVHgOIVimFa1eIAa8zDBjvEXTtl7coL
    MlxzMIj+DOvoDTy7gq+VlPcpk9hnY+AA4TtxEUZ+btc+ZnaaFGxjPkOxiSRp4nOHwtagJrJ4YQYNhfoAuOqNzFQOfoTw2HnCuJF4uM0kjDY
    0Anm9CjQkbWwH4zSinvLeU4fG4POLDqLwbZX7YLDPlGVFShRNZJtiPyoQNv root@centos-04
  60. [root@centos-04 tmp]#     

由于我们在脚本中输入密码不合适,除了用expect我们还可以用ssh-agent命令把密码放到内存里,现在我们在本机登录本机,出现让我们输入密码,这说明我们的秘钥没有生效

  1. [root@centos-04 tmp]# ssh -i /tmp/aaa root@127.0.0.1
  2. root@127.0.0.1's password:   

我们将公钥写到authorized_keys文件里

  1. [root@centos-04 tmp]# cat aaa.pub
  2. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWPuKc4lKJzZ+Zunq2RTi7IUP2FkhzZ7fXfH0lUvzom9+
    3wsarU1cCC5UU1JibgmhuU68/yTlj/ZAHa80Sgdyxn2/ZBJFMJcIKyHk5XyZJhhfdLvEB1QORHBYn0rcbPk
    qpCJcciZLNx1N0soSRVHgOIVimFa1eIAa8zDBjvEXTtl7coLMlxzMIj+DOvoDTy7gq+VlPcpk9hnY+AA4Tt
    xEUZ+btc+ZnaaFGxjPkOxiSRp4nOHwtagJrJ4YQYNhfoAuOqNzFQOfoTw2HnCuJF4uM0kjDY0Anm9CjQkbW
    wH4zSinvLeU4fG4POLDqLwbZX7YLDPlGVFShRNZJtiPyoQNv root@centos-04
  3.  
  4. [root@centos-04 tmp]# vim /root/.ssh/authorized_keys
  5. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWPuKc4lKJzZ+Zunq2RTi7IUP2FkhzZ7fXfH0lUvzom9+3
    wsarU1cCC5UU1JibgmhuU68/yTlj/ZAHa80Sgdyxn2/ZBJFMJcIKyHk5XyZJhhfdLvEB1QORHBYn0rcbPkqp
    CJcciZLNx1N0soSRVHgOIVimFa1eIAa8zDBjvEXTtl7coLMlxzMIj+DOvoDTy7gq+VlPcpk9hnY+AA4TtxEU
    Z+btc+ZnaaFGxjPkOxiSRp4nOHwtagJrJ4YQYNhfoAuOqNzFQOfoTw2HnCuJF4uM0kjDY0Anm9CjQkbWwH4z
    SinvLeU4fG4POLDqLwbZX7YLDPlGVFShRNZJtiPyoQNv root@centos-04  

再次测试让我们输入公钥的密码,输入之后即登录成功

  1. [root@centos-04 tmp]# ssh -i /tmp/aaa root@127.0.0.1
  2. Enter passphrase for key '/tmp/aaa':
  3. Last login: Wed Apr 17 18:22:40 2019 from 192.168.242.1
  4. [root@centos-04 ~]#

ssh-agent可以将私钥放到内存里去,执行ssh-agent命令后再执行命令结果中的前两行。

  1. [root@centos-04 ~]# ssh-agent
  2. SSH_AUTH_SOCK=/tmp/ssh-ZhiIYiItxeaK/agent.8521; export SSH_AUTH_SOCK;
  3. SSH_AGENT_PID=8522; export SSH_AGENT_PID;
  4. echo Agent pid 8522;
  5. [root@centos-04 ~]# SSH_AUTH_SOCK=/tmp/ssh-ZhiIYiItxeaK/agent.8521; export SSH_AUTH_SOCK;
  6. [root@centos-04 ~]# SSH_AGENT_PID=8522; export SSH_AGENT_PID;
  7. [root@centos-04 ~]#   

这样就有了一个秘钥代理,我们把私钥加进去

  1. [root@centos-04 ~]# ssh-add /tmp/aaa
  2. Enter passphrase for /tmp/aaa:
  3. Identity added: /tmp/aaa (/tmp/aaa)
  4. [root@centos-04 ~]#  

我们再次登录发现不需要密码直接登录成功了

  1. [root@centos-04 ~]# ssh -i /tmp/aaa root@127.0.0.1
  2. Last login: Thu Apr 18 01:43:51 2019 from localhost
  3. [root@centos-04 ~]#
  1. [root@centos-04 ~]# ps aux|grep ssh-agent
  2. root 8522 0.0 0.0 72312 1228 ? Ss 01:46 0:00 ssh-agent
  3. root 8579 0.0 0.0 112724 984 pts/2 S+ 01:53 0:00 grep --color=auto ssh-agent
  4. [root@centos-04 ~]# killall ssh-agent
  5. [root@centos-04 ~]# ps aux|grep ssh-agent
  6. root 8582 0.0 0.0 112724 988 pts/2 R+ 01:53 0:00 grep --color=auto ssh-agent
  7. [root@centos-04 ~]#

eval直接执行命令,用ssh-agent > 的方式再执行ssh-add 不能成功,因为这样执行不在一个终端里ssh-add执行失败。

  1. [root@centos-04 ~]# eval echo 123
  2. 123
  1. [root@centos-04 ~]# ssh-agent > /tmp/123.sh
  2. [root@centos-04 ~]# sh /tmp/123.sh
  3. Agent pid 8588
  4. [root@centos-04 ~]#
  1. [root@centos-04 ~]# ssh-add /tmp/aaa
  2. Could not open a connection to your authentication agent.
  3. [root@centos-04 ~]#
  1. [root@centos-04 ~]# killall ssh-agent
  2. [root@centos-04 ~]# ps aux|grep ssh-agent
  3. root 8615 0.0 0.0 112724 988 pts/2 R+ 02:00 0:00 grep --color=auto ssh-agent
  4. [root@centos-04 ~]#

但是我们用eval可以

  1. [root@centos-04 ~]# eval `ssh-agent`
  2. Agent pid 8651
  3. [root@centos-04 ~]# !ps
  4. ps aux|grep ssh-agent
  5. root 8651 0.0 0.0 72312 776 ? Ss 02:02 0:00 ssh-agent
  6. root 8653 0.0 0.0 112724 988 pts/2 R+ 02:02 0:00 grep --color=auto ssh-agent
  7. [root@centos-04 ~]# ssh-add /tmp/aaa
  8. Enter passphrase for /tmp/aaa:
  9. Identity added: /tmp/aaa (/tmp/aaa)
  10. [root@centos-04 ~]#
  1. [root@centos-04 ~]# ssh -i /tmp/aaa root@127.0.0.1
  2. Last login: Thu Apr 18 01:50:32 2019 from localhost
  3. [root@centos-04 ~]#
  1. [root@centos-04 ~]# killall ssh-agent
  2. [root@centos-04 ~]# eval `ssh-agent`; ssh-add /tmp/aaa
  3. Agent pid 8686
  4. Enter passphrase for /tmp/aaa:
  5. Identity added: /tmp/aaa (/tmp/aaa)
  6. [root@centos-04 ~]#   

给命令起个别名

  1. [root@centos-04 ~]# alias ak='eval `ssh-agent`; ssh-add /tmp/aaa'
  2. [root@centos-04 ~]# !kill
  3. killall ssh-agent
  4. [root@centos-04 ~]# ak
  5. Agent pid 8691
  6. Enter passphrase for /tmp/aaa:
  7. Bad passphrase, try again for /tmp/aaa:
  8. Identity added: /tmp/aaa (/tmp/aaa)
  9. [root@centos-04 ~]#
  1. [root@centos-04 tmp]# vim 94.sh
  2. #!/bin/bash
  3. #这个脚本用来批量查机器负载
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-17
  6.  
  7. for ip in `cat /tmp/ip.list`
  8. do
  9. echo $ip
  10. ssh $ip "uptime"
  11. done

自动挂云盘

  1. [root@centos-04 tmp]# vim 95.sh
  2. #!/bin/bash
  3. #这个脚本用来自动挂载磁盘
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-17
  6.  
  7. if [ $# -ne 2 ]
  8. then
  9. echo "Useage $0 盘符 挂载点, 如: $0 /dev/xvdb /data"
  10. exit 1
  11. fi
  12.  
  13. if [ ! -b $1 ]
  14. then
  15. echo "你提供的盘符不正确,请检查后再操作"
  16. exit 1
  17. fi
  18.  
  19. echo "格式化$1"
  20. mkfs -t ext4 $1
  21.  
  22. if [ ! -d $2 ] ;then
  23. mkdir -p $2
  24. fi
  25.  
  26. n=`awk '$NF == "$2"' /etc/fstab|wc -l`
  27. if [ $n -eq 0 ]
  28. then
  29. echo "$1 $2 ext4 defaults 0 0" >> /etc/fstab
  30. mount -a
  31. else
  32. echo "配置文件/etc/fstab中已经存在挂载点$2,请检查一下."
  33. exit 1
  34. fi

并发备份数据库

  1. [root@centos-04 tmp]# screen
  2. [root@centos-04 tmp]# mkfifo 123.fifo
  3. [root@centos-04 tmp]# echo "123" > 123.fifo
  4. ctrl+a d
  5. [root@centos-04 tmp]# cat 123.fifo
  6. 123
  7. [root@centos-04 tmp]# screen -r

  1. [root@centos-04 ~]# mkfifo test10.fifo
  2. [root@centos-04 ~]# exec 10<>test10.fifo
  3. [root@centos-04 ~]# ls -l /dev/fd/10
  4. lrwx------ 1 root root 64 4 18 19:00 /dev/fd/10 -> /root/test10.fifo
  5. [root@centos-04 ~]# read -u10 b
  6.  
  7. [root@centos-04 ~]# ls -l /dev/fd/(查看是否有fifo
  8. 总用量 0
  9. lrwx------ 1 root root 64 4 18 19:09 0 -> /dev/pts/0
  10. lrwx------ 1 root root 64 4 18 19:09 1 -> /dev/pts/0
  11. lrwx------ 1 root root 64 4 18 19:09 10 -> /root/test10.fifo
  12. lrwx------ 1 root root 64 4 18 19:09 2 -> /dev/pts/0
  13. lr-x------ 1 root root 64 4 18 19:09 3 -> /proc/11917/fd
  14. [root@centos-04 ~]#   

如果没有,需要执行

  1. [root@centos-04 ~]# exec 10<> test10.fifo
  2. [root@centos-04 ~]#
  1. [root@centos-04 ~]# echo "3333" >&10
  2. [root@centos-04 ~]#   
  1. screen -r
    [root@centos-04 ~]# echo $b
  2. 3333  

总结:第一步需要将10描述符和test10.fifo命名管道绑定到一起,然后用read命令向10要一个字符串赋值给a,如果有将直接输出,如果没有处于等待状态,我们需要用echo “123” >&10(在终端)将123写入,这样a就有值了也就是123,在screen里echo $a就输出123了。

  1. [root@centos-04 tmp]# vim 96.sh
  2. #!/bin/bash
  3. #这个脚本用来并发备份数据库
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-19
  6.  
  7. ##假设100个库的库名、host、port以及配置文件路径存到了一个文件里,文件名字为/tmp/databases.list
  8. ##格式:db1 10.10.10.2 3308 /data/mysql/db1/my.cnf
  9. ##备份数据库使用xtrabackup(由于涉及到myisam,命令为inoobackupex)
  10.  
  11. exec &> /tmp/mysql_bak.log
  12.  
  13. if ! which innobackupex &>/dev/nll
  14. then
  15. echo "安装xtrabackup工具"
  16. rpm -ivh http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm && \
  17. yum install -y percona-xtrabackup-24
  18. if [ $? -ne 0 ]
  19. then
  20. echo "安装xtrabackup工具出错,请检查。"
  21. exit 1
  22. fi
  23. fi
  24.  
  25. bakdir=/data/backup/mysql
  26. bakuser=vyNctM
  27. bakpass=99omeaBHh
  28.  
  29. function bak_data {
  30. db_name=$1
  31. db_host=$2
  32. db_port=$3
  33. cnf=$4
  34. [ -d $bakdir/$db_name ] || mkdir -p $bakdir/$db_name
  35. innobackupex --defaults-file=$4 --host=$2 --port=$3 --user=$bakuser --password=$bakpass $bakdir/$1
  36. if [ $? -ne 0 ]
  37. then
  38. echo "备份数据库$1出现问题。"
  39. fi
  40. }
  41.  
  42. fifofile=/tmp/$$
  43. mkfifo $fifofile
  44. exec 1000<>$fifofile
  45.  
  46. thread=10
  47. for ((i=0;i<$thread;i++))
  48. do
  49. echo >&1000
  50. done
  51.  
  52. cat /tmp/databases.list | while read line
  53. do
  54. read -u1000
  55. {
  56. bak_data `echo $line`
  57. echo >&1000
  58. } &
  59. done
  60.  
  61. wait
  62. exec 1000>&-
  63. rm -f $fifofile

打印三角形

  1. [root@centos-04 tmp]# vim 97.sh
  2. #!/bin/bash
  3. #这个脚本用来打印三角形
  4. #作者:SYJ
  5. #日期:2019-04-18
  6. #!/bin/bash
  7. #这个脚本用来打印三角形
  8. #作者:猿课-阿铭 www.apelearn.com
  9. #日期:2018-12-19
  10.  
  11. while true
  12. do
  13. read -p "please input the lenth: " n
  14. if [ -z $n ]
  15. then
  16. echo "要输入一个数字。"
  17. continue
  18. else
  19. n1=`echo $n|sed 's/[0-9]//g'`
  20. if [ -n "$n1" ]
  21. then
  22. echo "你输入的不是纯数字,重新输入。"
  23. continue
  24. else
  25. break
  26. fi
  27. fi
  28. done
  29.  
  30. for i in `seq 1 $n`
  31. do
  32. j=$[$n-$i]
  33. for m in `seq $j`
  34. do
  35. echo -n " "
  36. done
  37.  
  38. for p in `seq 1 $i`
  39. do
  40. echo -n "* "
  41. done
  42. echo
  43. done
  1. [root@centos-04 tmp]# sh 97.sh
  2. please input the lenth: 9
  3. *
  4. * *
  5. * * *
  6. * * * *
  7. * * * * *
  8. * * * * * *
  9. * * * * * * *
  10. * * * * * * * *
  11. * * * * * * * * *
  12. [root@centos-04 tmp]#

截取字符串

  1. [root@centos-04 tmp]# vim 98.sh
  2. #!/bin/bash
  3. #这个脚本用来截取字符串
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-19
  6. var=http://www.aaa.com/root/123.htm
  7. echo "1.取出www.aaa.com/root/123.htm"
  8. echo $var |awk -F '//' '{print $2}'
  9.  
  10. echo "2.取出123.htm"
  11. echo $var |awk -F '/' '{print $5}'
  12.  
  13. echo "3.取出http://www.aaa.com/root"
  14. echo $var |sed 's#/123.htm##'
  15.  
  16. echo "4.取出http:"
  17. echo $var |awk -F '//' '{print $1}'
  18.  
  19. echo "5.取出http://"
  20. echo $var |awk -F 'www' '{print $1}'
  21.  
  22. echo "6.取出root/123.htm"
  23. echo $var |awk -F 'com/' '{print $2}'
  24. echo $var |awk -F '/' '{print $4"/"$5}'
  25.  
  26. echo "7.取出123"
  27. echo $var |sed 's/[^0-9]//g'
  1. [root@centos-04 tmp]# sh 98.sh
  2. 1.取出www.aaa.com/root/123.htm
  3. www.aaa.com/root/123.htm
  4. 2.取出123.htm
  5. 123.htm
  6. 3.取出http://www.aaa.com/root
  7. http://www.aaa.com/root
  8. 4.取出http:
  9. http:
  10. 5.取出http://
  11. http://
  12. 6.取出root/123.htm
  13. root/123.htm
  14. root/123.htm
  15. 7.取出123
  16. 123
  17. [root@centos-04 tmp]#

修改文本格式  

  1. [root@centos-04 tmp]# vim 99.sh
  2. #!/bin/bash
  3. #这个脚本用来格式化文本
  4. #作者:猿课-阿铭 www.apelearn.com
  5. #日期:2018-12-19
  6.  
  7. n=`wc -l test3.txt|awk '{print $1}'`
  8. n2=$[$n/2]
  9.  
  10. for i in `seq 1 $n2`
  11. do
  12. i2=$[$i*2]
  13. j=$[$i2-1]
  14. l1=`sed -n "$i2"p test3.txt`
  15. l2=`sed -n "$j"p test3.txt`
  16. echo $l2:$l1
  17. done

自定义rm  

  1. [root@centos-04 tmp]# vim 100.sh
  2. 1. 简单
  3. #!/bin/bash
  4. #这个脚本用来自定义rm
  5. #作者:猿课-阿铭 www.apelearn.com
  6. #日期:2018-12-19
  7.  
  8. filename=$1
  9. big_filesystem=/data/
  10.  
  11. if [ ! -e $1 ]
  12. then
  13. echo "$1 不存在,请使用绝对路径"
  14. exit
  15. fi
  16. d=`date +%Y%m%d%H%M`
  17. read -p "Are U sure delete the file or directory $1? y|n: " c
  18. case $c in
  19. y|Y)
  20. mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/$1 && /bin/rm -rf $1
  21. ;;
  22. n|N)
  23. exit 0
  24. ;;
  25. *)
  26. echo "Please input 'y' or 'n'."
  27. ;;
  28. esac
  29.  
  30. 2.复杂
  31. #!/bin/bash
  32. #这个脚本用来自定义rm
  33. #作者:猿课-阿铭 www.apelearn.com
  34. #日期:2018-12-19
  35.  
  36. #!/bin/bash
  37. filename=$1
  38.  
  39. if [ ! -e $1 ]
  40. then
  41. echo "$1 不存在,请使用绝对路径"
  42. exit
  43. fi
  44. d=`date +%Y%m%d%H%M`
  45. f_size=`du -sk $1|awk '{print $1}'`
  46. disk_size=`LANG=en; df -k |grep -vi filesystem|awk '{print $4}' |sort -n |tail -n1`
  47. big_filesystem=`LANG=en; df -k |grep -vi filesystem |sort -n -k4 |tail -n1 |awk '{print $NF}'`
  48.  
  49. if [ $f_size -lt $disk_size ]
  50. then
  51. read -p "Are U sure delete the file or directory: $1? y|n: " c
  52. case $c in
  53. y|Y)
  54. mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/$1 && /bin/rm -rf $1
  55. ;;
  56. n|N)
  57. exit 0
  58. ;;
  59. *)
  60. echo "Please input 'y' or 'n'."
  61. ;;
  62. esac
  63. else
  64. echo "The disk size is not enough to backup the files $1."
  65. read -p "Do you want to delete $1? y|n: " c
  66. case $c in
  67. y|Y)
  68. echo "It will delete $1 after 5 seconds whitout backup."
  69. for i in `seq 1 5`; do echo -ne ". "; sleep 1;done
  70. echo
  71. /bin/rm -rf $1
  72. ;;
  73. n|N)
  74. echo "It will not delete $1."
  75. exit 0
  76. ;;
  77. *)
  78. echo "Please input 'y' or 'n'."
  79. ;;
  80. esac
  81. fi

  

  

Shell习题100例(2)的更多相关文章

  1. Shell习题100例

    每日一文件 https://github.com/aminglinux/shell100/blob/master/ 要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为20 ...

  2. linux shell习题训练

    shell习题训练 求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2 ...

  3. C语言程序设计100例之(14):丑数

    例14   丑数 问题描述 丑数是其质因子只可能是2,3或5的数.前10个丑数分别为1, 2, 3, 4, 5, 6, 8, 9, 10, 12.输入一个正整数n,求第n个丑数. 输入格式 每行为一个 ...

  4. shell习题训练

    shell习题训练 求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2 ...

  5. Python语言100例

    Python版本:python 3.2.2 电脑系统:win7旗舰 实例来源:python菜鸟教程100例 #!/usr/bin/python # -*- coding: UTF-8 -*- impo ...

  6. C语言经典算法100例

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. ...

  7. 实用ExtJS教程100例-001:开天辟地的Hello World

    ExtJS功能繁多,要想彻底的了解确实很困难.作为初学者,如何能找到一条快速的通道呢?我觉得,如果你有Javascript的基础,那就不要惧怕ExtJS的复杂,从动手开始,遇到问题,解决问题,积累经验 ...

  8. C 语言经典100例

    C 语言经典100例 C 语言练习实例1 C 语言练习实例2 C 语言练习实例3 C 语言练习实例4 C 语言练习实例5 C 语言练习实例6 C 语言练习实例7 C 语言练习实例8 C 语言练习实例9 ...

  9. Xena L23网络测试仪Valkyrie使用技巧100例,目录

    Xena L23网络测试仪Valkyrie使用技巧100例,先写个目录 100例,会不会有点多,写不完... ^_^ 第1次编辑于2019-11-27 22:05:52, Evan YE. 编号 标题 ...

随机推荐

  1. Jquery解析Json格式数据

    今天稍微学习了一下Json,JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. JSON采用完全独立于语言的 ...

  2. 【BZOJ2662】【BeiJing wc2012】冻结 分层图 裸的!

    我都不好意思发题解了,看这篇博吧.(飞行路线的,基本一样) http://blog.csdn.net/vmurder/article/details/40075989 同学做了好久.我害怕题里有坑,又 ...

  3. kafka基本原理概述——patition与replication分配

    kafka一直在大数据中承受着数据的压力也扮演着对数据维护转换的角色,下面重点介绍kafka大致组成及其partition副本的分配原则: 文章参考:http://www.linkedkeeper.c ...

  4. 【UI设计】扁平化设计之流行色值

    收集了一些颜色值 顏色表示方法: 以命名方式定义经常使用的顏色,如color="green".可是自由度较低,何况单词量...... 以RGB值表示.如#FF0000表示red(红 ...

  5. 【Nodejs】“快算24”扑克牌游戏算法 1.02

    快算24是一种挺好的锻炼孩子算数能力的扑克牌游戏,它的游戏方式是把四张牌的牌面数值通过有限四则运算得到结果24,四张牌必须仅用一次.各地玩法还有点差别,有的只算1-10,其它抽出来:有的地方把整幅牌都 ...

  6. Linux - 系统路径加入命令

    系统路径加入命令 本文地址: http://blog.csdn.net/caroline_wendy 1. 在根文件夹建立bin文件夹: $mkdir bin 2. 改动".bash_pro ...

  7. hdu1027 Ignatius and the Princess II (全排列 &amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  8. 使用GCD创建单例

    + (KKTextHUB *)sharedTextHUB { static KKTextHUB *sharedHub = nil; static dispatch_once_t onceToken; ...

  9. Uubntu14.04下 Hadoop集群环境搭建

    1机器信息 master192.168.3.10 hadoop/ hadoop slave1192.168.3.11 hadoop/ hadoop slave2192.168.3.12 hadoop/ ...

  10. PHP $_POST

    $_POST 变量用于收集来自 method="post" 的表单中的值. $_POST 变量 $_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值. ...