第五周博客作业

  感觉进入了年底,明显的时间不够,不管怎么说,还是要努力跟上学习的进度,不能给自己松懈找借口!
  1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

[root@localhost ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf
    root (hd0,0)
    kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swapSYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-431.el6.x86_64.img

  2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

[root@localhost ~]# grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.sysinit 
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.
# Initialize hardware
# Set default affinity
...

  3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:35227               0.0.0.0:*                   LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::46560                    :::*                        LISTEN 

  4、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s /sbin/nologin nologin
[root@localhost ~]# grep "^\([^:]\+\>\).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:500:500::/home/bash:/bin/bash
nologin:x:503:503::/home/nologin:/sbin/nologin

  5、显示当前系统上root、fedora或user1用户的默认shell;
  因为当前系统没有fedora和user1用户,所以先创建。

[root@localhost ~]# useradd fedora
[root@localhost ~]# useradd user1
[root@localhost ~]# grep "^\(root\|fedora\|user1\)" /etc/passwd
root:x:0:0:root:/root:/bin/bash
fedora:x:504:504::/home/fedora:/bin/bash
user1:x:505:505::/home/user1:/bin/bash
[root@localhost ~]# grep "^\(root\|fedora\|user1\)" /etc/passwd | cut -d: -f7
/bin/bash
/bin/bash
/bin/bash
···

  6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
  根据对题目的理解,应该是要小括号内为空的内容,也就是一对括号,而不是括号内可以有内容的行。

[root@localhost ~]# grep "\<[[:alnum:]]\+\>()" /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

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

[root@localhost ~]# echo /etc/rc.d/init.d/functions | grep "\<[^/]\+$" -o
functions

[root@localhost ~]# echo /etc/rc.d/init.d/functions | grep "/.*/" -o
/etc/rc.d/init.d/

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

[root@localhost ~]# ifconfig | grep "\<[1-9]\>\|\<[1-9][0-9]\>\|\<1[0-9][0-9]\>\|2[0-4][0-9]\|25[0-5]" -o
29
172
16
209
172
16
3
255
255
...

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

[root@localhost ~]# ifconfig | grep "\(\([01]\?[0-9]\?[0-9]\?\|2\?[0-4]\?[0-9]\?\|25\?[0-5]\?\)\.\)\{3\}\([01]\?[0-9]\?[0-9]\?\|2\?[0-4]\?[0-9]\?\|25\?[0-5]\?\)" -o
172.16.0.209
172.16.3.255
255.255.252.0
127.0.0.1
255.0.0.0

  10、挑战题:写一个模式,能匹配出所有的邮件地址;
  不是太懂email的格式要求,但是一般字符只能用-和_所以就只允许这两个了。

[root@localhost ~]# cat /tmp/mail.txt 
[email protected]
[email protected]
[email protected]
[email protected]
bcd2@bc_2.net
[email protected]
cd#@cs.cc
ty%@.com
mc^[email protected]
[email protected]
ss*@dds(.com

[root@localhost ~]# grep "^[0-9a-zA-Z\-\_]\+@\{1\}[0-9a-zA-Z\-\_]\+.[0-9a-zA-Z\-\_]\+$" /tmp/mail.txt
[email protected]
[email protected]
[email protected]
[email protected]
bcd2@bc_2.net

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

[root@localhost ~]# find /var -user root -group mail -ls
2229008    4 drwxrwxr-x   2 root     mail         4096 Jan  3 02:06 /var/spool/mail

  12、查找当前系统上没有属主或属组的文件;
  进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
  #由于系统上没有无属主或属组的文件,所以先做一些类似的文件出来。

[root@localhost ~]# chown user1:fedora /tmp/mail.txt 
[root@localhost ~]# cp /etc/issue /tmp
[root@localhost ~]# cp /etc/rc.d/init.d/functions /tmp
[root@localhost ~]# chown testbash:mail /tmp/issue 
[root@localhost ~]# chown basher:fedora /tmp/functions 
[root@localhost ~]# ll /tmp
total 28
-rw-r--r--. 1 basher   fedora 18586 Jan  3 04:27 functions
-rw-r--r--. 1 testbash mail      47 Jan  3 04:27 issue
-rw-r--r--. 1 user1    fedora   138 Jan  3 03:49 mail.txt
[root@localhost ~]# userdel basher
[root@localhost ~]# userdel testbash
[root@localhost ~]# userdel fedora
[root@localhost ~]# userdel user1
[root@localhost ~]# ll /tmp
total 28
-rw-r--r--. 1 502  504 18586 Jan  3 04:27 functions
-rw-r--r--. 1 501 mail    47 Jan  3 04:27 issue
-rw-r--r--. 1 505  504   138 Jan  3 03:49 mail.txt
[root@localhost ~]# find / \( -nouser -o -nogroup \) -ls
find: `/proc/2941/task/2941/fd/5': No such file or directory
find: `/proc/2941/task/2941/fd/5': No such file or directory
find: `/proc/2941/task/2941/fdinfo/5': No such file or directory
find: `/proc/2941/task/2941/fdinfo/5': No such file or directory
find: `/proc/2941/fd/5': No such file or directory
find: `/proc/2941/fd/5': No such file or directory
find: `/proc/2941/fdinfo/5': No such file or directory
find: `/proc/2941/fdinfo/5': No such file or directory
3932161    4 drwx------   2 504      504          4096 Jan  3 02:06 /home/fedora
3932162    4 -rw-r--r--   1 504      504            18 Jul 18  2013 /home/fedora/.bash_logout
3932164    4 -rw-r--r--   1 504      504           124 Jul 18  2013 /home/fedora/.bashrc
3932163    4 -rw-r--r--   1 504      504           176 Jul 18  2013 /home/fedora/.bash_profile
131073    4 drwx------   2 505      505          4096 Jan  3 02:06 /home/user1
131074    4 -rw-r--r--   1 505      505            18 Jul 18  2013 /home/user1/.bash_logout
131076    4 -rw-r--r--   1 505      505           124 Jul 18  2013 /home/user1/.bashrc
131075    4 -rw-r--r--   1 505      505           176 Jul 18  2013 /home/user1/.bash_profile
655361    4 drwx------   2 502      502          4096 Jan  3 01:36 /home/basher
655362    4 -rw-r--r--   1 502      502            18 Jul 18  2013 /home/basher/.bash_logout
655364    4 -rw-r--r--   1 502      502           124 Jul 18  2013 /home/basher/.bashrc
655363    4 -rw-r--r--   1 502      502           176 Jul 18  2013 /home/basher/.bash_profile
524289    4 drwx------   2 501      501          4096 Jan  3 01:36 /home/testbash
524290    4 -rw-r--r--   1 501      501            18 Jul 18  2013 /home/testbash/.bash_logout
524292    4 -rw-r--r--   1 501      501           124 Jul 18  2013 /home/testbash/.bashrc
524291    4 -rw-r--r--   1 501      501           176 Jul 18  2013 /home/testbash/.bash_profile
2230209    0 -rw-rw----   1 505      mail            0 Jan  3 02:06 /var/spool/mail/user1
2230206    0 -rw-rw----   1 502      mail            0 Jan  3 01:36 /var/spool/mail/basher
2230208    0 -rw-rw----   1 504      mail            0 Jan  3 02:06 /var/spool/mail/fedora
2230193    0 -rw-rw----   1 501      mail            0 Jan  3 01:36 /var/spool/mail/testbash
2752517   20 -rw-r--r--   1 502      504         18586 Jan  3 04:27 /tmp/functions
2752514    4 -rw-r--r--   1 501      mail           47 Jan  3 04:27 /tmp/issue
2752516    4 -rw-r--r--   1 505      504           138 Jan  3 03:49 /tmp/mail.txt


[root@localhost ~]# find / \( -nouser -o -nogroup \) -a -atime -3 -ls
find: `/proc/2927/task/2927/fd/5': No such file or directory
find: `/proc/2927/task/2927/fdinfo/5': No such file or directory
find: `/proc/2927/fd/5': No such file or directory
find: `/proc/2927/fdinfo/5': No such file or directory
3932161    4 drwx------   2 504      504          4096 Jan  3 02:06 /home/fedora
3932162    4 -rw-r--r--   1 504      504            18 Jul 18  2013 /home/fedora/.bash_logout
3932164    4 -rw-r--r--   1 504      504           124 Jul 18  2013 /home/fedora/.bashrc
3932163    4 -rw-r--r--   1 504      504           176 Jul 18  2013 /home/fedora/.bash_profile
131073    4 drwx------   2 505      505          4096 Jan  3 02:06 /home/user1
131074    4 -rw-r--r--   1 505      505            18 Jul 18  2013 /home/user1/.bash_logout
131076    4 -rw-r--r--   1 505      505           124 Jul 18  2013 /home/user1/.bashrc
131075    4 -rw-r--r--   1 505      505           176 Jul 18  2013 /home/user1/.bash_profile
655361    4 drwx------   2 502      502          4096 Jan  3 01:36 /home/basher
655362    4 -rw-r--r--   1 502      502            18 Jul 18  2013 /home/basher/.bash_logout
655364    4 -rw-r--r--   1 502      502           124 Jul 18  2013 /home/basher/.bashrc
655363    4 -rw-r--r--   1 502      502           176 Jul 18  2013 /home/basher/.bash_profile
524289    4 drwx------   2 501      501          4096 Jan  3 01:36 /home/testbash
524290    4 -rw-r--r--   1 501      501            18 Jul 18  2013 /home/testbash/.bash_logout
524292    4 -rw-r--r--   1 501      501           124 Jul 18  2013 /home/testbash/.bashrc
524291    4 -rw-r--r--   1 501      501           176 Jul 18  2013 /home/testbash/.bash_profile
2230209    0 -rw-rw----   1 505      mail            0 Jan  3 02:06 /var/spool/mail/user1
2230206    0 -rw-rw----   1 502      mail            0 Jan  3 01:36 /var/spool/mail/basher
2230208    0 -rw-rw----   1 504      mail            0 Jan  3 02:06 /var/spool/mail/fedora
2230193    0 -rw-rw----   1 501      mail            0 Jan  3 01:36 /var/spool/mail/testbash
2752517   20 -rw-r--r--   1 502      504         18586 Jan  3 04:27 /tmp/functions
2752514    4 -rw-r--r--   1 501      mail           47 Jan  3 04:27 /tmp/issue
2752516    4 -rw-r--r--   1 505      504           138 Jan  3 03:49 /tmp/mail.txt

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

[root@localhost ~]# find /etc -perm -222 -type f -ls
...

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

[root@localhost ~]# find /etc -type f -size +1M -ls
1181096 7124 -rw-r--r--   1 root     root      7292689 Dec 21 04:02 /etc/selinux/targeted/policy/policy.24
1181093 7124 -rw-r--r--   1 root     root      7292689 Dec 21 04:02 /etc/selinux/targeted/modules/active/policy.kern

  15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
  没有其他用户有写权限的文件,所以将查询改为读和执行。

[root@localhost ~]# find /etc/rc.d/init.d -perm -114 -type f -ls
1179729    4 drwxr-xr-x   2 root     root         4096 Jan  3 02:12 /etc/rc.d/init.d
1180321    4 -rwxr-xr-x   1 root     root          652 Oct 10  2013 /etc/rc.d/init.d/killall
1181236    4 -rwxr-xr-x   1 root     root         1725 Aug 19  2010 /etc/rc.d/init.d/acpid
1181220    4 -rwxr-xr-x   1 root     root         2034 Jun 13  2013 /etc/rc.d/init.d/quota_nld
1180644    4 -rwxr-xr-x   1 root     root         2094 Feb 22  2013 /etc/rc.d/init.d/certmonger
1181162    4 -r-xr-xr-x   1 root     root         2134 Nov 24  2013 /etc/rc.d/init.d/lvm2-lvmetad
1179849    4 -rwxr-xr-x   1 root     root         2023 Apr  3  2012 /etc/rc.d/init.d/portreserve
1180609    4 -rwxr-xr-x   1 root     root         2011 Aug 15  2013 /etc/rc.d/init.d/rsyslog
1181122    4 -rwxr-xr-x   1 root     root         1144 Nov 23  2013 /etc/rc.d/init.d/sysstat
...

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

[root@localhost ~]# find /usr -not \( -user root -o -user bin \) -ls
1314588   12 -rwsr-xr-x   1 abrt     abrt         9904 Nov 23  2013 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

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

[root@localhost ~]#  find /etc -perm /222 -type f -ls
1179706    4 -rw-r--r--   1 root     root          236 Nov 12  2010 /etc/sgml/docbook/xmlcatalog
1180292    4 -rw-r--r--   1 root     root           48 Dec 28 01:18 /etc/adjtime
1179780    0 -rw-r--r--   1 root     root            0 Nov 29  2012 /etc/odbc.ini
1179983    4 -rw-r--r--   1 root     root          251 Nov 23  2013 /etc/my.cnf
1180066    4 -rw-r--r--   1 root     root         2293 Apr  5  2012 /etc/libuser.conf
1179964   16 -rw-r--r--   1 root     root        13537 Dec 16  2004 /etc/java/font.properties
1179965    4 -rw-r--r--   1 root     root          684 Nov 12  2010 /etc/java/java.conf
1179966    4 -rw-r--r--   1 root     root           50 Nov 12  2010 /etc/java/jpackage-release
...

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

[root@localhost ~]# find /etc \( -not -user root -a -not -user bin \) -a -mtime -7

你可能感兴趣的:(第五周博客作业)