1、批量创建自带随机字符串的文件多种方法
本次运用到了$RANDOM变量
创建命令如下:
echo $RANDOM|md5sum|tr "[0-9]" "[a-z]"|cut -c 3-8
通过echo输出随机数,然后通过md5加密,再将数字转换为字母,然后取其中的10个
具体脚本实现如下:
#!/bin/bash #echo $RANDOM|md5sum|tr "[0-9]" "[a-z]"|cut -c 3-8 [ -d "/oldboy" ]|| mkdir -p /oldboy for n in `seq 10` do touch /oldboy/$(echo $RANDOM|md5sum|tr "[0-9]" "[a-z]"|cut -c 3-8)_oldboy.html done
运行脚本:
[root@Python test001]# sh oldboy01.sh
查看结果:
[root@Python test001]# ls -rt /oldboy/ gdcffb_oldboy.html fghgca_oldboy.html jffffh_oldboy.html dhbdec_oldboy.html fgjehc_oldboy.html ecfbca_oldboy.html gigcfe_oldboy.html deabbf_oldboy.html jbfbia_oldboy.html chacif_oldboy.html [root@Python test001]#
注释:获取随机字母:
[root@Python test001]# openssl rand -base64 40|sed 's#[^a-z]##g' acgilbtgjwpkejryjybkg
所以以上的脚本可以改写为如下形式:
#!/bin/bash Path=/oldboy [ -d "$Path" ]|| mkdir -p /oldboy for n in `seq 10` do random=$( openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 2-11) touch /oldboy/${random}_oldboy.html done
运行脚本:
[root@Python test001]# ll -rth /oldboy/ total 0 -rw-r--r-- 1 root root 0 Jan 22 09:58 uxodkflbvk_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 hegdftlpsg_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 dztkcvtris_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 uthjkiobcr_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 efzdhgafvp_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 ygxdfyqcyq_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 pxqaxozaim_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 ybbnmsuinc_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 ebvpjhaith_oldboy.html -rw-r--r-- 1 root root 0 Jan 22 09:58 lzxgyzqrbd_oldboy.html
2、批量修改文件名多种方法讲解
本次涉及到了命令的拼接,运用for循环取出文件,然后通过awk以"_"进行分割,取出固定的后缀(print $1),然后运行mv命令将源文件$n改名为$name${Filename}
#!/bin/bash Filename=_.oldgirl.HTML Dirname="/oldboy" cd $Dirname for n in `ls /oldboy` do name=$(ls /oldboy/${n}|awk -F:'_' '{print $1}') mv $n ${name}${Filename} done
[root@Python test001]#
或者
#!/bin/bash Filename=_oldgirl.HTML Dirname="/oldboy" cd $Dirname for n in `ls` do name=$(echo ${n}|awk -F '_' '{print $1}') mv $n ${name}{Filename} done
对以上脚本还是运用awk进行改写
#!/bin/bash Path="/oldboy" cd $Path && \ ls $Path|xargs -n1|awk -F '_' '{print "mv "$0" "$1"_oldgirl.HTML"}'|bash
注释:这里的xargs -nl可要可不要,另外上述脚本中的mv可以用rename替代,即:
#!/bin/bash Path="/oldboy" cd $Path && \ ls $Path|xargs -n1|awk -F '_' '{print "rename "$0" "$1"_oldgirl.HTML"}'|bash
3、批量创建10个系统账号并设置密码多种方法
运用到的知识点:
①useradd oldboy01
②echo "pass"|passwd --stdin oldboy
③给创建账号前加0,有两种方法:
1、seq -w 10
2、echo {00..10}
[root@Python test001]# seq -w 10 01 02 03 04 05 06 07 08 09 10 [root@Python test001]# echo {00..10} 00 01 02 03 04 05 06 07 08 09 10 [root@Python test001]# ④随机密码,有如下6种方法 ⑴ $RANDOM [root@Python test001]# echo $RANDOM|md5sum |cut -c 2-9 e691e92d ⑵ openssl #通过openssl产生随机数 [root@Python test001]# openssl rand -base64 45 i9IzYMu6QCIxSPLt0BQdkFzhA3ydJ12Y4mjcCL4zFMjVqBPjEGDmCQ+n5lPF [root@Python test001]#
⑶ date命令
[root@Python test001]# date +%s/%N #通过时间获取随机数
1453431452/536836731
[root@Python test001]# date +%s/%N|md5sum|tr "[0-9]" "[0-9][a-zA-Z]"
54af7dcb18a35cda83f299c7f389b720 -
⑷通过head /dev/urandom|cksum #设备产生随机数
[root@Python test001]# head /dev/urandom|cksum
2049066555 2401
[root@Python test001]#
⑸通过uuid随机数
[root@Python test001]# cat /proc/sys/kernel/random/uuid
1ea4deab-debc-4800-89d3-1817c198315b
[root@Python test001]#
⑹通过exect随机数
[root@Python test001]# yum -y install expect
[root@Python test001]# mkpasswd -l 10 -d 5
kn5G51X;73
[root@Python test001]#
实现代码如下:
#!/bin/bash
[ -f /etc/init.d/functions ]&& source /etc/init.d/functions [ $UID -ne 0 ]&&{echo "pls sudo to root "} for user in oldgirl{00..10} do work=$(grep "\b$user\b" /etc/passwd|wc -l) if [ $work -eq 1 ];then action "Useradd $user already exists" /bin/false continue fi pass=$(echo $RANDOM|md5sum|cut -c 1-8) useradd $user && echo "pass"|passwd --stdin $user &>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ];then action "Useradd $user is OK" /bin/true fi echo -e "\033[32m"$user"\033[0m \t \033[31m "$pass"\033[0m">>/tmp/user.txt done
注释:上述脚本设计到了知识点:
a、引用系统函数库
b、判断要创建的用户是否存在运用到了"grep \b$user\b"精确匹配,把grep "\b$user\b" /etc/passwd|wc -l 赋值给一个变量,将判断用户是否存在转换为判断变量值是否为1
c、在处理创建用户和生产随机密码时,运用&&符号,让账户产生成功后就赋给密码
d、判断运行脚本用户身份是否为root,如果不是则打印提示语句
另外一种方式实现(命令行)
运用知识点:
a、xargs -nn
将前面的内容通过管道交给xargs -n处理,拆分成列,后面n取值为1,2,3,4,5
[root@Python test001]# echo old{01..10}|xargs -n1
old01
old02
old03
old04
old05
old06
old07
old08
old09
old10
[root@Python test001]# echo old{01..10}|xargs -n2
old01 old02
old03 old04
old05 old06
old07 old08
old09 old10
[root@Python test001]# echo old{01..10}|xargs -n3
old01 old02 old03
old04 old05 old06
old07 old08 old09
old10
[root@Python test001]# echo old{01..10}|xargs -n4
old01 old02 old03 old04
old05 old06 old07 old08
old09 old10
b、运用sed语法替换
实现代码:
调试如下:
[root@Python test001]# echo old{01..10}|xargs -n1|sed -r 's#(.*)#useradd \1;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin \1;echo -e "\1" \t "$pass">>/tmp/use.txt#g'
useradd old01;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old01;echo -e "old01" "$pass">>/tmp/use.txt
useradd old02;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old02;echo -e "old02" "$pass">>/tmp/use.txt
useradd old03;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old03;echo -e "old03" "$pass">>/tmp/use.txt
useradd old04;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old04;echo -e "old04" "$pass">>/tmp/use.txt
useradd old05;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old05;echo -e "old05" "$pass">>/tmp/use.txt
useradd old06;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old06;echo -e "old06" "$pass">>/tmp/use.txt
useradd old07;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old07;echo -e "old07" "$pass">>/tmp/use.txt
useradd old08;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old08;echo -e "old08" "$pass">>/tmp/use.txt
useradd old09;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old09;echo -e "old09" "$pass">>/tmp/use.txt
useradd old10;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin old10;echo -e "old10" "$pass">>/tmp/use.txt
将调试结果的交给bash执行
[root@Python test001]# echo old{01..10}|xargs -n1|sed -r 's#(.*)#useradd \1;pass=$(echo $RANDOM|md5sum|cut -c 1-8);echo "$pass"|passwd --stdin \1;echo -e "\1" \t "$pass">>/tmp/use.txt#g'|bash
将初始代码进行拓展(运用ckpasswd命令):
#!/bin/bash
[ -f /etc/init.d/functions ]&& source /etc/init.d/functions
[ $UID -ne 0 ]&&{echo "pls sudo to root "}
for user in admin{00..10}
do
work=$(grep "\b$user\b" /etc/passwd|wc -l)
if [ $work -eq 1 ];then
action "Useradd $user already exists" /bin/false
continue
fi
pass=$(echo $RANDOM|md5sum|cut -c 1-8)
useradd $user && echo "$user:$pass"|tee >>/tmp/userlist.log|chpasswd
done
[root@Python test001]#
运行脚本:
[root@Python test001]# sh oldboy04-02.sh
[root@Python test001]# cat /tmp/userlist.log
admin00:9ed66b14
admin01:29af36b7
admin02:48929baa
admin03:8d541efd
admin04:0f8ea01f
admin05:994f8644
admin06:109e40a2
admin07:5c5a654b
admin08:9959dbbf
admin09:6847874d
admin10:4412f63c
[root@Python test001]#
3、开发脚本解决DOS攻击生产案例多种方法讲解
写一个脚本解决DOS攻击生产案例
提示:提示根据web日志或者网络连接数监控当某个IP并发连接数或者短内PV达到100,即调用防火墙命令封掉对应的IP,监控频率为每隔3分钟
根据web日志分析:日子文件为access_2016-01-22.log
#!/bin/bash [ -f /etc/init.d/functions ] && source /etc/init.d/functions Path=/root/system/test001/access_2016-01-22.log count=10 function ipt(){ awk '{print $1}' $Path|sort|uniq -c|sort -nr -k1 >/tmp/tmp.log exec </tmp/tmp.log while read line do IP=`echo $line|awk '{print $2}'` if [ `echo $line|awk '{print $1}'` -ge $count -a `iptables -L -n|grep "$IP"|wc -l` -lt 1 ];then iptables -I INPUT -s $IP -j DROP RETVAL=$? if [ $RETVAL -eq 0 ];then action "$IP is DROP" /bin/true echo "$IP" >>/tmp/ip_$(date +%F).log else action "$IP is DROP" /bin/false fi fi done } function del(){ [ -f /tmp/ip_$(date+%F -d '-1day').log ]||{echo "ip_$(date+%F -d '-1day').log is not exist";exit 1} exec >/tmp/ip_$(date+%F -d '-1day').log while read line do if [ `iptables -L -n|grep "$line"|wc -l` -ge 1 ];then iptables -D INPUT -s $line -j DROP fi done } main(){ flag=0 while true do sleep 3 ((flag++)) ipt [ $flag -ge 3 ] && del && flag=0 done } main
4、输出一堆英文其中字母数不大于6的单词(昆仑万维面试题)
I am oldboy teacher welcome to oldboy training class
思路:
首先需要取出每个单词然后查看每个单词的字符数,取单词有两种方法,如下:
⑴数组
⑵字符串(wc -w 去字符的单词数)
取单词长度的方法:
①${#变量}
②wc -L
③wc -c(比字符串长度多1,因为多了换行符)
④${变量:0:6}对比原变量
⑤expr length "变量"
⑥awk '{print length($变量)}'
实现代码①
#!/bin/bash array=(I am oldboy teacher welcome to oldboy training class) funFirst(){ for word in ${array[*]} do if [ ${#word} -le 6 ];then echo $word fi done } funFirst
实现代码②
#!/bin/bash array=(I am oldboy teacher welcome to oldboy training class) funFirst(){ for word in ${array[*]} do if [ $(echo $word|wc -L) -le 6 ];then echo $word fi done } funFirst
实现代码③
#!/bin/bash array=(I am oldboy teacher welcome to oldboy training class) funFirst(){ for word in ${array[*]} do check=$(echo $word|awk '{print length ($word)}') if [ $check -le 6 ];then echo $word fi done } funFirst
实现代码④
[root@Python test001]# word="I am oldboy teacher" [root@Python test001]# echo $word|xargs -n1|awk 'length >4{print $1}' oldboy teacher
实现代码⑤
[root@Python test001]# echo "I am oldboy teacher welcome to oldboy training"|tr " " "\n"|awk '{if (length($1)<=6) print $1}' I am oldboy to oldboy [root@Python test001]#
5、比较两个整数大小并严格判断参数输入