您的位置:首页 > 运维架构 > Linux

Linux+Python运维培训班第4期--马哥——第3次作业(20170212)

2017-02-09 21:01 495 查看
1、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
[root@Jady ~]# grep --color=auto'\<[[:alpha:]]\+\>()' /etc/rc.d/init.d/functions
checkpid()
{
daemon()
{
killproc()
{
pidfileofproc()
{
pidofproc()
{
status()
{
success()
{
failure()
{
passed()
{
warning()
{
action()
{
strstr()
{
confirm()
{

2、使用echo命令输出一个绝对路径,使用grep取出其基名;
[root@Jady ~]# echo '/etc/rc.d/init.d/functions' | grep--color=auto -E -o '[^/]+$'
functions
扩展:取出其路径名
[root@Jady ~]# echo '/etc/rc.d/init.d/functions' | grep--color=auto -o '^\/.*\/'
/etc/rc.d/init.d/

3、找出ifconfig命令结果中的1-255之间数字;
[root@Jady ~]# ifconfig | grep --color=auto -E -o'\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
29
192
168
20
4
192
168
20
255
255
255
255
64
1
8
6
127
1
255
1
128
1

4、查找当前系统上没有属主或属组的文件;
[root@Jady ~]# find / \( -nouser -o -nogroup \) -exec ls-lh {} \;
总用量 0
总用量 0
-rw-r--r--. 1 1005 distro 18 10月 16 2014/home/mandriva/.bash_logout
-rw-r--r--. 1 1005 distro 124 10月 16 2014/home/mandriva/.bashrc
-rw-r--r--. 1 1005 distro 176 10月 16 2014/home/mandriva/.bash_profile
总用量 4.0K
drwxr-xr-x. 2 1005 distro 1.0K 8月  18 2010 extensions
drwxr-xr-x. 2 1005 distro 1.0K 8月  18 2010 plugins
总用量 0
总用量 0
find: “/proc/3802/task/3802/fd/5”: 没有那个文件或目录
find: “/proc/3802/task/3802/fd/5”: 没有那个文件或目录
find: “/proc/3802/task/3802/fdinfo/5”: 没有那个文件或目录
find: “/proc/3802/task/3802/fdinfo/5”: 没有那个文件或目录
find: “/proc/3802/fd/5”: 没有那个文件或目录
find: “/proc/3802/fd/5”: 没有那个文件或目录
find: “/proc/3802/fdinfo/5”: 没有那个文件或目录
find: “/proc/3802/fdinfo/5”: 没有那个文件或目录
-rw-r-----. 1 27 27 0 8月   3 2016 /var/log/mysqld.log
总用量 0
-rw-rw----. 1 1005 mail 0 12月 2501:22 /var/spool/mail/mandriva
-rw-rw-r--. 1 root 3004 0 2月   7 23:12 /tmp/test1
-rw-rw-r--. 1 3004 root 0 2月   7 23:12 /tmp/test2
-rw-rw-r--. 1 3004 3004 0 2月   7 23:12 /tmp/test
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
[root@Jady ~]# find / \( -nouser -o -nogroup \) -a -atime-3 -exec ls -lh {} \;
总用量 0
总用量 0
总用量 4.0K
drwxr-xr-x. 2 1005 distro 1.0K 8月  18 2010 extensions
drwxr-xr-x. 2 1005 distro 1.0K 8月  18 2010 plugins
总用量 0
总用量 0
find: “/proc/3841/task/3841/fd/5”: 没有那个文件或目录
find: “/proc/3841/task/3841/fdinfo/5”: 没有那个文件或目录
find: “/proc/3841/fd/5”: 没有那个文件或目录
find: “/proc/3841/fdinfo/5”: 没有那个文件或目录
总用量 0
-rw-rw-r--. 1 root 3004 0 2月   7 23:12 /tmp/test1
-rw-rw-r--. 1 3004 root 0 2月   7 23:12 /tmp/test2
-rw-rw-r--. 1 3004 3004 0 2月   7 23:12 /tmp/test

5、查找/etc目录下大于1M,且类型为普通文件的所有文件;
[root@Jady ~]# find /etc -size +1M -type f -exec ls -lh{} \;
-rw-r--r--. 1 root root 2.1M 8月   3 2016/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
-rw-r--r--. 1 root root 7.8M 8月   3 2016/etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 7.8M 8月   3 2016/etc/selinux/targeted/policy/policy.24

6、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[root@Jady ~]# touch /etc/init.d/test ; chmod u+x,g+x,o+xw/etc/init.d/test ; find /etc/init.d/ -perm -113 -exec ls -lh {} \; ; rm -rf/etc/init.d/test
-rwxr-xrwx. 1 root root 0 2月   8 05:58 /etc/init.d/test

7、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
[root@Jady ~]# useradd jady ; useradd hadoop ; touch /etc/test; chown jady /etc/test ; echo 'test' > /etc/test ; find /etc/ \( -type f -a-mtime -7 -a -not \( -user root -o -user hadoop \) \) -exec ls -lh {} \; ; rm-rf /etc/test ; userdel -r jady ; userdel -r hadoop
-rw-r--r--. 1 jady root 5 2月   8 06:27 /etc/test

8、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
[root@Jady ~]# cp /etc/rc.d/rc.sysinit /tmp/ ; ll -h/tmp/rc.sysinit
-rwxr-xr-x. 1 root root 20K 2月   8 22:44 /tmp/rc.sysinit
[root@Jady ~]# vim /tmp/rc.sysinit
:%s@^[[:space:]]@#&@g

9、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
[root@Jady ~]# vim /tmp/rc.sysinit
:%s@^#[[:space:]]\+@@g

10、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
[root@Jady ~]# vim /etc/yum.repos.d/CentOS-Media.repo
:%s@enabled=0@enabled=1@g
:%s@gpgcheck=0@gpgcheck=1@g

11、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20161202
[root@Jady ~]# crontab -l
30 0 * * 2,4,6 [ -d /backup/messages ] ||/bin/mkdir -pv /backup/messages > /dev/null && /bin/cp /var/log/messages/backup/messages/messages\-$(/bin/date +\%Y\%m\%d)
[root@Jady ~]# ll -h /backup/messages/
总用量 260K
-rw-------. 1 root root 259K 2月   9 00:30 messages-20170209

12、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
[root@Jady ~]# crontab -l
50 */2 * * * [ -f /stats/memory ] ||/bin/mkdir -pv /stats/memory > /dev/null && /bin/grep --color=auto-i "^s" /proc/meminfo >> /stats/memory.txt
[root@Jady ~]# ll /stats/memory.txt
-rw-r--r--. 1 root root 196 2月   9 00:50 /stats/memory.txt

13、写一个脚本创建10用户user10-user19;密码同用户名;
[root@Jady ~]# vim useradd.sh
1 #!/bin/bash
2 # Program:
3 #       创建10个账号user10至user19,其密码同账号名;
4 # History:
5 #       2017-02-08      Jady    1.0
6
7 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
8 export PATH
9
10 # 1、非管理员账号创建账号时提醒退出;
11 if [ ! $UID -eq 0 ] ; then
12         echo "对不起,只有管理员账号方能创建用户!"
13         exit 1
14 fi
15
16 # 2、循环创建账号user10至user19;
17 for index in `seq 10 1 19` ; do
18         if id user$index &> /dev/null ; then
19                 read -p "user$index 账号在系统中已存在,是否删除重新创建(Y/N)?" username
20                 if [ $username = "Y" ] ; then
21                         userdel -r user$index
22                         if [ $? -eq 0 ] ; then
23                                 useradd user$index
24                                 if [ $? -eq 0 ] ; then
25                                         echo "user$index" | passwd --stdin user$index &> /dev/null
26                                         echo "原 user$index 账号连同家目录已删除,同时创建新 user$index 账号;"
27                                 fi
28                         fi
29                 else
30                         echo "保留原 user$index 账号信息;"
31                 fi
32         else
33                 useradd user$index
34                 if [ $? -eq 0 ] ; then
35                         echo "user$index" | passwd --stdin user$index &> /dev/null
36                         echo "user$index 账号创建成功!"
37                 fi
38         fi
39 done
[root@Jady ~]# bash -n useradd.sh
[root@Jady ~]# useradd user10 ; useradd user18 ; bash useradd.sh
user10 账号在系统中已存在,是否删除重新创建(Y/N)?N
保留原 user10 账号信息;
user11 账号创建成功!
user12 账号创建成功!
user13 账号创建成功!
user14 账号创建成功!
user15 账号创建成功!
user16 账号创建成功!
user17 账号创建成功!
user18 账号在系统中已存在,是否删除重新创建(Y/N)?Y
原 user18 账号连同家目录已删除,同时创建新 user18 账号;
user19 账号创建成功!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux