Shell combat (interview)


This article is to record some interesting and important Shell programming examples that I usually encounter. It does not involve the explanation of special basic knowledge, but will clearly point out the knowledge points and various ideas and some practical methods involved in each example. Will be updated continuously


Generate random character file names in batches

1. Requirement: Generate files in batches, require the file name to be randomly generated and all lowercase letters with 10 digits
2. Idea: Generate random characters first, and then replace the uppercase letters and numbers in the characters with lowercase letters by method, or remove them directly

Three. Knowledge points:
the method of generating random numbers
(1) the use of random
characteristics: random numbers range from 0 to 32767, the encryption is not very good, you can generate more irregular random numbers by operating with md5sum and intercept

[root@localhost application]# echo $RANDOM
32041
[root@localhost application]# echo $RANDOM
15603
[root@localhost application]# echo $RANDOM |md5sum|cut -c 10-20
c5d6e24eebd
#cut -c 截取第几位到第几位

(2) Use openssl to generate random numbers
Features: combining numbers with upper and lower case characters, and with special characters, you can get a long number of digits

[root@localhost application]# openssl rand -base64 20
gFPC0GQ7ULl9dzuer3lB7k1bePA=

4. Specific implementation

通过RANDOM变量实现
[root@localhost test]# for i in `seq 10` ; do touch `echo $RANDOM |md5sum|cut -c  1-40|sed 's#[^a-z]##g'|cut -c 1-10`;done
[root@localhost test]# ls
aeeebaadab  baafcbedda  bdaaaedfbf  dedaacd  ecadadbcce  edbdebdeb  eedcfdfbff  fcecdcaeeb  feedebcccc  ffcaaefaba

通过openssl实现
[root@localhost test]# for i in `seq 10` ; do touch `openssl rand -base64 40|sed 's#[^a-z]##g' | cut -c 1-10` ;done
[root@localhost test]# ls
ebguimpswn  flshyoqoxn  izjokaxqxf  kdxygjiroj  mfaeopjzfh  opedlaczfu  srvtmnrjhx  szbsrnobhe  tdrqybsrcc  vatddlbizj

Batch rename

1. Requirement: replace the file starting with fuxiangyu with abc_ as empty
2. Idea: You can use the mv command to change the name, use text processing tools and pipes to realize the splicing of the mv command, or you can use the special rename tool rename
three : The use of knowledge point
1.rename is involved
Role: replace the content specified in the file name

rename "被替换的字符串" "替换后的字符串" 文件名

[root@localhost test]# rm -rf
[root@localhost test]# touch fuxiangyuabc.txt
[root@localhost test]# rename "abc" "ABC" fuxiangyuabc.txt 
[root@localhost test]# ls

2. Common methods for renaming strings
(1) Use $ {string / <replaced field> / replacement field}

[root@localhost test]# a=123abc789
[root@localhost test]# echo ${a/abc/456}
123456789

(2) Replace with sed (splicing mv command)

[root@localhost test]# a=123abc789
[root@localhost test]# echo $a | sed 's#abc#456#g'
123456789

4. Specific implementation

创建文件作操作用
[root@localhost test]# touch fuxiangyuabc_{1..10}
[root@localhost test]# ls
fuxiangyuabc_1   fuxiangyuabc_2  fuxiangyuabc_4  fuxiangyuabc_6  fuxiangyuabc_8
fuxiangyuabc_10  fuxiangyuabc_3  fuxiangyuabc_5  fuxiangyuabc_7  fuxiangyuabc_9

1.使用变量自带的方法拼接mv命令
[root@localhost test]# for i in `ls fuxiangyu*`; do mv $i `echo ${i/abc_/}`;done
[root@localhost test]# ls
fuxiangyu1  fuxiangyu10  fuxiangyu2  fuxiangyu3  fuxiangyu4  fuxiangyu5  fuxiangyu6  fuxiangyu7  fuxiangyu8  fuxiangyu9

2.使用sed拼接mv命令的方法
[root@localhost test]# ls
fuxiangyuabc_1   fuxiangyuabc_2  fuxiangyuabc_4  fuxiangyuabc_6  fuxiangyuabc_8
fuxiangyuabc_10  fuxiangyuabc_3  fuxiangyuabc_5  fuxiangyuabc_7  fuxiangyuabc_9
[root@localhost test]# for i in `ls fuxiangyu*`; do mv $i `echo $i | sed 's#abc_##g'`;done
[root@localhost test]# ls
fuxiangyu1  fuxiangyu10  fuxiangyu2  fuxiangyu3  fuxiangyu4  fuxiangyu5  fuxiangyu6  fuxiangyu7  fuxiangyu8  fuxiangyu9

3.使用改名工具rename改名
[root@localhost test]# ls
fuxiangyuabc_1   fuxiangyuabc_2  fuxiangyuabc_4  fuxiangyuabc_6  fuxiangyuabc_8
fuxiangyuabc_10  fuxiangyuabc_3  fuxiangyuabc_5  fuxiangyuabc_7  fuxiangyuabc_9
[root@localhost test]# rename "abc_" "" fuxiangyu*
[root@localhost test]# ls
fuxiangyu1  fuxiangyu10  fuxiangyu2  fuxiangyu3  fuxiangyu4  fuxiangyu5  fuxiangyu6  fuxiangyu7  fuxiangyu8  fuxiangyu9

Write a script to create a user, and randomly generate a password for the user, the password is saved in the file

The implementation of this script is a combination of the above two examples. Below we give specific implementation
requirements: create ten users, prefixed with fuxiangyu, followed by "_number", such as fuxiangyu_1, and generate a random password for it, generate The password is saved in the passwd file in the root directory

以下是基本实现功能
[root@localhost /]# vim useradd.sh 
#!/bin/bash 
[ -f /passwd ] || touch /passwd    ###判断存放密码的文件是否存在,如果不存在则创建一个
user=fuxiangyu_                    ###定义文件名的前缀
for i in `seq 10`
do 
  useradd $user$i &>dev/null       ###创建用户
  passwd=`openssl rand -base64 40 |cut -c 1-10` ###使用随机数给用户生成密码
  echo $passwd | passwd --stdin $user$i &>/dev/null ###给用户添加密码
  [ $? -eq 0 ] && echo "user $user$i is ok" || echo "ERROR" ###判断创建用户是否成功,如果成功输出OK,否则报错
  echo "username=$user$i  passwd= $passwd" >> /passwd###将用户名和密码的对应打印到文件中
done

效果
[root@localhost /]# bash useradd.sh 
user fuxiangyu_1 is ok
user fuxiangyu_2 is ok
user fuxiangyu_3 is ok
user fuxiangyu_4 is ok
user fuxiangyu_5 is ok
user fuxiangyu_6 is ok
user fuxiangyu_7 is ok
user fuxiangyu_8 is ok
user fuxiangyu_9 is ok
user fuxiangyu_10 is ok
[root@localhost /]# tail -10 /etc/passwd
fuxiangyu_1:x:1001:1015::/home/fuxiangyu_1:/bin/bash
fuxiangyu_2:x:1002:1016::/home/fuxiangyu_2:/bin/bash
fuxiangyu_3:x:1003:1017::/home/fuxiangyu_3:/bin/bash
fuxiangyu_4:x:1004:1018::/home/fuxiangyu_4:/bin/bash
fuxiangyu_5:x:1005:1019::/home/fuxiangyu_5:/bin/bash
fuxiangyu_6:x:1006:1020::/home/fuxiangyu_6:/bin/bash
fuxiangyu_7:x:1007:1021::/home/fuxiangyu_7:/bin/bash
fuxiangyu_8:x:1008:1022::/home/fuxiangyu_8:/bin/bash
fuxiangyu_9:x:1009:1023::/home/fuxiangyu_9:/bin/bash
fuxiangyu_10:x:1010:1024::/home/fuxiangyu_10:/bin/bash

Write a script to delete users in batches

1. Requirements: Delete the users generated by the above script
2. Analysis: We can use the userdel command or modify the configuration file / etc / passwd to delete users that meet the requirements in batches
3. Related knowledge points
1. Users in Linux, The shell used by ordinary ordinary users and root users is / bin / bash, so we can find out ordinary users based on this feature

[root@localhost /]# cat /etc/passwd | grep /bin/bash |grep -v root
fuxiangyu_1:x:1001:1015::/home/fuxiangyu_1:/bin/bash
fuxiangyu_2:x:1002:1016::/home/fuxiangyu_2:/bin/bash
fuxiangyu_3:x:1003:1017::/home/fuxiangyu_3:/bin/bash
fuxiangyu_4:x:1004:1018::/home/fuxiangyu_4:/bin/bash
fuxiangyu_5:x:1005:1019::/home/fuxiangyu_5:/bin/bash
fuxiangyu_6:x:1006:1020::/home/fuxiangyu_6:/bin/bash
fuxiangyu_7:x:1007:1021::/home/fuxiangyu_7:/bin/bash
fuxiangyu_8:x:1008:1022::/home/fuxiangyu_8:/bin/bash
fuxiangyu_9:x:1009:1023::/home/fuxiangyu_9:/bin/bash
fuxiangyu_10:x:1010:1024::/home/fuxiangyu_10:/bin/bash

###首先筛选出使用/bin/bash的用户,然后再使用-v参数过滤出root用户

4. Realization

[root@localhost /]# vim userdel.sh
 #!/bin/bash
for i in `cat /etc/passwd | grep fuxiangyu|awk -F ":" '{print $1}'`
do
  userdel $i
done

Write a script to realize regular backup of data

The regular backup of data is actually a simple scheduled task. Of course, sometimes you need to perform operations such as cutting the log. I wo n’t give it too deep here.

 cp /var/log/httpd/access_log  /home/log.bak/access_log$(date +%Y%m%d-%H%M%S)

将上述语句直接加入到定时任务中即可
crontab -e  ###进入编辑定时任务的界面
00 00 * * * cp /var/log/httpd/access_log  /home/log.bak/access_log$(date +%Y%m%d-%H%M%S)


Write a script to delete the data backup seven days ago

1. Requirement: Find the files that have been created in the specified directory for more than seven days and delete them
. 2. Idea: You can find the files that meet the requirements by using the find option.
3. Knowledge points involved:
1. Find the file by the find command. According to time, size, and permissions
particular to achieve four

find /var/log/httpd/ -type f -mmtime +7 -exec rm -rf {} \;
具体更多的操作可以参见find的用法
注意 -exec的使用  后面要加 “{} \;

Detect hosts on the network

1. Requirements: Use scripts to detect surviving hosts in the 192.168.10.0/24 network segment
2. Ideas: Use ping command or nmap command to detect surviving hosts in the network segment
3. Specific implementation
1. Use ping command

#!/bin/bash
net="192.168.10."      ###定义要检测的网段
for i in `seq 254`     ###设置范围
do
{
  ping -c 2 $net$i &>/dev/null  ###执行ping命令,指定次数为两次,并将输出结果放入/dev/null中
  [ $? -eq 0 ]&& echo "$net$i is accessible " ###判断ping命令的返回值如果返回值为0,则表示主机可到达,即存活

}&  ###批量执行ping命令可以提高侠侣
done



实验效果
[root@www /]# bash host_test.sh 
192.168.10.10 is accessible 
192.168.10.2 is accessible 
192.168.10.1 is accessible 

2. How to use nmap ### nmap will not repeat them here

[root@www /]# vim host_test.sh 
#!/bin/bash
CMD="nmap -sn "
ip='192.168.10.0/24'
for i  in `$CMD $ip|awk -F " for " '{print $2}'`
do
 echo "$i is ok"
done

实现效果
[root@www /]# bash host_test.sh 
192.168.10.1 is ok
192.168.10.2 is ok
www.liusi.com is ok
(192.168.10.10) is ok

Check if the specified website is working

1. Requirements: Write a script to detect the website is working normally
2. Idea: Use the command curl or wget to detect the website
3. Implementation
1. Use wget to detect

#!/bin/bash
array=(           ###定义检查的对象组
      www.baidu.com
      www.4399.com
      www.51cto.com
      www.fuxiangyu999.com
)
for ((i=0;i<${#array[*]};i++))
 do
  wget --spider -q ${array[$i]}   ###wget的--spider选项
  [ $? -eq 0 ] && echo " ${array[$i]} is ok " || echo "${array[$i]}  is have a trouble"
 done
 ###通过wget的返回值来判断网站是否正常运作

效果
[root@www /]# bash web_check.sh 
 www.baidu.com is ok 
 www.4399.com is ok 
 www.51cto.com is ok 
www.fuxiangyu999.com  is have a trouble

2. Use curl for detection

Published 24 original articles · Like 10 · Visits 2365

Guess you like

Origin blog.csdn.net/flat0809/article/details/103136702