1、面试题:使用for循环在/tmp目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串template,示例如下
aaesdffbnv_template.html
方法1:
cd /tmpfor ((i=0;i<10;i++));do touch `echo $RANDOM | md5sum | sed ‘s/[^a-z]//g‘ | cut -c 1-10`_template.html;done
方法2:
cd /tmp for i in `seq 10`;do touch `openssl rand -base64 40 | sed ‘s/[^a-z]//g‘ | cut -c 1-10`_template.html;done
2、面试题:批量改名 将以上所得的文件名中的template全部改成leon,并且将html改成大写
方法1:
rename template.html leon.HTML *.html
方法2:
ls *.html | awk -F ‘_‘ ‘{print "mv "$0" "$1"_leon.HTML"}‘ | bash
3、面试题:批量10个系统账号template01到template10并设置密码(密码为随机数)
方法1:
for i in `seq -w 01 10`;do useradd -M template$i;echo $RANDOM |md5sum | cut -c 1-10 | tee -a passwd.log | template$i;done
方法2:
echo template{01..10} | tr " " "\n" | sed -r ‘s#(.*)#useradd \1;pass=`echo $RANDOM | md5sum | cut -c 1-10`;echo "$pass"|passwd --stdin \1;echo -e "\1\t`echo "$pass"`" >> /tmp/passwd.log#g‘ | bash
方法3:
echo template{01..10} | xargs -n1 useradd;echo template{01..10}:`cat /dev/urandom|tr -dc 0-9|fold -w8|head -1` | xargs -n1|tee -a pass.txt|chpasswd
4、面试题:写一个脚本,判断192.168.8.0/24网路里,当前在线的IP有哪些
方法1:
#!/bin/bash G for i in `seq 254` do { ping -c 2 -W 2 192.168.8.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.8.$i is alive" ? fi ? }& #shell的并发检测功能,批量ping,快速返回结果 done
方法2:使用namp(需要安装namp)
nmap -sP 192.168.8.0/24 | awk ‘/Nmap scan report for/{print $NF}‘ #1到2秒返回结果
5、面试题:写一个shell脚本以解决DDOS攻击生产的问题
请根据Web日志或网络连接数,监控当某个IP并发连接数或短时间内PV达到100时,即调用防火墙命令封掉该对应的IP:防火墙的命令为:
iptables -I INPUT -s IP -j DROP
参考此脚本:
#!/bin/bash #Author Template #Time 2018-07-02 22:06 file=$1 log_file=/tmp/tmp.log JudgeExt(){ if expr "$1" : ".*\.log" &> /dev/null;then : else echo "Usage: $0 xxx.log" exit 1 fi } IpCount(){ grep "ESTABLISHED" $1 | gawk -F "[ :]+" ‘{++S[$(NF-3)]} END {for (key in S) print S[key],key}‘ | sort -rn -k1 | head -5 > $log_file } ipt(){ local ip=$1 if [ `iptabls -L -n | grep "$ip" | wc -l` -lt 1 ];then iptabls -I INPUT -s $ip -j DROP echo "$line is dorpped" >> /tmp/drop_list_$(date +%F).log fi } main(){ JudgeExt $file while true do IpCount $file while read line do ip=`echo $line | gawk ‘{print $2}‘` count=`echo $line | gawk ‘{print $1}‘` if [ $count -gt 500 ];then ipt $ip fi done < $log_file sleep 180 done } main
6、面试题:请用脚本实现Mysql数据库分库备份
#!/bin/bash echo -e [`date +"%Y-%m-%d %H:%M:%S"`] start #system time time=`date +"%y-%m-%d"` #host IP host="127.0.0.1" #database backup user user="root" #database password passwd="yourpasswd" #Create a backup directory mkdir -p /backup/db/"$time" #list database name all_database=`/usr/bin/mysql -u$user -p$passwd -Bse ‘show databases‘` #in the table from the database backup for i in $all_database do /usr/bin/mysqldump -u$user -p$passwd $i > /backup/db/"$time"/"$i"_"$time".sql done echo -e [`date +"%Y-%m-%d %H:%M:%S"`] end exit 0
7、面试题:请用脚本实现Mysql数据库分库分表备份
#!/bin/bash PATH="/application/mysql/bin:$PATH" DBPATH=/server/backup MYUSER=root MYPASS=123456 SOCKET=/data/3306/mysql.sock MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET" MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET" [ ! -d "$DBPATH" ] && mkdir $DBPATH for dbname in `$MYCMD -e "show database;" | sed ‘1,2d‘ | egrep -v "mysql|schema"` do mkdir $DBPATH/${dbname}_$(date +%F) -p for table in `$MYCMD -e "show tables from $dbname;"| sed ‘1d‘` do $MYDUMP $dbname $table | gzip > $DBPATH/${dbname}_$(date +%F)/${dbname}_${table}.sql.gz done done
8、面试题:请开发一个shell脚本,比较两个整数的大小,通过传参的方式
#!/bin/bash a=$1 b=$2 Usage(){ echo "Usage:$0 NUM1 NUM2" exit 2 } [ $# -ne 2 ] && { ##判断传参个数 Usage } expr $a + 1 &> /dev/null #使用expr只能计算两个整数的原理,判断传入的参数是否为整数 RETVAL_A=$? expr $b + 1 &> /dev/null RETVAL_B=$? [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ] && { echo "you must input integer " exit 1 } if [ "$a" -lt "$b" ];then echo "$a < $b" elif [ "$a" -gt "$b" ];then echo "$a > $b" else echo "$a = $b" fi exit 0
未完待续。。。。
原文地址:https://www.cnblogs.com/Template/p/9279052.html
时间: 2024-11-06 16:20:29