AI智能
改变未来

shell脚本习题练习


1、 统计/var/log下文件的个数。

//find /var/log -type f | wc -l;

2、如何将F1.txt文件的运行结果输出到F2.txt里?

// ./F1 > F2.txt   shF1 > F2.txt;

3、写一个脚本实现判断192.168.1.0/24 网络里,当前在线的ip有哪些,能ping通则认为在线

// #!/bin/shCMD=\"ping -W 2 -c 2\"Ip=\"192.168.1.\"for n in $(seq 254)do{$CMD $Ip$n &> /dev/nullif [ $? -eq 0 ];thenecho \"$Ip$n 在线\"fi}&donewait;

4、根据以下信息:

IP_Address		MAC_Address 		Interface 	Static10.66.10.250 	80:71:7A:33:CA:A7 	br 			on10.66.10.249 	5C:50:15:7F:3B:F5 	br 			on将以上文件名称test.txt文件中IP_Address,MAC_Address, Interface三项下的内容取出来,值以“:”分割,并呈格式显示出来。注:10.66.10.250:80:71:7A:33:CA:A7:br10.66.10.249:5C:50:15:7F:3B:F5:br
// awk \'NR!=1{OFS=\":\";print $1,$2,$3}\' test.txt;

5、在shell中变量的赋值有四种方法,其中采用name=oupeng.com的方法称:(直接赋值)

// 直接赋值 使用read命令 使用命令行传参 使用命令输出;

6、编写一个脚本,5分钟检查一次日志,发现有暴力SSH破解现象的,提取此类IP地址,并去重,并按降序排序。

要求:同一个IP暴力破解超过10次,自动屏蔽IP地址,指定办公室IP地址(192.168.100.100)为可信任IP地址,不受屏蔽规则限制,以下为日志格式:日志样式:May 4 03:43:07 tz-monitor sshd{14003}: Failed password for root from 124.232.135.84 port 25251 ssh2Myy 4 03:43:07 tz-monitor sshd{14082}: invalid user postgres from 124.232.135.84
// #!/bin/bashawk \'/Failed password/{count[$(NF-3)]++}END{for (ip in count) if(count[ip]>=10){print count[ip],ip}}\' /var/log/secure > /tmp/count_ip.txtwhile read linedoIP=$(echo $line |awk \'{print $2}\')if [ \"$IP\" != \"192.168.100.100\" ];thenif ! grep -w $IP /tmp/drop_ip.txt &> /dev/null;theniptables -I INPUT -s $IP -j DROPecho $IP >> /tmp/drop_ip.txtfifidone < /tmp/count_ip.txt;

7、检查IP地址合规,请用shell编写代码,列出不以199或200开头的IP地址,如199.x.x.x 或200.x.x.x

Interface Physical Protocol IP AdderssEth1/0/1 up up 199.11.250.1Eth1/0/2 up up 200.11.250.5Loop0 up up(s) 199.11.250.1Vlan1 *down down unassignedVlan500 down down 139.100.1.157Vlan900 up up 140.11.250.41
// #!/bin/bashwhile read linedoisnum=$(echo $line | awk -F \"[ .]+\" \'{print $(NF-3)}\')if [[ $isnum =~ ^[0-9]+$ ]];thenif [ $isnum -ne 199 ] && [ $isnum -ne 200 ];thenecho $line | awk  \'{print $NF}\'fifidone < /tmp/config.txt;

8、处理以下文件内容,将域名提取并进行计数排序,如处理:

http://www.baidu.com/index.htmlhttp://www.baidu.com/1.htmlhttp://post.baidu.com/index.htmlhttp://mp3.baidu.com/index.htmlhttp://www.baidu.com/3.htmlhttp://post.baidu.com/2.html得到如下结果:域名的出现次数 域名3 www.baidu.com2 post.baidu.com1 mp3.baidu.com
// An highlighted blockvar foo = \'bar\';

9、在单台服务器Linux操作系统环境下,写一行命令,将所有该机器的所有以“.log.bak“为后缀的文件,打包压缩并上传到ftp上,FTP地址为123.234.25.130的/home/bak文件夹

//  cd / find -type f -name \"*.log.bak\" |xargs tar zcf /tmp/all.tar.gzftp -i -n <<FTPITopen 123.234.25.130user username_xxx password_xxxbinpassivehashcd /home/baklcd /tmpput all.tar.gzquitFTPIT;

10、Linux脚本:现在要删除本机中若干文件,/root/file.list中记录了这些文件的绝对路径,请用脚本实现。/root/file.list内容范例:/tmp/1.file

// #!/bin/bashwhile read linedorm $line -fdone < /root/file.list;
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » shell脚本习题练习