生产环境中有哪些常用的shell脚本
本篇内容主要讲解“生产环境中有哪些常用的shell脚本”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“生产环境中有哪些常用的shell脚本”吧!
#!/bin/bashif[$#-ne1];thenecho"Usage:$0filename"fidir=$(dirname$1)file=$(basename$1)ftp-n-v#-n自动登录open192.168.1.10#ftp服务器useradminpasswordbinary#设置ftp传输模式为二进制,避免MD5值不同或.tar.gz压缩包格式错误cd$dirget"$file"EOF2、连续输入5个100以内的数字,统计和、最小和最大
#!/bin/bashCOUNT=1SUM=0MIN=0MAX=100while[$COUNT-le5];doread-p"请输入1-10个整数:"INTif[[!$INT=~^[0-9]+$]];thenecho"输入必须是整数!"exit1elif[[$INT-gt100]];thenecho"输入必须是100以内!"exit1fiSUM=$(($SUM+$INT))[$MIN-lt$INT]&&MIN=$INT[$MAX-gt$INT]&&MAX=$INTletCOUNT++doneecho"SUM:$SUM"echo"MIN:$MIN"echo"MAX:$MAX"3、将结果分别赋值给变量
应用场景:希望将执行结果或者位置参数赋值给变量,以便后续使用。方法1:foriin$(echo"456");doevala$i=$idoneecho$a4$a5$a6方法2:将位置参数192.168.1.1{1,2}拆分为到每个变量num=0foriin$(evalecho$*);do#eval将{1,2}分解为12letnum+=1evalnode${num}="$i"doneecho$node1$node2$node3#basha.sh192.168.1.1{1,2}192.168.1.11192.168.1.12方法3:arr=(456)INDEX1=$(echo${arr[0]})INDEX2=$(echo${arr[1]})INDEX3=$(echo${arr[2]})4、批量修改文件名
示例:#toucharticle_{1..3}.html#lsarticle_1.htmlarticle_2.htmlarticle_3.html目的:把article改为bbs方法1:forfilein$(ls*html);domv$filebbs_${file#*_}#mv$file$(echo$file|sed-r's/.*(_.*)/bbs\1/')#mv$file$(echo$file|echobbs_$(cut-d_-f2)done方法2:forfilein$(find.-maxdepth1-name"*html");domv$filebbs_${file#*_}done方法3:#renamearticlebbs*.html5、统计当前目录中以.html结尾的文件总大
方法1:#find.-name"*.html"-execdu-k{}\;|awk'{sum+=$1}END{printsum}'方法2:forsizein$(ls-l*.html|awk'{print$5}');dosum=$(($sum+$size))doneecho$sum6、扫描主机端口状态
#!/bin/bashHOST=$1PORT="2225808080"forPORTin$PORT;doifecho&>/dev/null>/dev/tcp/$HOST/$PORT;thenecho"$PORTopen"elseecho"$PORTclose"fidone7、Expect实现SSH免交互执行命令
Expect是一个自动交互式应用程序的工具,如telnet,ftp,passwd等。需先安装expect软件包。方法1:EOF标准输出作为expect标准输入#!/bin/bashUSER=rootPASS=123.comIP=192.168.1.120expectsettimeout30spawnssh$USER@$IPexpect{"(yes/no)"{send"yes\r";exp_continue}"password:"{send"$PASS\r"}}expect"$USER@*"{send"$1\r"}expect"$USER@*"{send"exit\r"}expecteofEOF方法2:#!/bin/bashUSER=rootPASS=123.comIP=192.168.1.120expect-c"spawnssh$USER@$IPexpect{\"(yes/no)\"{send\"yes\r\";exp_continue}\"password:\"{send\"$PASS\r\";exp_continue}\"$USER@*\"{send\"df-h\rexit\r\";exp_continue}}"方法3:将expect脚本独立出来登录脚本:#catlogin.exp#!/usr/bin/expectsetip[lindex$argv0]setuser[lindex$argv1]setpasswd[lindex$argv2]setcmd[lindex$argv3]if{$argc!=4}{puts"Usage:expectlogin.expipuserpasswd"exit1}settimeout30spawnssh$user@$ipexpect{"(yes/no)"{send"yes\r";exp_continue}"password:"{send"$passwd\r"}}expect"$user@*"{send"$cmd\r"}expect"$user@*"{send"exit\r"}expecteof执行命令脚本:写个循环可以批量操作多台服务器#!/bin/bashHOST_INFO=user_info.txtforipin$(awk'{print$1}'$HOST_INFO)douser=$(awk-vI="$ip"'I==$1{print$2}'$HOST_INFO)pass=$(awk-vI="$ip"'I==$1{print$3}'$HOST_INFO)expectlogin.exp$ip$user$pass$1doneLinux主机SSH连接信息:#catuser_info.txt192.168.1.120root1234568、批量修改服务器用户密码
Linux主机SSH连接信息:旧密码#catold_pass.txt192.168.18.217root12345622192.168.18.218root12345622内容格式:IPUserPasswordPortSSH远程修改密码脚本:新密码随机生成#!/bin/bashOLD_INFO=old_pass.txtNEW_INFO=new_pass.txtforIPin$(awk'/^[^#]/{print$1}'$OLD_INFO);doUSER=$(awk-vI=$IP'I==$1{print$2}'$OLD_INFO)PASS=$(awk-vI=$IP'I==$1{print$3}'$OLD_INFO)PORT=$(awk-vI=$IP'I==$1{print$4}'$OLD_INFO)NEW_PASS=$(mkpasswd-l8)#随机密码echo"$IP$USER$NEW_PASS$PORT">>$NEW_INFOexpect-c"spawnssh-p$PORT$USER@$IPsettimeout2expect{\"(yes/no)\"{send\"yes\r\";exp_continue}\"password:\"{send\"$PASS\r\";exp_continue}\"$USER@*\"{send\"echo\'$NEW_PASS\'|passwd--stdin$USER\rexit\r\";exp_continue}}"done生成新密码文件:#catnew_pass.txt192.168.18.217rootn8wX3mU%22192.168.18.218rootc87;ZnnL229、打印乘法口诀
方法1:#awk'BEGIN{for(n=0;n++方法2:for((i=1;idofor((j=1;jdoresult=$(($i*$j))echo-n"$j*$i=$result"doneechodone10、getopts工具完善脚本命令行参数
getopts是一个解析脚本选项参数的工具。命令格式:getoptsoptstringname[arg]初次使用你要注意这几点:脚本位置参数会与optstring中的单个字母逐个匹配,如果匹配到就赋值给name,否则赋值name为问号;optstring中单个字母是一个选项,如果字母后面加冒号,表示该选项后面带参数,参数值并会赋值给OPTARG变量;optstring中第一个是冒号,表示屏蔽系统错误(test.sh:illegaloption--h);允许把选项放一起,例如-ab下面写一个打印文件指定行的简单例子,引导你思路:#!/bin/bashwhilegetopts:f:n:option;docase$optioninf)FILE=$OPTARG[!-f$FILE]&&echo"$FILEFilenotexist!"&&exit;;n)sed-n"${OPTARG}p"$FILE;;?)echo"Usage:$0-f-n"echo"-f,--filespecifiedfile"echo"-n,--line-numberprintspecifiedline"exit1;;esacdone
到此,相信大家对“生产环境中有哪些常用的shell脚本”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。