shell脚本

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

[root@bogon ~]# find /var/log -type f |wc -l
58

2、如何将F1.txt文件的运行结果输出到F2.txt里?
# touch F1.txt          创建文件
# chmod 755 F1.txt      设置权限
#  ./F1.txt > F2.txt    运行脚本
3、写一个脚本实现判断192.168.1.0/24 网络里,当前在线的ip有哪些,能ping通则认为在哪
# vim b.sh 

在这里插入图片描述

# chmod 755 b.sh     设置权限
# sh b.sh            运行脚本
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}' test.txt

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

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
# vim /server/scripts/fairban.sh

#!/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
# 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值