1、面试题:使用for循环在/tmp目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串template,示例如下

aaesdffbnv_template.html

方法1:

cd /tmp
for ((i=;i<;i++));do touch `echo $RANDOM | md5sum | sed 's/[^a-z]//g' | cut -c -`_template.html;done

方法2:

cd /tmp

for i in `seq `;do touch `openssl rand -base64  | sed 's/[^a-z]//g' | cut -c -`_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{..} | 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{..} | xargs -n1 useradd;echo template{..}:`cat /dev/urandom|tr -dc -|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 `
do
{
ping -c -W 192.168..$i &>/dev/null
if [ $? -eq ];then
echo "192.168.8.$i is alive" ?
fi ?
}& #shell的并发检测功能,批量ping,快速返回结果
done

方法2:使用namp(需要安装namp)

nmap -sP 192.168.8.0/ | 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 -- :
file=$
log_file=/tmp/tmp.log JudgeExt(){
if expr "$1" : ".*\.log" &> /dev/null;then
:
else
echo "Usage: $0 xxx.log"
exit
fi
} IpCount(){ grep "ESTABLISHED" $ | gawk -F "[ :]+" '{++S[$(NF-3)]} END {for (key in S) print S[key],key}' | sort -rn -k1 | head - > $log_file } ipt(){
local ip=$
if [ `iptabls -L -n | grep "$ip" | wc -l` -lt ];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 ];then
ipt $ip
fi
done < $log_file
sleep 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

7、面试题:请用脚本实现Mysql数据库分库分表备份

#!/bin/bash
PATH="/application/mysql/bin:$PATH"
DBPATH=/server/backup
MYUSER=root
MYPASS=
SOCKET=/data//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=$
b=$ Usage(){ echo "Usage:$0 NUM1 NUM2"
exit
} [ $# -ne ] && { ##判断传参个数
Usage
} expr $a + &> /dev/null #使用expr只能计算两个整数的原理,判断传入的参数是否为整数
RETVAL_A=$?
expr $b + &> /dev/null
RETVAL_B=$? [ $RETVAL_A -ne -o $RETVAL_B -ne ] && { echo "you must input integer "
exit
} if [ "$a" -lt "$b" ];then
echo "$a < $b"
elif [ "$a" -gt "$b" ];then
echo "$a > $b"
else
echo "$a = $b"
fi
exit

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 ""
;;
esac
done

10、Web及Mysql服务异常监测

网站监测:

#!/bin/bash
#Author Template
#Time -- :
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=;i<;i++))
do
echo -n "." ;sleep
done
echo
} function check_url(){
wait
for ((i=;i<`echo ${#url_list[*]}`;i++))
do
wget -o /dev/null -T --tries= --spider ${url_list[$i]} &> /dev/null
if [ $? -eq ];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
done }
main

Mysql监测:

方法1:

#!/bin/bash
if [ "`netstat -tlunp | grep 3306 | awk -F "[ :]+" '{print $4}'`" = "" ];then
echo "Mysql is Running"
else
echo "Mysql is Stopped"
/etc/init.d/mysqld start
fi

方法2:

#!/bin/bash
if [ `netstat -tlunp | grep "" | wc -l` -gt ];then
echo "Mysql is Running"
else
echo "Mysql is Stopped"
/etc/init.d/mysqld start
fi

方法3:

#!/bin/bash
if [ `lsof -i tcp: | wc -l` -gt ];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:
#description Rsyncd start stop script . /etc/init.d/functions function Usage(){
echo "Usage:$0 {start | stop | restart}"
exit
} function start (){
rsync --daemon
sleep if [ `netstat -tlunp | grep rsync | wc -l` -ge ];then
action "rsyncd started" /bin/true
else
action "rsyncd started" /bin/false
exit
fi
} function stop(){
killall rsync &> /dev/null
sleep
if [ `netstat -tlunp | grep rsync|wc -l` -eq ];then
action "rsyncd stopped" /bin/true
else
action "rsyncd stopped" /bin/false
exit
fi
} function restart(){
stop && start
} case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
*)
Usage
esac

12、面试题:请写出mysql多实例启动脚本

#!/bin/bash
port=
mysql_user="root"
mysql_pwd=""
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
. /etc/init.d/functions start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf >& > /dev/null &
[ $? -eq ] && action "Mysql start" /bin/true || action "Mysql start" /bin/false
else
printf "MySQL is running...\n"
exit
fi
} stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
[ $? -eq ] && action "Stop mysql" /bin/true || action "Stop mysql" /bin/false
fi
} restart(){
printf "Restarting MySQL...\n"
stop
sleep
start
} Usage(){
echo "Usage: /data/${port}/mysql (start|stop|restart)"
exit
} case "$1" in start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
Usage
;;
esac
05-11 20:07