第五周作业内容:

1、显示当前系统上root、fedora或user1用户的默认shell;

# nano ~/usershell.sh

#!/bin/bash
#

for userid in {root,fedora,user1};do
    id $userid &> /dev/null || useradd $userid && grep "^$userid" /etc/passwd | cut -d: -f1,7
done

# bash -n ~/usershell.sh
# bash -x ~/usershell.sh
# chmod +x !$
# bash !$

root:/bin/bash

fedora:/bin/bash

user1:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();

# grep "\<[[:alpha:]]\+\>()" /etc/rc.d/init.d/functions

checkpid() {

daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

3、使用echo命令输出一个绝对路径,使用grep取出其基名; 扩展:取出其路径名

# echo "/etc/rc.d/init.d/functions" | grep -o "[^/]\+$"
# echo "/etc/rc.d/init.d/functions/" | grep -o "[^/]\+/\?$" | tr -d '/'

functions

# echo "/etc/rc.d/init.d/functions" | grep  -o "/.*/"
# echo "/etc/rc.d/init.d/functions/" | cut -d'/' -f1,2,3,4

/etc/rc.d/init.d

4、找出ifconfig命令结果中的1-255之间数字;

# ifconfig | grep -E -o --color '\b(2[0-4][0-9])\b|\b(25[0-5])\b|\b(1[0-9][0-9])\b|\b([1-9][0-9])\b|\b([1-9])\b'

29
83
192
168
1
102
192
168
1
255
255
255
255
64
1
1
127
1
255
1
128
1
4
4
240
240
240
240

5、挑战题:写一个模式,能匹配合理的IP地址;

扩展正规表达式的模式:

"\b(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-3][0-9]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-4]))\b"

示例:

# ifconfig | grep -E  --color "\b(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-3][0-9]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-4]))\b"

          inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0

          inet addr:127.0.0.1  Mask:255.0.0.0

说明:排除了主机位全1(广播地址)和主机全0(网络位)

6、挑战题:写一个模式,能匹配出所有的邮件地址;

扩展正规表达式的模式:

'^([[:alnum:]]{0,}[-_]?[[:alnum:]]{0,})@(([[:alnum:]]{0,}[-_]?[[:alnum:]]{0,})\.){1,5}([[:alnum:]]{0,}[-_]?[[:alnum:]]{0,})$'

7、查找/var目录下属主为root,且属组为mail的所有文件或目录;

# find /var -user root -group mail

/var/spool/mail

8、查找当前系统上没有属主或属组的文件;

    进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;

# find / -nouser -o -nogroup -atime -3 | ls -l

-rw-rw----. 1 admin     mail 0 7月  31 23:39 admin

-rw-rw----. 1 asd       mail 0 8月  24 21:30 asd
-rw-rw----. 1 bash      mail 0 8月  25 12:07 bash
-rw-rw----. 1 basher    mail 0 8月  25 12:07 basher
-rw-rw----. 1 fedora    mail 0 9月   1 07:57 fedora
-rw-rw----. 1 gdm       mail 0 8月  24 20:37 gdm
-rw-rw----. 1 gentoo    mail 0 8月  24 21:36 gentoo
-rw-rw----. 1 hadoop    mail 0 8月  21 21:37 hadoop
-rw-rw----. 1 lll       mail 0 8月  24 21:31 lll
-rw-rw----. 1 mageia    mail 0 8月  21 18:40 mageia
-rw-rw----. 1 nologin   mail 0 8月  25 12:07 nologin
-rw-rw----. 1 openstack mail 0 8月  21 18:53 openstack
-rw-rw----. 1 rpc       mail 0 7月  31 22:22 rpc
-rw-rw----. 1 slackware mail 0 8月  21 18:48 slackware
-rw-rw----. 1 testbash  mail 0 8月  25 12:07 testbash
-rw-rw----. 1 user1     mail 0 9月   1 07:57 user1

9、查找/etc目录下所有用户都有写权限的文件;

# find /etc -perm /222

10、查找/etc目录下大于1M,且类型为普通文件的所有文件;

# find /etc -type f -size +1M

/etc/gconf/gconf.xml.defaults/%gconf-tree.xml

/etc/selinux/targeted/modules/active/policy.kern
/etc/selinux/targeted/policy/policy.24

11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;

# find /etc/init.d -perm /113

/etc/init.d

12、查找/usr目录下不属于root、bin或hadoop的文件;

# find /usr -not \( -user root -o -user bin -o -user hadoop \)
# find /usr -not -user root -a -not -user bin -a -not -user hadoop

/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

13、查找/etc/目录下至少有一类用户没有写权限的文件;

# find /etc -not -perm /222

/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem

/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/java/cacerts
/etc/shadow-
/etc/shadow
/etc/pam.d/cups
/etc/openldap/certs/password
/etc/gshadow
/etc/gshadow-
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/thin-performance.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/ld.so.conf.d/kernel-2.6.32-642.el6.x86_64.conf
/etc/dbus-1/system.d/cups.conf
/etc/rc.d/init.d/blk-availability
/etc/rc.d/init.d/lvm2-lvmetad
/etc/rc.d/init.d/lvm2-monitor
/etc/sudoers

14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;

# find /etc -mtime -7 -not -user root -a -not -user hadoop
# find /etc -mtime -7 -not \( -user root -user hadoop \)

/etc/aa.txt