您好,欢迎来到小侦探旅游网。
搜索
您的当前位置:首页使用Linux+Python高端运维班作业记录

使用Linux+Python高端运维班作业记录

来源:小侦探旅游网

本次作业内容:

1、写一个脚本,完成如下功能

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

(2) 如果存在,则显示此设备上的所有分区信息;

答:

#!/bin/bash
#
if [ $# -ne 1 ]; then
echo "Please enter a disk device."
exit 2
fi
if [ -b $1 ]; then 
fdisk -l $1
else
echo "this disk device is not exist."
fi

2、写一个脚本,完成如下功能

传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;

(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;

(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;

(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;

(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

答:

#!/bin/bash
#
[ -d /backups ] || mkdir /backups
read -p "pelase input a argu(gzip/bzip2/xz):" argu
 
case $argu in
gzip)
tar -Pzcf /backups/etc-`date +%Y%m%d`.tar.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、写一个脚本,接受一个路径参数:

(1) 如果为普通文件,则说明其可被正常访问;

(2) 如果是目录文件,则说明可对其使用cd命令;

(3) 如果为符号链接文件,则说明是个访问路径;

(4) 其它为无法判断;

答:

if [ $# -lt 1 ];then
echo "please input a url"
fi
if [ -L $1 ];then
echo "this is a access url"
elif [ -d $1 ];then
echo "can use cd common"
elif [ -f $1 ];then
echo "normal access"
else
echo "unknow"
fi

4、写一个脚本,取得当前主机的主机名,判断

(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;

(2) 否则,显示现有的主机名即可;

答:

#!/bin/bash
hostname=`hostname`
if [ $hostname == localhost -o $hostname == none ];then
hostname mail.magedu.com
else
echo $hostname
fi

5、写一个脚本,完成如下任务 :

(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;

(2) 复制目录时,才使用cp -r命令;

(3) 复制文件时使用cp命令;

(4) 复制链接文件时使用cp -d命令;

(5) 余下的所有类型,使用cp -a命令;

答:

#!/bin/bash
mkdir /tmp/test1-testn
path="/tmp/test1-testn"
for file in /var/log/*;do
 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

6、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)

答:

CentOS主机按以下顺序启动
(1)POST 加电自检 
(2)BIOS 读取CMOS中的BIOS设置的参数来识别基础硬件,寻找到启动设备 
(3)MBR (1)读取启动设备MBR中前446字节的bootloader 
 (2)读取MBR后的扇区用来识别grub以及内核kernel所在的区域 
 (3)启动grub 
(4)GRUB 显示菜单界面,选择运行内核kernel;配置文件是/boot/grub/grub.conf 
 (1) 提供菜单、并提供交互式接口 
 (2) 加载用户选择的内核或操作系统 
 (3) 为菜单提供了保护机制 
(5)KERNEL 自身初始化 
 (1)探测可识别到的所有硬件设备; 
 (2)加载硬件驱动程序;(有可能会借助于ramdisk加载驱动) 
 (3)以只读方式挂载根文件系统; 
 (4)运行用户空间的第一个应用程序:/sbin/init 
(6)INIT 运行/sbin/init程序,配置文件/etc/inittab和/etc/init/*.conf 
 设置默认运行级别 如:id:3:initdefault:
 运行系统初始脚本 如:si::sysinit:/etc/rc.d/rc.sysinit
 (1) 设置主机名; 
 (2) 设置欢迎信息; 
 (3) 激活udev和selinux; 
 (4) 挂载/etc/fstab文件中定义的文件系统; 
 (5) 检测根文件系统,并以读写方式重新挂载根文件系统; 
 (6) 设置系统时钟; 
 (7) 激活swap设备; 
 (8) 根据/etc/sysctl.conf文件设置内核参数; 
 (9) 激活lvm及software raid设备; 
 (10) 加载额外设备的驱动程序; 
 (11) 清理操作; 
 关闭对应的脚本中需要关闭的服务,启动需要启动服务(实际服务命令位于/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 
 设置登录终端 
 tty1:2345:respawn:/usr/sbin/mingetty tty1 
 tty2:2345:respawn:/usr/sbin/mingetty tty2 
 ... 
 tty6:2345:respawn:/usr/sbin/mingetty tty6

7、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;

(1) 为硬盘新建两个主分区;并为其安装grub;

(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;

(3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;

(4) 为grub提供配置文件;

(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;

答:

添加一块硬盘
~]# fdisk /dev/sdb //将新硬盘sdb分为2个主区
~]# mke2fs -t ext4 /dev/sdb{1,2} //格式化分区
~]# mount /dev/sdb1 /mnt //挂载分区1到/mnt目录
~]# grub-install --root-directory=/mnt /dev/sdb //安装grub到分区1上
~]# cp /boot/initramfs-2.6.32-504.el6.i686.img /mnt/initramfs //复制内核文件
~]# cp /boot/vmlinuz-2.6.32-504.el6.i686 /mnt/vmlinuz //复制ramdisk文件
~]# vim /mnt/boot/grub/grub.conf //创建grub.conf文件
 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 //卸载分区1
~]# mount /dev/sdb2 /mnt //挂载分区2
~]# mkdir -p /mnt/{bin,sbin,lib,lib,etc,home,root,media,mnt,dev,tmp}
~]# mkdir -p /mnt/{usr/{bin,sbin,lib,lib},var{lib,lib,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 //复制lib文件
~]# sync //同步
~]# init 6 //重启主机
重启后进入bios设置 调整硬盘启动顺序后保存退出。

8、写一个脚本

(1) 能接受四个参数:start, stop, restart, status

start: 输出“starting 脚本名 finished.”

...

(2) 其它任意参数,均报错退出;

答:

#!/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
 ;;
 esac
else
 echo "error 1"
fi

9、写一个脚本,判断给定的用户是否登录了当前系统;

(1) 如果登录了,则显示用户登录,脚本终止;

(2) 每3秒钟,查看一次用户是否登录;

答:

#!/bin/bash
#
if id $1 &>/dev/null && [ $# -eq 1 ] ;then 
 until w |grep "^$1>" &>/dev/null;do
 sleep 3
 echo "seaching..." 
 done
 echo "$1 is online."
else
 echo "UserId is error."
fi

10、写一个脚本,显示用户选定要查看的信息;

cpu) display cpu info

mem) display memory info

disk) display disk info

quit) quit

非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

答:

#!/bin/bash
#
cat << EOF
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
==============================
EOF
read -p "Enter a option: " option
until [ "$option" == 'cpu' -o "$option" == "mem" -o "$option" == "disk" -o "$option" == "quit" ];do
 read -p "Wrong option, Enter again: " option
done
 
case "$option" in 
cpu)
 lscpu
 ;;
mem)
 cat /proc/meminfo
 ;;
disk)
 fdisk -l
 ;;
*)
 echo "Quit..."
 exit 0
 ;;
esac

11、写一个脚本

(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

(2) 提示用户输入一个用户名或输入“quit”退出;

当输入的是用户名,则调用函数显示用户信息;

当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:

答:

#!/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: " option
until [ "$option" == "quit" ];do
 if id $option &>/dev/null;then
 userInfo $option
 echo -e "User:	$option
UID:	$uId
SHELL:	$uShell"
 else
 echo "Id is wrong."
 fi
 read -p "Input a user name or quit: " option
done

12、写一个脚本,完成如下功能(使用函数)

(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;

(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

答:

#!/bin/bash
mkdir -p /mnt/sysroot
destPath="/mnt/sysroot"
read -p "enter a command: " command
which --skip-alias ${command} &> /dev/null
[ $? -ne 0 ] && echo "command notfound" && exit 1
binary=`which --skip-alias ${command}`
mkdir -p ${destPath}`dirname ${binary}`
cp --preserve ${binary} ${destPath}${binary}
for lib in `ldd ${binary} | awk ‘(NR>1){print$(NF-1)}‘`;do
 mkdir -p${destPath}`dirname ${lib}`
 cp--preserve ${lib} ${destPath}${lib}
done

Copyright © 2019- xiaozhentang.com 版权所有 湘ICP备2023022495号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务