linux-based regular expressions, shell-based, file lookup and compression

Keywords: Linux network SELinux shell vim

1.shell programming shows basic information of computer and basic script. Only for version 6.7.

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "CPU type is $COLOR `lscpu |grep 'Model name'|tr -s ' '|cut -d: -f2`$COLOREND"
echo -e "Disk space is $COLOR `lsblk |grep 'disk' |tr -s ' '|cut -d' ' -f4|head -n 1` $COLOREND"
echo -e "Memory size is $COLOR `free -h |grep 'Mem'|tr -s ' '|cut -d' ' -f2` $COLOREND"
echo -e "OS Verion is $COLOR `cat /etc/redhat-release` $COLOREND"
echo -e "kernel version is $COLOR `uname -r` $COLOREND"
echo -e "IPaddr is $COLOR ` ifconfig ens33 |grep 'netmask'|tr -s ' '|cut -d' ' -f3`$COLOREND"        

2. Find out the IPv4 address of the local computer in the result of ifconfig "network card name" command

[root@centos7 scripts38]#ifconfig ens33 |grep netmask|tr -s ' '|cut -d' ' -f3
192.168.11.131
[root@centos7 ~]#ifconfig ens33 |grep ask | grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"|head -1
192.168.6.136

3. Find out the maximum percentage of partition space utilization

[root@centos7 scripts38]#df|grep '/dev/sd'|tr -s ' '|cut -d' ' -f5|tr -d %|sort -nr|head -n 1
17

4. Find out the user name, UID and shell type of the maximum user UID

[root@centos7 scripts38]#cat /etc/passwd |cut -d: -f1,3,7 |sort -t: -k2 -nr|head -n 1
nfsnobody:65534:/sbin/nologin

5. Find out the permissions of / tmp and display them digitally

[root@centos7 ~]#stat /tmp |grep 'drwx'|cut -d' ' -f2|tr -dc [0-9]
1777

6. Statistics the number of connections currently connected to each remote host IP, and sort them from large to small.

netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c
      1 192.168.6.1

7. Display lines in / proc/meminfo file beginning with size s (Requirements: Two methods)

[root@centos7 ~]#grep ^[sS]    /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88744 kB
SReclaimable:      39980 kB
SUnreclaim:        48764 kB
[root@centos7 ~]#grep '^s\|^S'    /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88744 kB
SReclaimable:      39980 kB
SUnreclaim:        48764 kB
[root@centos7 ~]#grep ^[s\|S]    /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88744 kB
SReclaimable:      39980 kB
SUnreclaim:        48764 kB
[root@centos7 ~]#grep -w \<'s\|S.*' /proc/meminfo 
SwapCached:         2288 kB
SwapTotal:       2097148 kB
SwapFree:        2058044 kB
Shmem:             16796 kB
Slab:              88716 kB
SReclaimable:      39980 kB
SUnreclaim:        48736 kB

8. Display rows in / etc/passwd file that do not end with / bin/bash

[root@centos7 ~]#cat /etc/passwd |grep -v  '/bin/bash$'
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

9. Display user rpc default shell program

[root@centos7 ~]#cat /etc/passwd |grep '\<rpc\>'|cut -d: -f7
/sbin/nologin

10. Find two or three digits in / etc/passwd

[root@centos7 ~]#cat /etc/passwd |grep   "\<[0-9]\{2,3\}\>"
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin

11. Display CentOS 7's / etc/grub2.cfg file, beginning with at least one blank character and followed by a non-
Lines of blank characters

[root@centos7 ~]#cat /etc/grub2.cfg |grep ^[[:space:]].*
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
  set timeout_style=menu
  set timeout=5
  set timeout=5
  source ${prefix}/user.cfg
  if [ -n "${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root ${GRUB2_PASSWORD}
  fi
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  b6888a3b-3755-49af-b1db-5b412eda8657
    else
      search --no-floppy --fs-uuid --set=root b6888a3b-3755-49af-b1db-5b412eda8657
    fi
    linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=05bdb265-31b6-4a0f-942b-f69d4b103e8d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-3.10.0-957.el7.x86_64.img
    load_video
    insmod gzio
    insmod part_msdos
    insmod xfs
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  b6888a3b-3755-49af-b1db-5b412eda8657
    else
      search --no-floppy --fs-uuid --set=root b6888a3b-3755-49af-b1db-5b412eda8657
    fi
    linux16 /vmlinuz-0-rescue-55bf685d782642d9bcbb1eaf123db175 root=UUID=05bdb265-31b6-4a0f-942b-f69d4b103e8d ro crashkernel=auto rhgb quiet
    initrd16 /initramfs-0-rescue-55bf685d782642d9bcbb1eaf123db175.img
  source ${config_directory}/custom.cfg
  source $prefix/custom.cfg;

12. Find the line in the "netstat-tan" command result that ends with LISTEN followed by any number of blank characters

[root@centos7 ~]#netstat -tan|grep 'LISTEN[[:space:]]\+'
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:6000            0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::6000                 :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN  

13. Display usernames and UIDs on CentOS7 with all UIDs less than 1000

[root@centos7 ~]#cat /etc/passwd |cut -d':' -f1,3 |grep -v '\<[0-9]\{4\}\>'
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
colord:997
rpc:32
gluster:996
saslauth:995
abrt:173
rtkit:172
pulse:171
radvd:75
rpcuser:29
nfsnobody:65534
unbound:994
chrony:993
qemu:107
tss:59
usbmuxd:113
geoclue:992
ntp:38
sssd:991
setroubleshoot:990
saned:989
gdm:42
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72
[root@centos7 ~]#cat /etc/passwd |cut -d':' -f1,3 |grep '\<[0-9]\{1,3\}\>'
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
colord:997
rpc:32
gluster:996
saslauth:995
abrt:173
rtkit:172
pulse:171
radvd:75
rpcuser:29
unbound:994
chrony:993
qemu:107
tss:59
usbmuxd:113
geoclue:992
ntp:38
sssd:991
setroubleshoot:990
saned:989
gdm:42
gnome-initial-setup:988
sshd:74
avahi:70
postfix:89
tcpdump:72

14. Add users bash, testbash, basher, sh, nologin (whose shell is / sbin/nologin) and find out rows with the same name of / etc/passwd user name and shell

[root@centos7 ~]#grep '\(.*\+\):.*\<\1$'  /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1026:1032::/home/nologin:/sbin/nologin
[root@centos7 ~]#egrep "(.*+):.*\<\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1026:1032::/home/nologin:/sbin/nologin

15. Use df and grep to extract disk partition utilization and sort from large to small

[root@centos7 ~]#df |grep '/dev/sd'|tr -s ' '|cut -d' ' -f5 |sort -nr
17%
5%
1%
[root@centos7 ~]#df |grep '/dev/sd'|grep -o '[0-9]\{0,3\}%' |sort -nr 
17%
5%
1%

16. View the large version of Linux.

[root@centos7 data]#cat /etc/redhat-release |cut -d. -f1|grep -o '[[:digit:]]'
7

17. Display UID s and default shell s for three users root, mage, wang

[root@centos7 scripts38]#cat /etc/passwd |grep  -w '^root\|^wang\|^mage'|cut -d: -f1,3,7
root:0:/bin/bash
wang:1027:/bin/bash
mage:1028:/bin/bash

18. Find a line in the / etc/rc.d/init.d/functions file with the first line of a word (including underscores) followed by a small bracket

[root@centos7 data]#cat /etc/rc.d/init.d/functions |grep  '^[[:alpha:]].*(.*).*'
systemctl_redirect () {
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
convert2sec() {
is_true() {
is_false() {
apply_sysctl() {
strstr "$(cat /proc/cmdline)" "rc.debug" && set -x

19. Use egrep to extract its base name and_list from / etc/rc.d/init.d/functions

Catalog Name
[root@centos7 data]#echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"
/etc/rc.d/init.d/

20. Statistics the number of times each host logs in with root IP address in last command

[root@centos7 data]#last |grep 'root'   | egrep -o \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}|sort|uniq -c|sort -nr
     70 192.168.6.1
      1 172.18.37.37

21. Extended regular expressions are used to represent 0-9, 10-99, 100-199, 200-249, 250-255, respectively.

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

22. Display all IPv4 addresses in the ifconfig command results

[root@centos7 data]#ifconfig |egrep -o "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
192.168.6.136
255.255.255.0
192.168.6.255
172.18.7.7
255.255.0.0
172.18.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255

23. Remove and sort each character in welcome to magedu linux, ranking it first with more repetitions

[root@centos7 data]#echo welcome to magedu linux |grep -o "."|sort|uniq -c|sort -nr
      3 e
      3  
      2 u
      2 o
      2 m
      2 l
      1 x
      1 w
      1 t
      1 n
      1 i
      1 g
      1 d
      1 c
      1 a

24. Write a script backup.sh to backup / etc / directory to / backup/etcYYYYmm-dd every day

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "${COLOR}Starting backup ...$COLOREND"
cp -av /etc/   /data/etc_backup-`date +%F`
echo -e "${COLOR}Backup is finished$COLOREND"

25. Write the script disk.sh to show the maximum utilization of space in the current hard disk partition

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "${COLOR}Starting Hard Disk Inspection ...$COLOREND"                              
echo -e "${COLOR}`df|grep "/dev/sd" |tr -s " "|cut -d ' ' -f5|sort -nr|head -1`$COLOREND"
echo -e "${COLOR}Hard Disk Inspection is finished$COLOREND"

26. Write a script links.sh to display the IPv4 address and the number of connections for each remote host that is connected to the host, and sort them from large to small by the number of connections.

COLOR="\033[1;36m"
COLOREND="\033[0m"
echo -e "${COLOR}Linsk ...$COLOREND"                              
echo -e "${COLOR}` netstat -tun|grep ESTAB|tr -s " " : |cut -d: -f6|sort -nr|uniq -c`$COLOREND"
echo -e "${COLOR}Linsk$COLOREND"

27. Location of General Version Information Storage

[root@centos7 data]#cat /proc/partitions
 Hard Disk Size Storage Location
[root@centos7 data]#cat /proc/meminfo
 Memory size storage location
[root@centos7 data]#cat /proc/cpuinfo
 CPU and other storage locations

28. Remote execution of scripts.

Start the service first, 6 bring the service with you
[root@centos6 ~]#service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for centos6.10.localdomain
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
Copy the script to host 6
[root@centos7 scripts38]#scp 2systeminfo.sh 192.168.6.129:/var/www/html/
The authenticity of host '192.168.6.129 (192.168.6.129)' can't be established.
RSA key fingerprint is SHA256:7SNWeID7Jr5cvOTZkLQWElsa/MbwymmEUZRhTd8qxJ4.
RSA key fingerprint is MD5:3e:fa:86:dd:c6:b8:41:34:a0:50:f3:f6:b3:17:82:9a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.6.129' (RSA) to the list of known hosts.
root@192.168.6.129's password: 
2systeminfo.sh                            100%  964   581.7KB/s   00:00    
Check to see if you copied to host 6
[root@centos6 ~]#ls /var/www/html/
2systeminfo.sh
It can be implemented by executing on other hosts.
[root@centos7 scripts38]#curl 192.168.6.129/2systeminfo.sh |bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--    100   964  100   964    0     0   335k      0 --:--:-- --:--:-- --:--:--  470k

29. Find out the path of the component in the result of the ldd/usr/bin/cat command

[root@centos7 data]#ldd /usr/bin/cat | grep  -o '/[^[:space:]]\+'
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2

30. Duplicate / etc/profile / tmp / record, Find Replacement Command delete empty characters in / tmp/profile file

[root@centos7 data]#cp /etc/profile /tmp/
[root@centos7 data]#vim /tmp/profile 
:%s/^[[:space:]]\+//g

31. Copy / etc/rc.d/init.d/functions / tmp directory, Find each beginning with an empty character with a replacement command of / tmp/functions, and add # numbers

[root@centos7 data]#cp /etc/rc.d/init.d/functions /tmp/
[root@centos7 data]#vim /tmp/functions 
:%s/^[[:space:]]/#/g

31. Write the script sumid.sh to calculate the sum of the UID s of the 10th and 20th users in the / etc/passwd file

NUM1=` cat /etc/passwd |head -1 |cut -d: -f3`
NUM10=`cat /etc/passwd |head -10 |tail -1|cut -d: -f3`
SUM=$[NUM1+NUM10]
echo "num1 user UUID + num10 user UUID = $SUM"

32. Write the script sumspace.sh, pass two file paths as parameters to the script, and calculate the sum of all blank lines in the two files.

NUM1=` grep '^$' $1 |wc -l`
NUM2=` grep '^$' $2 |wc -l`
SUM=$[NUM1+NUM2]
echo "The sum of blank lines =$SUM"

33. Write script sumfile.sh, statistics / etc, /var, /usr directory, how many first-level subdirectories and files are there?

ETC=`ls -l /etc/ | wc -l`
VAR=`ls -l /var/ | wc -l`
USR=`ls -l /usr/ | wc -l`
SUM=$[ETC+VAR+USR]
echo "sum=$SUM"

34. Write the script argsnum.sh, accept a file path as a parameter; if the number of parameters is less than 1,
The user is prompted to "give at least one parameter" and quit immediately; if the number of parameters is not less than 1, the number of blank lines in the file to which the first parameter refers is displayed.

[ $# -lt 1 ]  && { echo "At least one parameter should be given"; exit 10; } 
NUM1=` grep '^$' $1 |wc -l`
echo "The  blank lines =$NUM1"

35. Write a script hosting.sh, accept IPv4 address of a host as a parameter, test whether it is connected
Tong. If it can ping, the user is prompted to "the IP address is accessible"; if it cannot ping, the user is prompted to "the IP address is not accessible".

. /data/scripts38/color.sh
read -p "Please enter a correct IP:" IP
# [[ $# -lt 1 ]]  && { echo "Please enter a IP"; exit 10; }
[[ "$IP" =~  ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] || { echo -e "${COLOR} Please enter a IP${COLOREND}" ; exit 20;}
ping -c1 -w2 $IP  &> /dev/null && echo -e "${COLOR}this IP Address accessible ${COLOREND}"  ||echo -e "${COLOR}this IP Address inaccessible ${COLOREND}"

36. Write a script checkdisk.sh to check disk partition space and inode usage. If it exceeds 80%, the broadcast warning space will be full.

USE=` df|grep "/dev/sd" |tr -s " " %|cut -d "%" -f5|sort -nr|head -1` 
INODE=`df -i|grep "/dev/sd" |tr -s " " %|cut -d "%" -f5|sort -nr|head -1`
test $USE -gt 80 && echo " disk will be full " &&echo disk will be full | mail -s warning root || echo "disk is normal"
[[ "$INODE" -gt 80 ]] && echo " inode will be full " &&echo inode will be full | mail -s warning root || echo "inode is normal"
unset USE INODE

37. Write a script per.sh to determine whether the current user is unreadable and not writable to the specified parameter file

. /data/scripts38/color.sh
#read -p "Please enter a file:" FILE
[[ $# -lt 1 ]] && { echo -e "${COLOR}No input file${COLOREND}" ; exit 10;}
[[ ! -e $@ ]] && { echo -e "${COLOR}file does not exist${COLOREND}" ; exit 20;}
[ ! -r $@ -a ! -w $@ ] && { echo -e "${COLOR}The file is unreadable and not writable${COLOREND}" ;}
[  -r $@ -a ! -w $@ ] && { echo -e "${COLOR}The file is readable and not writable${COLOREND}" ;}
[  -r $@ -a  -w $@ ] && { echo -e "${COLOR}The file is readable and  writable${COLOREND}" ;}

38. Write the script excute.sh to determine whether the parameter file is a common file with SH suffix. If so, add all executable privileges, or prompt the user for non-script files.

. /data/scripts38/color.sh
[[ $# -lt 1 ]] && { echo -e "${COLOR}No input file${COLOREND}" ; exit 10;}
[[ ! -e $@ ]] && { echo -e "${COLOR}file does not exist${COLOREND}" ; exit 20;}
[ ! -f $@ ] && { echo -e "${COLOR}Not an ordinary document${COLOREND}" ; exit 30;}
[[ $@ =~ .sh$ ]] && (chmod a+x $@ ; echo -e "${COLOR}$@Is a script file ${COLOREND}" ) || { echo -e "${COLOR}User Non-script Files${COLOREND}" ; exit 40;}

39. Write scripts nologin.sh and login.sh to prohibit and allow ordinary users to log on to the system

. /data/scripts38/color.sh
[ -f /etc/nologin ] && echo -e "${COLOR}Normal users are prohibited from logging in${COLOREND}"|| (touch /etc/nologin ; echo -e "${COLOR}Normal users are prohibited from logging in${COLOREND}")
. /data/scripts38/color.sh
[ -f /etc/nologin ] && (rm -f /etc/nologin ; echo -e "${COLOR}Ordinary users can log in${COLOREND}")||echo -e "${COLOR}Ordinary users can log in${COLOREND}"

40. Age

read -p "Input your age: " AGE
if [[ ! "$AGE" =~ ^[0-9]+$ ]]; then
    echo "Please input a digit age"
elif [ $AGE -lt 18 ];then
    echo "GOOD good study"
elif [ $AGE -lt 80 ];then
    echo "goot goot work"
elif [ $AGE -le 120 ];then
    echo "enjoy life"
else
    echo "Welcome to the earth"
fi

41. Cocks and rabbits in the same cage, head 37, feet 94

read -p "please input the head number:" HEAD
read -p "please input the foot number:" FOOT
Rabbit=$[FOOT/2-HEAD]
Chook=$[HEAD-Rabbit]
echo "Number of rabbits:" $Rabbit
echo "Number of chook:" $Chook

Change ens33 to eth0

[root@centos7 ~]#sed -ri '/^[[:space:]]+linux16/s#(.*)#\1 net.ifnames=0#' /boot/grub2/grub.cfg
[root@centos7 ~]#sed -ir '/GRUB_CMDLINE_LINUX/s@"$@ net.ifnames=0"@' /etc/default/grub 

Close selinux

[root@centos7 data]#sed -i  's/SELINUX=enforcing/SELINUX=disabled/   ' /etc/selinux/config

42.

cat <<EOF
1.I don't know what I want to do.
2.It's not easy to do anything.
3.It's not good to do something.
4.Dinner
5.Sleep
6.Bean Bean
EOF
read -p "Lottery draw, enter a number (1)-6)): " NUM
case $NUM in 
1)
    echo "Better to sleep than to do anything."
    ;;
2)
    echo "Everything in dreams, what bicycles do you want?"
    ;;
3) 
    echo "Eat, sleep, play beans, day after day"
    ;;
4) 
    echo "Eat less. It's time to lose weight."
    ;;
5)
    echo "Sleep is a whole day."
    ;;
6)
    echo "What kind of beans do you want?"
    ;;
*)
    echo "Welcome to Earth"
esac

43. Write the script createuser.sh to realize the following functions: Make user names as parameters, if the user with specified parameters exists, it will show its existence, otherwise it will be added; Display the id number of user added and other information.

if [ -z "$1" ];then
    read -p "please input user:" USER
    if
        [ -z $USER ] ;then
        echo "Password cannot be empty"
        exit 10;
    else
        NAME=$USER
    fi
else 
    NAME=$USER
fi

if      id $NAME &> /dev/null ; then
    echo "$NAME Account already exists"
    exit 20
else
    useradd $NAME && echo "Password Creation Successful"
    echo magedu | passwd --stdin $NAME  &> /dev/null && echo "The initial password is: magedu"
    passwd -e $NAME &> /dev/null
    echo "userinfo:`id $NAME`"
fi

44. Write a script / root/bin/yesorno.sh, mention yes or no, and judge whether yes or no, or any other information, is lost by a household.

read -p "Do you agree? (yes or no): " ANSWER
case $ANSWER in
[Yy]|[Yy][Ee][sS])
    echo YES
    ;;
[Nn]|[Nn][Oo])
    echo NO
    ;;
*)
    echo input false
esac

45. Write a script / root/bin/filetype.sh, judge the path of the incoming items, and show the type of the items (ordinary, record, link, other types)

read -p "Enter a file:" A
if [ -z $A ];then
    echo "Input cannot be empty"
    exit
elif [ ! -e $A ];then
    echo "file does not exist"
    exit
elif [ -d $A ];then
    echo "This is a directory."
    exit
elif [ -L $A ];then
    echo "This is a link file."
    exit
elif [ -f $A ];then
    echo "This is an ordinary document."
    exit
else 
    echo "other file"
    exit

fi

46. Write a script / root/bin/checkint.sh to determine whether the parameters of user input are positive integers

 . /data/scripts38/color.sh
read  -t 30 -p "please input a number:" NUM
if [ -z $NUM ] ; then
    echo -e "${COLOR}Input cannot be empty ${COLOREND}"
    exit 10
elif [[ "$NUM" =~ ^[0-9]+$ ]] ;then
    echo "This is a positive integer."
    exit 20
else
    echo "This is not a positive integer."
    exit 30 
fi

47. Let PATH environment variables of all households have more than one path, such as: / usr/local/apache/bin

echo 'PATH=/usr/local/apache/bin:$PATH' >> /etc/profile.d/env.sh
source /etc/profile.d/env.sh

48. When the user root logs in, turn the command reference character into red, and activate the following alias:
rm='rm –i'
cdnet='cd /etc/sysconfig/network-scripts/'
editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
editnet='vim/etc/sysconfig/network-scripts/ifcfg-eno167736 or
ifcfg-ens33'(if the system is CentOS7)

echo "PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\]'" >> /etc/profile.d/env.sh
source /etc/profile.d/env.sh
echo "alias rm='rm –i'" >> ~/.bashrc                                      
echo "alias cdnet='cd /etc/sysconfig/network-scripts/'" >> ~/.bashrc
echo "alias editnet1='vim /etc/sysconfig/network-scripts/ifcfg-eth0'" >> ~/.bashrc
echo "alias editnet2='vim /etc/sysconfig/network-scripts/ifcfg-ens33'" >> ~/.bashrc
source .bashrc

49. Search/var_records all the subordinates of mail and the subordinate group is root.

[root@centos7 data]#find /var/ -user root -group mail
/var/spool/mail
/var/spool/mail/root

50. Find/var_and record all the pieces that do not belong to root, lp and gdm.

[root@centos7 data]#find /var/ -not \( -user root -o -user lp -o -user gdm \)
/var/tmp/abrt
/var/lib/colord
/var/lib/colord/icc

51. Find out that there are no master or group members on the current system and that they have been visited in recent weeks.

[root@centos7 data]#find /var -atime -7 -nouser -o -nogroup

52. Find/var. Record the content of the last week that has been modified, and it is not a postfix or a master not root.

[root@centos7 data]#find /var -mtime -7 -not \( -user root -o -group postfix \)
/var/log/cups
/var/spool/mail/test
/var/spool/mail/bash
/var/spool/mail/testbash
/var/spool/mail/basher
/var/spool/mail/sh
/var/spool/mail/nologin
/var/spool/mail/wang
/var/spool/mail/mage
/var/spool/mail/lll
/var/spool/mail/aaa
/var/spool/mail/aaaaa
/var/spool/mail/jiaoyu
/var/spool/mail/chen
/var/spool/postfix/active
/var/spool/postfix/incoming
/var/spool/postfix/maildrop
/var/spool/postfix/private
/var/spool/postfix/public

53. Find/etc. Record all the pieces of 1M and the type is ordinary.

[root@centos7 data]#find /etc/ -size +1M -type f
/etc/udev/hwdb.bin
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/brltty/zh-tw.ctb

54. Find / etc and record all households that do not have write permission.

[root@centos7 data]#find /etc/ -not -perm /222 
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/udev/hwdb.bin
/etc/openldap/certs/password
/etc/gshadow
/etc/shadow
/etc/gshadow-
/etc/ld.so.conf.d/kernel-3.10.0-957.el7.x86_64.conf
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/lvmdbusd.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/thin-performance.profile
/etc/dbus-1/system.d/cups.conf
/etc/pam.d/cups
/etc/shadow-
/etc/machine-id
/etc/sudoers

55. Find / etc and record rare households that do not have the right to hold privileges.

[root@centos7 data]#find /etc/ -not -perm -111

56. Find/etc/init.d. Record, all users have the right to hold the right to write, and other users have the right to write.

[root@centos7 data]#find /etc/init.d/ -perm -113

Posted by Elizabeth on Sat, 03 Aug 2019 00:13:30 -0700