Linux (17): Shell Programming (4)

Case 1: Bulk generate random characters in the file name case

For loop 10 batch create html files in / neo directory, wherein each file contains the name of the result needs to Example 10 random lowercase string plus fixed neo created as follows:

[root@oldgirl C19]# ls /neo
apquvdpqbk_neo.html mpyogpsmwj_neo.html txynzwofgg_neo.html
bmqiwhfpgv_neo.html mtrzobsprf_neo.html vjxmlflawa_neo.html
jhjdcjnjxc_neo.html qeztkkmewn_neo.html
jpvirsnjld_neo.html ruscyxwxai_neo.html

Refer to the following:

# Random number generation mode: 
echo $ the RANDOM                           # range of the random numbers: 0 ~ 32767 
OpenSSL RAND -base64 10                 # 10 indicates the length of the random number to be generated 
DATE + N% S%                             # made by the random number time 
head / dev / urandom | cksum        
uuidgen                                # 利用uuid
cat /proc/sys/kernel/random/uuid
the mkpasswd                              # need to install Expect; Expect yum the install -Y 
    L Total length
    d digits
    Lowercase letters number c
    The number of uppercase letters C
    The number of special characters s

# Example: 
[the root M01 @ ~] # the mkpasswd -l -d. 17 -C 20 is -s. 1. 1. 1 -C
1194S1242h756%909877

    
# Obtain a random 10 lowercase letters 
[root @ M01 ~] # echo "Oldboy $ the RANDOM" 
OLDBOY31600
[root @ M01 ~] # echo "Oldboy $ the RANDOM" | md5sum 
2b33f154112a2f371beba1600d24f246 - 
[root @ M01 ~] # echo "Oldboy $ the RANDOM" | md5sum | TR 0-9 AJ 
icfafgebagjaajeieifeifabgfhfeegj - 
[root @ M01 ~] # echo "Oldboy the rANDOM $ "| md5sum | TR 0-9 AJ | Cut -c 2-11 
afbagebbdf             # obtain a random 10 lowercase letters 
[root @ M01 ~] #  

[root @ M01 ~] # mkpasswd the -l -d 0 -C 10 0 -c 10 -s 0 # to get 10 random lowercase letters 
tkbviqfovu
[root@m01 ~]#


echo " Oldboy $ the RANDOM " | md5sum | TR 0-9 AJ | Cut -c 2-11 
    echo " 123456 " | md5sum                     # View a string of md5 value 
    CAT File | TR [0-9] [AJ]                  # the file the numbers 0-9 replaced AJ 
    Cut -C 2-11                                 # -C parameter indicates the character line displayed only in the specified range

# 示例代码:
[root@m01 cases]# cat case01.sh
#!/bin/bash
##############################################################
# File Name: case01.sh
# Version: V1.0
# Author: neo
# Organization: anonymous
# Created Time : 2019-07-04 11:15:30
# Description:
##############################################################
path=/neo
[ -D $ path] || mkdir -p $ path       # if / neo directory does not exist, create

for n in {1..10}
do
    random=`mkpasswd -l 10 -d 0 -C 0 -c 10 -s 0`
    touch $path/${random}_neo.html
done
[root@m01 cases]# sh case01.sh
[root@m01 neo]# ll
total 0
-rw-r--r-- 1 root root 0 Jul  4 11:23 hqxdsueipv_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 jpiauceiiy_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 lagtetdxnu_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 ogkstnleki_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 qkljrkpckc_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 rpslndkrjc_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 snvcqbwlko_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 tuwsslvcgn_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 tzxismkjka_neo.html
-rw-r--r-- 1 root root 0 Jul  4 11:23 wlaroblbvi_neo.html
[root@m01 neo]# 

Case 2: batch rename special cases 

Above all neo string Case 1 results in a file name changed Neomaple, is best implemented for cycling and extensions into all uppercase html

Refer to the following:

# Way a: for loop 
[root @ M01 Cases] # CAT case02.sh 
# / bin / bash! 
For File in `LS / Neo / * .html`
do
    mv $file /${file/neo.html/Neomaple.HTML}
done
[root@m01 cases]# 

# 方式二:awk 
[root@m01 neo]# ls *.html|awk -F "neo.html" '{print "mv",$0,$1"Neomaple.HTML"}'
mv dbizjjvosl_neo.html dbizjjvosl_Neomaple.HTML
mv ezoiojifzy_neo.html ezoiojifzy_Neomaple.HTML
mv fanspikyqs_neo.html fanspikyqs_Neomaple.HTML
mv hmvkxpoyru_neo.html hmvkxpoyru_Neomaple.HTML
mv hymssbshwu_neo.html hymssbshwu_Neomaple.HTML
mv kgavlirhts_neo.html kgavlirhts_Neomaple.HTML
mv khxcbyrnnt_neo.html khxcbyrnnt_Neomaple.HTML
mv ntgtztpzsp_neo.html ntgtztpzsp_Neomaple.HTML
mv pfykhbwgyk_neo.html pfykhbwgyk_Neomaple.HTML
mv sybwbmnmyh_neo.html sybwbmnmyh_Neomaple.HTML
[root@m01 neo]# ls *.html|awk -F "neo.html" '{print "mv",$0,$1"Neomaple.HTML"}'|bash
    
# Three ways: the rename 
[root @ M01 Neo] # . The rename "neo.html" "Neomaple.HTML" * .html # neo.html put into Neomaple.HTML; * html html represented the end of the file for

Case 3: Batch requires the user to create special cases

Bulk create 10 systems account neo01 - neo10 and set a password (the password is a random number, required characters and numbers mix)

# Analysis: 
1) 01 to generate a sequence of 10:
        echo {01..10}
        seq -w 10
2 ) random number:
        openssl rand -base64 10
3 ) Create a user and password:
        neo01 useradd                     # Create a user 
        # password Method 1: 
        echo password | passwd - stdin username    
         # password Method 2: 
        chpasswd command
            When chpasswd password, user name and password to follow this format: Username: Password
4) for circulation

# Way a: 
[the root @ M01 Cases] # CAT case03.sh 
# / bin / the bash! 
For n- in {01..10 }
do
    the passwd = `10` -base64 OpenSSL RAND     # save to make random password variable 
    useradd neo $ n
    echo $passwd|passwd --stdin neo$n
    echo -e " Neo the n-$ \ t $ passwd " >> / tmp / user.list    # to save the password to a file; the role of echo -e is to parse \ t 
DONE
[root@m01 cases]# sh case03.sh
Changing password for user neo01.
passwd: all authentication tokens updated successfully.
Changing password for user neo02.
passwd: all authentication tokens updated successfully.
Changing password for user neo03.
passwd: all authentication tokens updated successfully.
Changing password for user neo04.
passwd: all authentication tokens updated successfully.
Changing password for user neo05.
passwd: all authentication tokens updated successfully.
Changing password for user neo06.
passwd: all authentication tokens updated successfully.
Changing password for user neo07.
passwd: all authentication tokens updated successfully.
Changing password for user neo08.
passwd: all authentication tokens updated successfully.
Changing password for user neo09.
passwd: all authentication tokens updated successfully.
Changing password for user neo10.
passwd: all authentication tokens updated successfully.    
[root@m01 cases]# cat /tmp/user.list 
neo01    pFvlvG0NXpIvYg==
neo02    UPRO/U7uTxnJNw==
neo03    0xkAtO5U70ws5w==
neo04    wXR+K7fl+8i5wA==
neo05    7bt6b09wv5dw7Q==
neo06    AUNPXdRaJ8NUTA==
neo07    E+JPwENB7KXHUA==
neo08    1lZylckuf8rNlg==
neo09    ZD7qVwMfsPMvzg==
neo10    B3EgYyb/swvDow==
[root@m01 cases]# su - neo01
[neo01@m01 ~]$ su - neo02
Password: 
[neo02@m01 ~]$ whoami
neo02
[neo02@m01 ~]$ 

# Second way: 
[root @ M01 Cases] # CAT case0302.sh 
# / bin / bash! 
For the n- in `seq -w 11 15 `
do
    the passwd = `10` -base64 OpenSSL RAND     # save to make random password variable 
    useradd neo $ n
    echo " Neo $ the n-: $ passwd " >> / tmp / chpasswd.log     # the user name and password Username: Password this format to save a file in chpasswd.log 
done

chpasswd </tmp/chpasswd.log      # to /tmp/chpasswd.log chpasswd this file to redirect output by the command processing, is due in chpasswd.log Username: Password format saved, so can deal directly with chpasswd command 
[root Cases M01 @] # SH case0302.sh 
[the root @ M01 Cases] # CAT /tmp/chpasswd.log 
neo11: Ij76k7pbfyByZQ == 
neo12: lmCPKpKoUP / UOG == 
neo13: JNIfuo8wwXgEkA == 
neo14: QkBxlLf6mhQP + G == 
neo15: fbvtdTSu9yTySg == 
[root @ M01 Cases] # su - neo11 
[neo11 @ M01 ~] $ su - neo15
Password: 
[neo15 m01 @ ~ ] $


# Three ways: 
[root @ M01 Cases] # CAT case0303.sh 
# / bin / bash! 
/Etc/init.d/. Functions

if [ $UID -ne 0 ]
then
    action "only root can execute this script" /bin/false
    exit 1
be

for n in {12..17}
do
    the passwd = RAND -base64 OpenSSL `10 `
     IF `grep -w!   " Neo n-$ " / etc / the passwd &> / dev / null`    # grep -w exact matching; grep corresponding result if obtained, the grep command execution result is 0 (true), otherwise execution result is 1 (false); if `command expression &> / dev / null` when the judgment result of the command execution of the expression is true (since the look results of this overtime &> / dev / null, or if the string will be error) 
    the then
         # \ represent a newline character 
        useradd the n-Neo $ &> / dev / null && \
        echo $passwd|passwd --stdin neo$n &>/dev/null && \
        echo -e "neo$n\t$passwd" >>/tmp/user.list && \
        action "neo$n added successfully" /bin/true
    else
        action "neo$n exists already" /bin/false
    be
done
[root@m01 cases]# sh case0303.sh
neo12 exists already                                       [FAILED]
neo13 exists already                                       [FAILED]
neo14 exists already                                       [FAILED]
neo15 exists already                                       [FAILED]
neo16 added successfully                                   [  OK  ]
neo17 added successfully                                   [  OK  ]
[root@m01 cases]# 

Case 4: Scan the network memory alive hosts Case

Write a Shell script to determine 10.0.0.0/24 network, which currently online IP

1 ) the ping command
[root@m01 ~]# ping -c 2 -i 1 -w 3 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=0.052 ms

--- 10.0.0.61 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1005ms
rtt min/avg/max/mdev = 0.052/0.065/0.079/0.015 ms

ping command:
    - C number of ping
     - I of ping interval (s)
     - W of ping total duration (s)

2 ) nmap command
nmap -sP 10.0.0.0/24


# 方式一: ping
[root@m01 cases]# cat case0401.sh
#!/bin/bash
##############################################################
# File Name: case0401.sh
# Version: V1.0
# Author: neo
# Organization: anonymous
# Created Time : 2019-07-05 00:54:20
# Description:
##############################################################
for n in {1..254}
do
    {
    if `ping -c 1 -w 3 10.0.0.$n &>/dev/null`   # ping 通为true
    then
        echo "10.0.0.$n is up"
    else
        echo "10.0.0.$n is down"
    be
    } &   # {} {} Indicates & concurrent execution of code inside; here indicates quantities of ping 
DONE
[root@m01 cases]# 

# Second way: nmap 
[the root M01 @ ~] # nmap -sP 10.0.0.0/24 

Starting Nmap 5.51 (http://nmap.org) AT 2019-07-05 01:34 CST
Nmap scan report for 10.0.0.61
Host is up.
Nmap scan report for 10.0.0.253
Host is up (0.0032s latency).
MAC Address: 00:50:56:C0:00:08 (VMware)
Nmap scan report for 10.0.0.254
Host is up (0.00081s latency).
MAC Address: 00:50:56:EA:79:FB (VMware)
Nmap done: 256 IP addresses (3 hosts up) scanned in 8.36 seconds
[M01 the root @ ~] # nmap -sP 10.0.0.0/24|awk '/ Nmap Scan Report for / of NF} {Print $' # $ last Field (column) of NF expressed, i.e., the output of the contents of the last field
10.0.0.61
10.0.0.253
10.0.0.254
[root@m01 ~]# 

 

Guess you like

Origin www.cnblogs.com/neozheng/p/11131462.html