python123第五周作业答案_马哥2016全新Linux+Python高端运维班第五周作业
本周作業內容:
1、顯示當前系統上root、fedora或user1用戶的默認shell;# 沒有fedora、user1用戶,添加fadora,user1模擬環境
[root@localhost ~]# useradd -s /sbin/nologin fedora && useradd -s /bin/sh user1
# 添加fadora、user1用戶,并指定不同的默認shell
[root@localhost ~]# getent passwd fedora
fedora:x:1001:1001::/home/fedora:/sbin/nologin
[root@localhost ~]# getent passwd user1
user1:x:1002:1002::/home/user1:/bin/sh
# 過濾/etc/passwd中root、fedora或user1用戶,切割顯示其對應的默認shell
[root@localhost ~]# egrep "^(root|fedora|user1)" /etc/passwd | cut -d: -f7
/bin/bash
/sbin/nologin
/bin/sh
2、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello();# 過濾每行中,匹配所有字母,然后跟一組小括號的行
# 使用egrep擴展正則的話,()需要轉義
[root@localhost ~]# egrep -o "[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
# 或者
[root@localhost ~]# grep -o "[[:alpha:]]\+()" /etc/rc.d/init.d/functions
3、使用echo命令輸出一個絕對路徑,使用grep取出其基名;# 匹配基名,就是最后的/后的文件名,使用-o選項精確匹配,匹配[^/]結尾的,
[root@localhost ~]# echo "/etc/sysconfig/network-scripts/ifcfg-eth0" | grep -o '[^/]\+$'
ifcfg-eth0
擴展:取出其路徑名# 匹配路徑名,使用-o選項精確匹配,匹配最后一個/前的內容
[root@localhost ~]# echo "/etc/sysconfig/network-scripts/ifcfg-eth0" | grep -o '^/.*/'
/etc/sysconfig/network-scripts/
# 然后把最后的/也去掉
[root@localhost ~]# echo "/etc/sysconfig/network-scripts/ifcfg-eth0" | grep -o '^/.*/' | grep -o '^/.*[^/]'
/etc/sysconfig/network-scripts
4、找出ifconfig命令結果中的1-255之間數字;# 找出1-255之間數字,使用-o選項精確匹配,[1-9]找出1-9,[1-9][0-9]找出10-99,1[0-9]{2}找出100-199,2[0-5]{2}找出200-255
# 排序,去重
[root@localhost ~]# # ifconfig | egrep -o '\b(2[0-5]{2}|1[0-9]{2}|[1-9][0-9]|[1-9])\b' | sort -n -u
5、挑戰題:寫一個模式,能匹配合理的IP地址;# A類地址范圍:1.0.0.1—126.255.255.254 B類地址范圍:128.0.0.1—191.255.255.254
# C類地址范圍:192.0.0.1—223.255.255.254 D類地址范圍:224.0.0.1—239.255.255.254
# E類地址范圍:240.0.0.1—255.255.255.254 127.X.X.X是保留地址,用做循環測試用的
# 匹配范圍為 1-255.0-255.0-255.1-254
# ifconfig | egrep -o "(2[0-5]{2}|1[0-9]{2}|[1-9][0-9]|[1-9])\.\
((2[0-5]{2}|1[0-9]{2}|[1-9][0-9]|[0-9])\.){2}\
\<(2[0-5][0-4]|1[0-9]{2}|[1-9][0-9]|[1-9])\>"
6、挑戰題:寫一個模式,能匹配出所有的郵件地址;# 郵件地址,匹配用戶名@郵件服務器,用戶名可以是數字或字母,服務器名是字母或者數字,頂級域名是字母
# 添加一些測試用的郵件地址
[root@localhost ~]# cat > /tmp/test_mail.txt << EOF
123456@qq.com
12345678@456.com
123456te@123.net
123983@163.cn
12cd@123.com
EFgh@qq.com
EOF
# egrep -o "[[:alnum:]]+@[[:alnum:]]+\.[[:alpha:]]+"
[root@localhost ~]# egrep -o "[[:alnum:]]+@[[:alnum:]]+\.[[:alpha:]]+" /tmp/test_mail.txt
7、查找/var目錄下屬主為root,且屬組為mail的所有文件或目錄;# find /var -user root -group mail
[root@localhost var]# find /var -user root -group mail -ls
3153 0 drwxrwxr-x 2 root mail 45 Sep 2 09:33 /var/spool/mail
8、查找當前系統上沒有屬主或屬組的文件;# find / \( -nouser -o -nogroup \)
# 新建幾個測試文件,直觀比較
[root@localhost ~]# useradd user2
[root@localhost ~]# su - user2
[user2@localhost ~]$ cd /tmp
[user2@localhost tmp]$ touch test{1..3}.txt
[root@localhost tmp]# chown root test2.txt
[root@localhost tmp]# chown .root test3.txt
[root@localhost tmp]# userdel -r user2
# 顯示部分內容
[root@localhost tmp]# find / \( -nouser -o -nogroup \) -ls | head
進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄;# find / \( -nouser -o -nogroup \) -atime -3
# 顯示部分內容
[root@localhost tmp]# find / \( -nouser -o -nogroup \) -atime -3 -ls | head
9、查找/etc目錄下所有用戶都有寫權限的文件;# 所有用戶同時滿足寫權限,使用-222
# find /etc/ -perm -222
10、查找/etc目錄下大于1M,且類型為普通文件的所有文件;# find /etc/ -size +1M -type f
11、查找/etc/init.d/目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件;# find /etc/init.d -perm -113
[root@localhost tmp]# find /etc/init.d -perm -113 -ls
33709906 0 lrwxrwxrwx 1 root root 11 Aug 30 09:39 /etc/init.d -> rc.d/init.d
12、查找/usr目錄下不屬于root、bin或hadoop的文件;# find /usr ! \( -user root -o -user bin -o -user hadoop \)
或者
# find /usr ! -user root ! -user bin ! -user hadoop
13、查找/etc/目錄下至少有一類用戶沒有寫權限的文件;# -222是所有用戶都有寫權限,使用!取反,就是除了所有用戶都有寫權限的文件,就是至少一類用戶是沒有寫權限
# find /etc ! -perm -222
14、查找/etc目錄下最近一周內其內容被修改過,且不屬于root或hadoop的文件;# find /etc -mtime -7 ! \( -user root -o -user hadoop \)
或者
# find /etc -mtime -7 ! -user root ! -user hadoop
總結
以上是生活随笔為你收集整理的python123第五周作业答案_马哥2016全新Linux+Python高端运维班第五周作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线代码托管平台 GitHub 增强 A
- 下一篇: mybatis的简单查询用语句吗_面试官