Exercise 1:Detect file Changes
Requirements: There are two Linux servers A and B, if a can ssh directly to B, do not enter the password. A and B have a directory called/data/web/. Here are a lot of files, of course, we do not know the specific number of subdirectories, if the previous A and B files in this directory are identical. But now I'm not sure if it's the same. Solid needs us to write a script to implement such a function, detection of a machine and B machine/data/web/directory of the similarities and differences, we use a machine on the document as standard. For example, if the B machine is missing a a.txt file, then we should be able to detect it, or B machine on the B.txt file has changed, we should be able to detect (b machine More files We do not consider).
Tip: Use the core command md5sum A.txt to figure out the MD5 value and compare it to the B machine.
Reference Answer:
#!/bin/bash# date:2018 March 15 dir=/data/webb_ip=192.168.139.128find $dir -type f |xargs md5sum > /tmp/md5.txtssh $B _ip "find $dir -type f |xargs md5sum > /tmp/md5_b.txt "scp $B _ip:/tmp/md5_b.txt /tmpfor f in ' awk ' {print $2} ' /tmp/md5.txt ' do if grep -q "$f" /tmp/ Md5_b.txt then md5_a= ' grep $f /tmp/md5.txt|awk ' {print $1} ' md5_b= ' grep $f /tmp/md5_b.txt|awk ' {print $1} ' if [ $MD 5_a != $MD 5_b ] then echo "$f changed ..." fi else echo "$f deleted ..." fidone
Exercise 2: determine if 80 ports are turned on
Requirement: write a script to determine if the Web service is open on your Linux server? (Listen to port 80) if it is turned on, please determine what the service is running, is it httpd or nginx or something else?
reference answer:
#!/bin/bash# date:2018 year March 15 port= ' netstat -lnp|grep -w 80 ' if [ -z "$port ];then echo " not service Start! " exit;fiweb_service= ' echo $port |awk -f '/' ' {print $2} ' |awk -F ': ' ' {print $1} ' case $web _service in httpd) echo "Apache server." ;; nginx) echo "Nginx server." ;; *) echo "Other server." ;; Esac
Exercise 3: monitoring MySQL Services
requirements: Assuming that the current MySQL service has a root password of 123456, the script detects that the MySQL service is normal (for example, it can go to MySQL to perform show processlist) and detects if the current MySQL service is master or slave, If it is from, please determine if its master-slave service is abnormal. If it's the Lord, you don't need to do anything.
reference answer:
#!/bin/bash# date:2018 March 15 login= "mysql -uroot -p123456" $login -e "show Processlist " >/tmp/mysql_error.log 2>&1if [ $? -ne 0 ]then echo "mysql server something wrong!" exit;else $login -e "Show slave status\g" > /tmp/mysql_slave_status.log res= ' Wc -l /tmp/mysql_slave_ status.log| awk ' {print $1} ' if [ $res -gt 0 ] then y1=grep "slave_io_running:" /tmp/mysql_slave_status.log |awk ' {print $2} ' y2=grep "slave_sql_running:" /tmp/mysql_slave_status.log |awk ' {print $2} ' if [ "$y 1" == "yes" ] && [ "$y 2" == "yes" ] then echo "Slave status good!" else echo "slave down!" fi fifi
Exercise 4: user script with options
Requirements:
1 only three options ' –del ' –add ' help input other options error.
2 The use of ' –add ' requires verifying that the user name exists and that the feedback exists. And not added. Does not exist then create the user, cut > Add the same password as the user name. and feedback.
3 The use of ' –del ' requires verifying that the user name exists and that the user and his or her home directory are deleted. Does not exist then feedback that the user does not save > in.
4–HELP option feedback out how to use
5 support to, separate delete multiple or add multiple users at once.
6 can use echo $? Detect script execution successfully deleted or added as 0, error messages are other numbers.
7 can with, divide. Add or remove multiple users at once. For example Adddel.sh–add user1,user2,user3 ....
8 No obvious bugs are allowed.
reference answer:
#!/bin/bash# date:2018 March 15 If [ $# -eq 0 -o $# -gt 2 ]then echo "usage $0 --add username or $0 --del Username or $0 --help. " exit 1ficase $1 in --add) for u in ' echo $2|sed ' s/,/ /g ' do if awk -F ': ' ' {print $1} ' /etc/passwd|grep -qw ' $u ' then echo "the user $u is exist." else & nbsp; useradd $u echo -e $u "$u \n$u" |passwd > /dev/null 2>&1 echo "the user $u added successfully." fi done ;; --del) for u in ' echo $2|sed -r ' s/,/ /g ' do if awk -F ': ' ' {print $1} ' /etc/passwd|grep -qw "$u" then userdel -r $u echo -e "the user $u deleted successfully." else echo "the user $u not exist. " fi done ;;     --HELP) echo -e "--add Can add user,and the passwd is the same as username.\nit can add multiuser such as --add user1,user2,user3 " echo -e "--del cat delete user. It can delete user such as --del user1,user2,user3 ... " ;; *) echo "Usage $0 --add username or $0 --del username or $0 --help. " exit 1 ;; Esac
Shell Exercises (12)