您的位置:首页 > 运维架构 > Shell

SHELL练习题

2020-07-14 06:32 537 查看

SHELL练习题

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


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

vim test2.sh
#!/bin/bash
./F1.txt | cat > F2.txt
vim F1.txt
echo "F1"
vim F2.txt
echo "F2"
chmod 755 test2.sh F1.txt F2.txt
bash test2.sh
vim F2.txt


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

vim test3.sh
#!/bin/bash
for ((n=1;n<=254;n++));
do
ping -c 3 192.168.1.$n &> /dev/null
if [ $? -eq 0 ];then
echo "$n online"
fi
done

4、根据以下信息:
IP_Address MAC_Address Interface Static
10.66.10.250 80:71:7A:33:CA:A7 br on
10.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:br
10.66.10.249:5C:50:15:7F:3B:F5:br

awk 'NR!=1{ofs=":";print $1,$2,$3}' text.txt

5、在shell中变量的赋值有四种方法,其中采用name=oupeng.com的方法称:
直接赋值 使用read命令 使用命令行传参 使用命令输出

#!/bin/bash
read -p "name=" q
name=$q
echo $name

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 ssh2
Myy 4 03:43:07 tz-monitor sshd{14082}: invalid user postgres from 124.232.135.84

#!/bin/bash
awk '/Failed password/{count[$(NF-3)]++}END{for (ip in count) if(count[ip]>=10){print count[ip],ip}}' /var/log/secure > /tmp/count_ip.txt
while read line
do
IP=$(echo $line |awk '{print $2}')
if [ "$IP" != "192.168.100.100" ];then
if ! grep -w $IP /tmp/drop_ip.txt &> /dev/null;then
iptables -I INPUT -s $IP -j DROP
echo $IP >> /tmp/drop_ip.txt
fi
fi
done < /tmp/count_ip.txt

*7、检查IP地址合规,请用shell编写代码,列出不以199或200开头的IP地址,如199.x.x.x 或200.x.x.x
Interface Physical Protocol IP Adderss
Eth1/0/1 up up 199.11.250.1
Eth1/0/2 up up 200.11.250.5
Loop0 up up(s) 199.11.250.1
Vlan1 down down unassigned
Vlan500 down down 139.100.1.157
Vlan900 up up 140.11.250.41

#!/bin/bash
while read line
do
isnum=$(echo $line | awk -F "[ .]+" '{print $(NF-3)}')
if [[ $isnum =~ ^[0-9]+$ ]];then
if [ $isnum -ne 199 ] && [ $isnum -ne 200 ];then
echo $line | awk  '{print $NF}'
fi
fi
done < /tmp/config.txt

**8、处理以下文件内容,将域名提取并进行计数排序,如处理:
http://www.baidu.com/index.html
http://www.baidu.com/1.html
http://post.baidu.com/index.html
http://mp3.baidu.com/index.html
http://www.baidu.com/3.html
http://post.baidu.com/2.html

得到如下结果:
域名的出现次数 域名
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com**

awk -F '/' '{count[$3]++}END{for (url in count) print count[url],url}' url.txt |sort -rn

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.gz
ftp -i -n <<FTPIT
open 123.234.25.130
user username_xxx password_xxx
bin
passive
hash
cd /home/bak
lcd /tmp
put all.tar.gz
quit
FTPIT

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

#!/bin/bash
while read line
do
rm $line -f
done < /root/file.list
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: