The latest Linux operations must be 22 shell programming surface questions Wonderful explanation

Source: Internet
Author: User
Tags ip number stdin

1. Batch creation of files with random strings multiple methods

This time, the $random variable is used.

The Create command is as follows:

echo $RANDOM |md5sum|tr "[0-9]" "[A-z]" |cut-c 3-8

Output a random number via echo, then encrypt it by MD5, convert the number to a letter, and take the 10

The specific script is implemented as follows:

#!/bin/bash#echo $RANDOM |md5sum|tr "[0-9]" "[A-z]" |cut-c 3-8 [-D "/oldboy"]| | Mkdir-p/oldboyfor N in ' seq Ten ' do touch/oldboy/$ (echo $RANDOM |md5sum|tr "[0-9]" "[A-z]" |cut-c 3-8) _oldboy.htmldone

To run the script:

[Email protected] test001]# sh oldboy01.sh

View results:

[Email protected] test001]# ls-rt/oldboy/gdcffb_oldboy.html fghgca_oldboy.html jffffh_oldboy.html dhbdec_oldboy.htm L fgjehc_oldboy.htmlecfbca_oldboy.html gigcfe_oldboy.html deabbf_oldboy.html jbfbia_oldboy.html chacif_oldboy.html[ [Email protected] test001]#


Note: Get random letters:

[email protected] test001]# OpenSSL rand-base64 40|sed ' s#[^a-z]# #g ' acgilbtgjwpkejryjybkg

So the above script can be rewritten in the following form:

#!/bin/bashpath=/oldboy[-D "$Path"]| | Mkdir-p/oldboyfor N in ' seq Ten ' do random=$ (OpenSSL rand-base64 40|sed ' s#[^a-z]# #g ' |cut-c 2-11) touch/oldboy/${ Random}_oldboy.htmldone

To run the script:

[[Email protected] 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&Nbsp;root root 0 jan 22 09:58 lzxgyzqrbd_oldboy.html 

2, Batch modified file name a variety of ways to explain

This involves stitching the commands, using a For loop to take out the file, then splitting it with "_" through awk, removing the fixed suffix (print $), and then running the MV command to rename the source file $n to $name${filename}

#!/bin/bashfilename=_.oldgirl. Htmldirname= "/oldboy" CD $Dirnamefor N in ' Ls/oldboy ' Do name=$ (ls/oldboy/${n}|awk-f: ' _ ' {print $} ') MV $n ${name}${f Ilename}done

[Email protected] test001]#


Or

#!/bin/bashfilename=_oldgirl. Htmldirname= "/oldboy" CD $Dirnamefor n in ' ls ' do name=$ (Echo ${n}|awk-f ' _ ' {print $} ') MV $n ${name}{filename}don E

Overwrite the above script or use awk

#!/bin/bashpath= "/oldboy" CD $Path && ls $Path |xargs-n1|awk-f ' _ ' {print "MV" $ "" $ "_oldgirl. HTML "} ' |bash

Note: The XARGS-NL here can not be, in addition to the above script MV can be replaced with rename, namely:

#!/bin/bashpath= "/oldboy" CD $Path && ls $Path |xargs-n1|awk-f ' _ ' {print ' rename ' $ "" $ "_oldgirl. HTML "} ' |bash

3, Batch create 10 system account number and set the password a variety of methods

The knowledge points applied to:

①useradd Oldboy01

②echo "Pass" |passwd--stdin Oldboy

③ add 0 to the account before creating it, there are two ways to do it:

1, Seq-w 10

2. Echo {00..10}

[[email protected] test001]# seq-w 1001020304050607080910[[email protected] test001]# echo {00..10}00 01 02 03 04 05 06 0 7 10[[email protected] Test001]#④ random password, there are 6 ways to ⑴ $RANDOM [[email protected] test001]# echo $RANDOM |md5sum |cut-c 2-9e6 91e92d⑵openssl #通过openssl产生随机数 [[email protected] test001]# OpenSSL rand-base64 45i9izymu6qcixsplt0bqdkfzha3ydj12y4mjccl4zfmjvqbpjegdmcq+n5lpf[[email protected] test001]#

⑶date command

[Email protected] test001]# date +%s/%n #通过时间获取随机数

1453431452/536836731

[Email protected] test001]# date +%s/%n|md5sum|tr "[0-9]" "[0-9][a-za-z]"

54af7dcb18a35cda83f299c7f389b720-

⑷ through Head/dev/urandom|cksum #设备产生随机数

[Email protected] test001]# head/dev/urandom|cksum

2049066555 2401

[Email protected] test001]#

⑸ by UUID random number

[Email protected] test001]# Cat/proc/sys/kernel/random/uuid

1ea4deab-debc-4800-89d3-1817c198315b

[Email protected] test001]#

⑹ by Exect random number

[Email protected] test001]# yum-y install expect

[Email protected] test001]# mkpasswd-l 10-d 5

kn5g51x;73

[Email protected] test001]#

The implementation code is as follows:

#!/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.txtdone 

Note: The above script is designed to the knowledge point:

A, referencing the system function library

b, determine whether the user to create a "grep \b$user\b" to apply the exact match, the grep "\b$user\b"/etc/passwd|wc-l assigned to a variable, will determine whether the user exists to determine whether the value of the variable is 1

C, in the process of creating users and production of random passwords, the use of && symbols, so that the account has been successfully assigned to the password

D. Determine if the user running the script is root, and if not, print the prompt statement


Another way to implement (command line)

Use knowledge points:

A, Xargs-nn

Pass the preceding content through the pipeline to xargs-n processing, split into columns, followed by n values of 1,2,3,4,5

[Email protected] test001]# echo old{01..10}|xargs-n1

Old01

Old02

Old03

Old04

Old05

Old06

Old07

Old08

Old09

Old10

[Email protected] test001]# echo old{01..10}|xargs-n2

OLD01 old02

OLD03 old04

OLD05 old06

Old07 old08

Old09 OLD10

[Email protected] test001]# echo old{01..10}|xargs-n3

OLD01 old02 old03

OLD04 old05 old06

Old07 old08 old09

Old10

[Email protected] test001]# echo old{01..10}|xargs-n4

OLD01 old02 old03 old04

OLD05 old06 old07 old08

Old09 OLD10

B. Using SED syntax to replace

Implementation code:

Debug as follows:

[[email protected] 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" >>/t Mp/use.txt

Useradd old02;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old02;echo-e "old02" "$pass" >>/t Mp/use.txt

Useradd old03;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old03;echo-e "old03" "$pass" >>/t Mp/use.txt

Useradd old04;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old04;echo-e "old04" "$pass" >>/t Mp/use.txt

Useradd old05;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old05;echo-e "old05" "$pass" >>/t Mp/use.txt

Useradd old06;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old06;echo-e "old06" "$pass" >>/t Mp/use.txt

Useradd old07;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old07;echo-e "old07" "$pass" >>/t Mp/use.txt

Useradd old08;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old08;echo-e "old08" "$pass" >>/t Mp/use.txt

Useradd old09;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old09;echo-e "old09" "$pass" >>/t Mp/use.txt

Useradd old10;pass=$ (Echo $RANDOM |md5sum|cut-c 1-8), echo "$pass" |passwd--stdin old10;echo-e "Old10" "$pass" >>/t Mp/use.txt


Give the debug results to bash execution

[[email protected] 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


Extend the initial code (using the CKPASSWD command):

#!/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

[Email protected] test001]#

To run the script:

[Email protected] test001]# sh oldboy04-02.sh

[Email protected] 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

[Email protected] test001]#



3, development scripts to solve the Dos attack production case a variety of ways to explain

Write a script to solve the DOS attack production case

Tip: Tips based on web logs or network connections monitoring when an IP number of concurrent connections or a short PV reaches 100, that is, call the firewall command to seal off the corresponding IP, monitoring frequency of every 3 minutes

Based on Web log analysis: The days file is Access_2016-01-22.log

#!/bin/bash[ -f /etc/init.d/functions ] && source /etc/init.d/ Functionspath=/root/system/test001/access_2016-01-22.logcount=10function ipt () {awk  ' {print $1} '   $Path |sort|uniq -c|sort -nr -k1 >/tmp/tmp.logexec </tmp/tmp.logwhile  read linedo 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 fidone}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 ') .logwhile read linedoif [  ' iptables -L -n| grep  "$line" |wc -l '  -ge 1 ];then       iptables  -D INPUT -s  $line  -j drop fidone}main () {   flag=0    while true   do        sleep 3         ((flag++))        ipt       [  $flag  -ge 3 ] &&  Del && flag=0   done}main

4. Output a pile of English words with a number of letters not greater than 6 (Kunlun Worldwide interview questions)

I am Oldboy Teacher Welcome to Oldboy Training class

Ideas:

You first need to take out each word and then look at the number of characters in each word, and take the word in two ways, as follows:

⑴ Array

⑵ string (number of words wc-w de-char)


How to take the word length:

①${#变量}

②wc-l

③wc-c (1 more than string length because of the line break)

④${variable: 0:6} vs. original variable

⑤expr Length "Variable"

⑥awk ' {print length ($ variable)} '


Implementing Code ①

#!/bin/basharray= (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

Implementing Code ②

#!/bin/basharray= (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

Implementing Code ③

#!/bin/basharray= (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

Implementing Code ④

[Email protected] test001]# word= "I am oldboy teacher" [[email protected] test001]# echo $word |xargs- N1|awk ' length >4{print $ ' oldboyteacher

Implementing Code ⑤

[[email protected] test001]# echo "I am Oldboy teacher Welcome to Oldboy Training" |tr "" \ n "|awk ' {if (length ($) <=6) Print $ ' iamoldboytooldboy[[email protected] test001]#


This article from "Flat Light is true" blog, please be sure to keep this source http://codings.blog.51cto.com/10837891/1737734

The latest Linux operations must be 22 shell programming surface questions Wonderful explanation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.