1、面试题:使用for循环在/tmp目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串template,示例如下
aaesdffbnv_template.html
方法1:
cd /tmp
for ((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 " " " " | sed -r 's#(.*)#useradd 1;pass=`echo $RANDOM | md5sum | cut -c 1-10`;echo "$pass"|passwd --stdin 1;echo -e "1 `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
9、面试题:打印选择菜单,按照选择一键安装不同的web服务
#!/bin/bash PS3="Enter option: " #设置提示符 stty erase ^h #设置退格删除键为backspace trap "echo Goodbye..." EXIT #检测到exit信号,输出Goodbye select option in "install LNMP" "install LANP" "Exit" do case $option in "install LNMP") sh install_lnmp.sh ;; "install LANP") sh install_lamp.sh ;; "Exit") exit ;; *) echo "123" ;; esac done
10、Web及Mysql服务异常监测
网站监测:
#!/bin/bash #Author Template #Time 2018-07-01 16:00 url_list=( http://www.cnblogs.com/Template/ http://www.baidu.com http://127.0.0.1 ) . /etc/init.d/functions function wait(){ echo -n "3 秒后执行检查." for ((i=0;i<3;i++)) do echo -n "." ;sleep 1 done echo } function check_url(){ wait for ((i=0;i<`echo ${#url_list[*]}`;i++)) do wget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]} &> /dev/null if [ $? -eq 0 ];then action "${url_list[$i]}" /bin/true else action "${url_list[$i]}" /bin/false fi ((check_count++)) done } main(){ while true do check_url echo "------------check count:${check_count}------------------" sleep 10 done } main
Mysql监测:
方法1:
#!/bin/bash if [ "`netstat -tlunp | grep 3306 | awk -F "[ :]+" '{print $4}'`" = "3306" ];then echo "Mysql is Running" else echo "Mysql is Stopped" /etc/init.d/mysqld start fi
方法2:
#!/bin/bash if [ `netstat -tlunp | grep "3306" | wc -l` -gt 0 ];then echo "Mysql is Running" else echo "Mysql is Stopped" /etc/init.d/mysqld start fi
方法3:
#!/bin/bash if [ `lsof -i tcp:3306 | wc -l` -gt 0 ];then echo "Mysql is Running" else echo "Mysql is Stopped" /etc/init.d/mysqld start fi
11、面试题:写出网络服务独立进程模式下Rsync的系统启动脚本,例如:/etc/init.d/rsyncd {start|stop|restart}
#!/bin/bash # chkconfig: 2345 20 80 #description Rsyncd start stop script . /etc/init.d/functions function Usage(){ echo "Usage:$0 {start | stop | restart}" exit 1 } function start (){ rsync --daemon sleep 1 if [ `netstat -tlunp | grep rsync | wc -l` -ge 1 ];then action "rsyncd started" /bin/true else action "rsyncd started" /bin/false exit 1 fi } function stop(){ killall rsync &> /dev/null sleep 1 if [ `netstat -tlunp | grep rsync|wc -l` -eq 0 ];then action "rsyncd stopped" /bin/true else action "rsyncd stopped" /bin/false exit 1 fi } function restart(){ stop && start } case "$1" in "start") start ;; "stop") stop ;; "restart") restart ;; *) Usage esac
12、面试题:请写出mysql多实例启动脚本
#!/bin/bash port=3306 mysql_user="root" mysql_pwd="123456" CmdPath="/application/mysql/bin" mysql_sock="/data/${port}/mysql.sock" . /etc/init.d/functions start(){ if [ ! -e "$mysql_sock" ];then printf "Starting MySQL... " /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null & [ $? -eq 0 ] && action "Mysql start" /bin/true || action "Mysql start" /bin/false else printf "MySQL is running... " exit 1 fi } stop(){ if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped... " exit 1 else printf "Stoping MySQL... " ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown [ $? -eq 0 ] && action "Stop mysql" /bin/true || action "Stop mysql" /bin/false fi } restart(){ printf "Restarting MySQL... " stop sleep 2 start } Usage(){ echo "Usage: /data/${port}/mysql (start|stop|restart)" exit 1 } case "$1" in start) start ;; stop) stop ;; restart) restart ;; *) Usage ;; esac