Linux+python high-end operation and maintenance class fifth time operation

Source: Internet
Author: User

The contents of this assignment:

1, write a script to complete the following functions

(1) Pass a disk device file path to the script to determine whether the device exists;

(2) If present, displays all the partition information on this device;

For:

#!/bin/bash#if [$#-ne 1]; Thenecho "Please enter a disk device." Exit 2fiif [-B $]; Then Fdisk-l $1elseecho the "This disk device was not exist." Fi

2, write a script to complete the following functions

Pass a parameter to the script, this parameter is one of gzip, bzip2 or XZ;

(1) If the value of parameter 1 is gzip, use the TAR and gzip archive to compress/etc directories into the/backups directory and name/backups/etc-20160613.tar.gz;

(2) If the value of parameter 1 is bzip2, use tar and bzip2 archive to compress/etc directory to/backups directory, and named/backups/etc-20160613.tar.bz2;

(3) If the value of parameter 1 is XZ, then the tar and XZ archives are used to compress/etc directories into the/backups directory and named/backups/etc-20160613.tar.xz;

(4) Any other value, the error compression tool is displayed, and an abnormal exit is performed;

For:

#!/bin/bash#[-d/backups] | | Mkdir/backupsread-p "Pelase input a Argu (GZIP/BZIP2/XZ):" Argucase $argu ingzip) tar-pzcf/backups/etc-' Date +%y%m%d '. Ta R.GZ/ETC;; bzip2) tar-pjcf/backups/etc-' Date +%y%m%d '. tar.bz2/etc; XZ) tar-pjcf/backups/etc-' Date +%y%m%d '. tar.xz/etc; *) echo "error compression tools";; Esac

3. Write a script that accepts a path parameter:

(1) If it is an ordinary document, it can be accessed normally;

(2) If it is a catalog file, then the CD command is available for it;

(3) If the file is a symbolic link, the description is a access path;

(4) Other is impossible to judge;

For:

If [$#-lt 1];thenecho "Please input a URL" fiif [-L $];thenecho "This is a access URL" elif [-D $];thenecho "can us E CD common "Elif [-F $];thenecho" normal access "Elseecho" Unknow "fi

4, write a script, get the host name of the current host, judge

(1) If the host name is empty or localhost, or "(none)", it is named mail.magedu.com;

(2) Otherwise, display the existing host name can be;

For:

#!/bin/bashhostname= ' hostname ' if [$hostname = = Localhost-o $hostname = = None];thenhostname Mail.magedu.comelseecho $ho Stnamefi

5. Write a script to complete the following tasks:

(1) Sequentially copy each direct file or subdirectory in the/var/log directory to the/TMP/TEST1-TESTN directory, respectively;

(2) The Cp-r command is used only when copying the directory;

(3) use CP command when copying files;

(4) Use the cp-d command when copying a linked file;

(5) All remaining types, using the CP-A command;

For:

#!/bin/bashmkdir /tmp/test1-testnpath= "/TMP/TEST1-TESTN" for  file in /var/log/*;d o        if [ -d$file ];  then                cp-r   $file   $path         elif [ -l$file ];then                 cp-d $ file  $path         elif [ -f$file ];then                 cp$file  $path         else                 cp-a  $file   $path          fidone 

6. Please describe in detail the start-up process of the CentOS system (detailed to what each process system does)

For:

The CentOS host is started in the following order (1) post      power-on self-Test          (2) bios      read the BIOS setup parameters in the CMOS to identify the underlying hardware, find the boot device           (3) mbr           (1) Read the first 446 bytes of bootloader  in the boot device MBR                          (2) the sector after reading the MBR is used to identify the area where grub and kernel kernel are located                           (3) Launch grub         (4) grub      Display Menu interface, select Run kernel kernel ; The configuration file is/boot/grub/grub.conf                          (1)   provides a menu, and provides interactive interface                       (2)   load user-selected kernel or operating system                       (3)   provides a protection mechanism for menus      (5) kernel    itself initialization                           (1 ) detects all the hardware devices that can be identified;                      (2) Load the hardware driver (possibly with RAMDisk load driver)                       (3) Mount the root file system as read-only;                      (4) The first application running user space:/sbin/init     (6) init      run/sbin/init program, configuration file/etc/ Inittab and/ETC/INIT/*.CONF                     Set default RunLevel      such as:id:3:initdefault:              run System initial scripts      such as:si::sysinit:/etc/rc.d/rc.sysinit                  (1)   Set host name;                      (2)   Set Welcome information;                      (3)   Activate Udev and selinux;                       (4)   mount the file system defined in the/etc/fstab file;                      (5   Detect the root file system, and re-mount the root file system in read-write mode;                      (6)   Set system clock;                      (7)   Activate swap device;                      (8)   set kernel parameters according to/etc/sysctl.conf file;                       (9)   activating LVM and Software raid equipment;                      (Ten)   load drivers for additional devices;                      (one)   cleanup operations ;                 Close the service that needs to be shut down in the corresponding script, start the service that needs to start (the actual service command is located in/ETC/RC.D/INIT.D)                          l0:0:wait:/etc/rc.d/rc 0                     l1:1:wait:/etc/rc.d/ rc 1                     ...                     l6:6:wait:/etc/rc.d/rc 6                  set up a login terminal                            tty1:2345:respawn:/usr/sbin/mingetty tty1                     tty2:2345:respawn:/usr/sbin/mingetty tty2                     ...                     tty6:2345: Respawn:/usr/sbin/mingetty tty6

7. Add a new piece of hardware to the CentOS 6 running on the virtual machine, providing two primary partitions;

(1) Create a new two primary partition for the hard disk, and install grub for it;

(2) Provide kernel and RAMDisk files for the first primary partition of the hard disk, and provide rootfs for the second partition;

(3) for Rootfs to provide bash, LS, cat programs and the library files that depend on;

(4) Provide the configuration file for grub;

(5) The new hard disk is set as the first boot item and can start the target host normally;

For:

Add a piece of hard disk ~]# fdisk /dev/sdb                                                 // The new drive SDB is divided into 2 main zones ~]# mke2fs -t ext4 /dev/sdb{1,2}                                  //Formatting Partitions ~]# mount /dev/sdb1 /mnt                                            //mount partition 1 to/mnt directory ~]# grub-install --root-directory=/mnt /dev/sdb                   //installing grub to partition 1 on ~]# cp /boot/ initramfs-2.6.32-504.el6.i686.img /mnt/initramfs    //Copying kernel files ~]# cp /boot/ vmlinuz-2.6.32-504.el6.i686 /mnt/vmlinuz             //Copying RAMDisk files ~]# vim /mnt/boot/grub/grub.conf                                   //Creating grub.conf Files     default=0     TIMEOUT=5    TITLE CENTOS6 (Test)     root  (hd0,0)     kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash     initrd /initramfs~]# umount /dev/sdb1                                                //Unload Partition 1~]# mount  /dev/sdb2 /mnt                                            //Mount Partition 2~]# mkdir -p /mnt/{bin,sbin, Lib,lib64,etc,home,root,media,mnt,dev,tmp}~]# mkdir -p /mnt/{usr/{bin,sbin,lib,lib64},var{lib, lib64,log,local,cache},proc,sys,selinux}~]# cp /bin/{bash,ls,cat} /mnt/bin~]# cp  ' LDD  /bin/{bash,ls,cat}|grep -eo  "/lib.*[[:space:]" |    sort -u '  / mnt/lib                                               //copy lib file ~]# sync                                                             //Synchronous ~]# init 6                                                            //Restart the console after you enter BIOS settings   Adjust the hard drive boot order after saving exit.

8. Write a script

(1) can accept four parameters: Start, stop, restart, status

Start: Output "starting script name finished."

...

(2) Any other parameters, all error exits;

For:

#!/bin/bash#if [ $# -eq 1 ];then    case $1 in     start)         echo  "starting $0  Finished. "         ;;     stop)         echo  "stopping $0  Finished. "         ;;     restart)         echo  "restart $0  finished. "         ;;     status)         echo  "status $0  Finished. "         ;;     *)         echo  "Error 2"          exit 1        ;;     esacelse    echo  "Error 1" fi 

9, write a script to determine whether a given user is logged into the current system;

(1) If the login, the user login, the script terminates;

(2) Every 3 seconds, check whether the user is logged in;

For:

#!/bin/bash#if ID $ &>/dev/null && [$#-eq 1], then until W |grep "^$1\>" &>/dev/null; Do sleep 3 echo "seaching ..." Done with Echo "is online." else echo "UserId is error." Fi

10, write a script, display the user selected to view the information;

CPU) Display CPU Info

MEM) Display Memory info

Disk) Display Disk info

Quit) quit

Not these four options, the prompt error, and ask the user to re-select, only to give the correct choice;

For:

#!/bin/bash#cat << eofcpu) display CPU Infomem) display memory Infodisk) display disk infoquit) quit====== ========================eofread-p "Enter a option:" Optionuntil ["$option" = = ' cpu '-o ' $option "= =" Mem "-O" $option "    = = "Disk"-O "$option" = = "Quit"];d o read-p "wrong option, Enter again:" Optiondonecase "$option" in CPU) LSCPU ;; MEM) Cat/proc/meminfo;; disk) fdisk-l;; *) echo "Quit ..." exit 0;; Esac

11. Write a script

(1) Return a user's UID and shell with function implementation; The user name is passed through the parameters;

(2) Prompt the user to enter a user name or enter "quit" to exit;

When the user name is entered, the calling function displays the user information;

Exit the script when the user enters quit, and further: after displaying the user-related information you typed, remind the output user name or quit again:

For:

#!/bin/bash#function userInfo {uid= ' grep "^$1\>"/etc/passwd | cut-d:-f3 ' ushell= ' grep "^$1\>"/etc/passwd | cut-d :-f7 '}read-p ' Input a user name or quit: "Optionuntil [" $option "= =" Quit "];d o if ID $option &>/dev/null;th     En userInfo $option echo-e "User:\t$option\nuid:\t$uid\nshell:\t$ushell" Else echo "Id is wrong." Fi read-p "Input a user name or quit:" Optiondone

12, write a script to complete the following functions (using functions)

(1) Prompt the user to enter the name of an executable command; Get all the library files that this command relies on;

(2) Copy the command file to the corresponding Rootfs path in the/mnt/sysroot directory, for example, if the original path of the copied file is/usr/bin/useradd, it is copied to the/mnt/sysroot/usr/bin/directory;

(3) Copy the library files that this command relies on to the corresponding Rootfs path in the/mnt/sysroot directory, and the rules related to the above command;

For:

#!/bin/bashmkdir-p/mnt/sysrootdestpath= "/mnt/sysroot" read-p "Enter a command:" Commandwhich--skip-alias ${command} & Amp;>/dev/null[$-ne 0] && echo "command NotFound" && exit 1binary= ' which--skip-alias ${command} ' m Kdir-p ${destpath} ' dirname ${binary} ' cp--preserve ${binary} ${destpath}${binary}for lib in ' LDd ${binary} | awk ' (nr>1) {print$ (NF-1)} ';d o mkdir-p${destpath} ' dirname ${lib} ' cp--preserve ${lib} ${destpath}${lib}d One


This article from "The Boundless" blog, declined reprint!

Linux+python high-end operation and maintenance class fifth time operation

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.