1.使用for循环在/opt/test目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串test,名称示例下:apquvdpqbk_test.html
#!/bin/bash
reserve='a-z'
unset -v random
if [ ! -d /opt/test ];then
mkdir /opt/test
fi
count=0
for i in {1..10}
do
random=$( cat /dev/urandom| tr -dc $reserve |head -c 10)
touch /opt/test/${random}_test.html
done
2.将以上面试题1中结果文件名中的test字符串全部改成test2(最好用for循环实现),并且将扩展名html全部改成大写。
#!/bin/bash
cd /opt/test
file=$(find ./ -name "*test*")
for i in $file
do
echo $i
newfile=$(echo $i |sed "s/test/test1/g" | sed "s/html/HTML/g")
echo $newfile
mv $i $newfile
done
3.批量创建10个系统帐号test01-test10并设置密码(密码为随机数,要求字符和数字等混合)
#!/bin/bash
a=(01 02 03 04 05 06 07 08 09 10)
unset -v password
for i in {0..9}
do
useradd test${a[$i]}
password=$( cat /dev/urandom| tr -dc 0-9-a-z-A-Z |head -c 10)
echo $password |passwd test${a[$i]} --stdin
done
echo test{01..10} | xargs -n1 useradd | echo $(echo $RANDOM|md5sum|cut -c 1-8) |xargs -n1 passwd --stdin
4.写一个Shell脚本,判断10.0.0.0/24网络里,当前在线的IP有哪些?
#!/bin/bash
for i in {2..254}
do
ping -c 2 10.0.0.$i >/dev/null
if [ $? -eq 0 ];then
echo 10.0.0.$i
fi
done
5.写一个Shell脚本解决DOS攻击生产案例。
请根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100(读者根据实际情况设定),即调用防火墙命令封掉对应的IP。防火墙命令为:iptables-I INPUT -s IP地址 -j DROP。
#!/bin/bash
file=/apps/project/nginx/logs/access_2017-09-01.log
#command=$(cat $file | awk '{++S[$1]} END {for(i in S)print S[i],i}' |sort -rn| head -n 100)
command=$(cat $file |awk '{print $1}' |sort | uniq -c | sort -n -k 1 -r | head -n 100)
echo $command > access_ip.txt
iplist=$(cat acceess_ip.txt |awk '{if($1>100) print $2}')
for ip in $iplist
do
iptables -I INPUT -s $ip -j DROP
echo "$ip is drop!"
done
#!/bin/bash
command=$(netstat -ntu |awk '{print $5}' |cut -d ":" -f 1 | sort | uniq -c |sort -rn )
echo $command > access_ip.txt
iplist=$(cat acceess_ip.txt |awk '{if($1>100) print $2}')
for ip in $iplist
do
iptables -I INPUT -s $ip -j DROP
echo "$ip is drop!"
done
6.MySQL数据库分库分表备份
#!/bin/bash
user=root
password=123456
socket=/var/lib/mysql.sock
login=$(mysql -u$user -p$password -S $socket)
database=$($login -e "show databases"|egrep -iv "date|_schema|mysql")
for dbname in $database
do
backup_dir=/apps/backup/
[ ! -d $backup_dir ] && mkdir -p $backup_dir
mysqldump -u$user -p$password -S $socket -B $dbname |gzip > $backup_dir/$dbname_${date +%F}.sql.gz
done
http://www.xuliangwei.com/xubusi/252.html
7.利用bash for循环打印下面这句话中字母数不大于6的单词(某企业面试真题)。
I am oldboy teacher welcome to oldboy trainingclass
#!/bin/bash
sentence=$(cat test.txt)
word=$(echo $sentence |tr " " "\n")
for i in $word
do
count=$(echo $i |wc -L )
if [ $count -le 6 ];then
echo $i
fi
done
网友评论