{"id":221708,"date":"2021-06-29T15:27:03","date_gmt":"2021-06-29T07:27:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221708"},"modified":"2021-06-29T15:27:03","modified_gmt":"2021-06-29T07:27:03","slug":"grep-exclude-func","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/grep-exclude-func.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528grep\u547d\u4ee4\u7684\u6392\u9664\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\ngrep \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u6587\u4ef6\u4e2d\u641c\u7d22\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u884c\uff0c\u5e76\u5c06\u5339\u914d\u7684\u884c\u6807\u51c6\u8f93\u51fa\u3002\u5728\u672c\u6587\u4e2d\u4ecb\u7ecd\u5982\u4f55\u5728\u4f7f\u7528 grep \u641c\u7d22\u65f6\u6392\u9664\u4e00\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u6216\u76ee\u5f55\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6392\u9664\u5355\u8bcd\u6216\u591a\u4e2a\u6761\u4ef6<\/strong><\/div>\n

\u8981\u4ec5\u663e\u793a\u4e0e\u641c\u7d22\u6a21\u5f0f\u4e0d\u5339\u914d\u7684\u884c\uff0c\u8bf7\u4f7f\u7528-v<\/code>\u9009\u9879\u3002\u4f8b\u5982\uff0c\u663e\u793a\u4e0d\u5305\u542bnologin<\/code>\u7684\u884c\uff0c\u4f7f\u7528\u4e0b\u9762\u547d\u4ee4\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -wv nologin \/etc\/passwd\r\nroot:x:0:0:root:\/root:\/bin\/bash\r\nsync:x:5:0:sync:\/sbin:\/bin\/sync\r\nshutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown\r\nhalt:x:7:0:halt:\/sbin:\/sbin\/halt\r\nbob:x:1000:1000::\/home\/bob:\/bin\/bash\r\nuser01:x:1001:1001::\/home\/user01:\/bin\/bash\r\npostgres:x:26:26:PostgreSQL Server:\/var\/lib\/pgsql:\/bin\/bash\r\n<\/pre>\n

\"\"
\n-w<\/code>\u9009\u9879\u544a\u8bc9 grep \u4ec5\u8fd4\u56de-w\u9009\u9879\u540e\u9762\u5305\u542b\u5355\u8bcd\u7684\u884c\u3002<\/p>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0bgrep \u533a\u5206\u5927\u5c0f\u5199\u3002\u8fd9\u610f\u5473\u7740\u5927\u5199\u548c\u5c0f\u5199\u5b57\u7b26\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u3002\u82e5\u8981\u5728\u641c\u7d22\u65f6\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u8bf7\u4f7f\u7528-i<\/code> \u9009\u9879\u8c03\u7528 grep\u3002<\/p>\n

\u5982\u679c\u641c\u7d22\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7a7a\u683c\uff0c\u5219\u9700\u8981\u5c06\u5185\u5bb9\u653e\u5728\u5355\u5f15\u53f7\u6216\u8005\u53cc\u5f15\u53f7\u5185\u3002<\/p>\n

\u82e5\u8981\u6392\u9664\u4e24\u4e2a\u6216\u591a\u4e2a\u641c\u7d22\u6761\u4ef6\uff0c\u8bf7\u4f7f\u7528-e<\/code>\u9009\u9879\uff0c\u53ef\u6839\u636e\u9700\u8981\u591a\u6b21\u4f7f\u7528-e<\/code>\u9009\u9879\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -wv -e nologin -e bash \/etc\/passwd\r\nsync:x:5:0:sync:\/sbin:\/bin\/sync\r\nshutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown\r\nhalt:x:7:0:halt:\/sbin:\/sbin\/halt\r\n<\/pre>\n

\"\"
\n\u6392\u9664\u591a\u4e2a\u641c\u7d22\u6761\u4ef6\u7684\u53e6\u4e00\u4e2a\u65b9\u5f0f\u662f\u4f7f\u7528\u8fd0\u7b97\u7b26 |<\/code>\uff0c\u4ee5\u4e0b\u793a\u4f8b\u6253\u5370\u4e0d\u5305\u542b\u5b57\u7b26\u4e32nologin<\/code>\u548cbash<\/code>\u7684\u884c\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -wv 'nologin\\|bash' \/etc\/passwd\r\nsync:x:5:0:sync:\/sbin:\/bin\/sync\r\nshutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown\r\nhalt:x:7:0:halt:\/sbin:\/sbin\/halt\r\n<\/pre>\n

\"\"
\n\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f7f\u7528\u9009\u9879 -E<\/code>\u4f1a\u540e\uff0c\u8fd0\u7b97\u7b26|<\/code>\u5c31\u4e0d\u9700\u8981\u88ab\u8f6c\u4e49\u4e86\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -Ewv 'nologin|bash' \/etc\/passwd\r\nsync:x:5:0:sync:\/sbin:\/bin\/sync\r\nshutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown\r\nhalt:x:7:0:halt:\/sbin:\/sbin\/halt\r\n<\/pre>\n

\"\"
\n\u4e0b\u9762\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u662f\u6392\u9664\/etc\/ssh\/sshd_config<\/code>\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u4ee5#<\/code>\u548c \u7a7a\u683c \u5f00\u5934\u7684\u884c\uff1a<\/p>\n

\r\n[root@localhost ~]# cat \/etc\/ssh\/sshd_config |grep -Ev '(^#|^$)'\r\n<\/pre>\n

\"\"
\n\u4e0b\u9762\u7684\u5b9e\u4f8b\uff0c\u8981\u6253\u5370\u51fa\u7cfb\u7edf\u4e0a\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u9664\u4e86\u4ee5\u201croot\u201d\u7528\u6237\u8eab\u4efd\u8fd0\u884c\u7684\u8fdb\u7a0b\uff1a<\/p>\n

\r\n[root@localhost ~]# ps -ef|grep -wv root\r\n<\/pre>\n

\"\"<\/p>\n

\u6392\u9664\u76ee\u5f55\u6216\u6587\u4ef6<\/strong><\/div>\n

\u8981\u4ecegrep\u641c\u7d22\u4e2d\u6392\u9664\u76ee\u5f55\uff0c\u8bf7\u4f7f\u7528--exclude-dir<\/code>\u9009\u9879\u3002\u6392\u9664\u76ee\u5f55\u7684\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u540e\u9762\u6307\u5b9a\u7684\u641c\u7d22\u76ee\u5f55\u3002<\/p>\n

\u4e0b\u9762\u5b9e\u4f8b\uff0c\u663e\u793a\u5982\u4f55\u5728\/etc\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u4e2d\u641c\u7d22\u5355\u8bcdkeys<\/code>\uff0c\u4e0d\u5305\u62ec \/etc\/pki \u76ee\u5f55\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -Rw --exclude-dir=pki keys \/etc\/\r\n<\/pre>\n

\"\"
\n\u8981\u6392\u9664\u591a\u4e2a\u76ee\u5f55\uff0c\u8bf7\u5c06\u6392\u9664\u7684\u76ee\u5f55\u62ec\u5728\u5927\u62ec\u53f7\u4e2d\uff0c\u5e76\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff0c\u4e0d\u8981\u6709\u7a7a\u683c\u3002<\/p>\n

\u4f8b\u5982\uff0c\u8981\u5728\u7cfb\u7edf\u4e2d\u67e5\u627e\u5305\u542b\u5b57\u7b26\u4e32 gnu<\/code> \u7684\u6587\u4ef6\uff0c\u4e0d\u5305\u62ec proc\u3001boot \u548c sys \u76ee\u5f55\uff0c\u9700\u8981\u8fd0\u884c\u4e0b\u9762\u547d\u4ee4\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -r --exclude-dir={proc,boot,sys} gnu \/\r\n<\/pre>\n

\u4e0b\u9762\u5b9e\u4f8b\uff0c\u4f7f\u7528--exclude<\/code>\u9009\u9879\u6392\u9664\u591a\u4e2a\u6587\u4ef6\uff0c\u6211\u4eec\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u641c\u7d22\u5b57\u7b26\u4e32 linuxprobe\uff0c\u4e0d\u5305\u62ec\u4ee5 .png \u548c .jpg \u7ed3\u5c3e\u7684\u6587\u4ef6\uff1a<\/p>\n

\r\n[root@localhost ~]# grep -rl --exclude=*.{png,jpg} linuxprobe *\r\n<\/pre>\n
\u603b\u7ed3<\/strong><\/div>\n

grep \u662f\u4e00\u79cd\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u6587\u4ef6\u4e2d\u641c\u7d22\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u884c\uff0c\u5e76\u5c06\u5339\u914d\u7684\u884c\u6807\u51c6\u8f93\u51fa\u3002\u5728\u672c\u6587\u4e2d\u4ecb\u7ecd\u5982\u4f55\u5728\u4f7f\u7528 grep \u641c\u7d22\u65f6\u6392\u9664\u4e00\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u6216\u76ee\u5f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8981\u4ec5\u663e\u793a\u4e0e\u641c\u7d22\u6a21\u5f0f\u4e0d\u5339\u914d\u7684\u884c\uff0c\u8bf7\u4f7f\u7528-v\u9009\u9879\u3002\u4f8b\u5982\uff0c\u663e\u793a\u4e0d\u5305\u542bnologin\u7684\u884c\uff0c\u4f7f\u7528\u4e0b\u9762\u547d\u4ee4\uff1a [root@ […]<\/p>\n","protected":false},"author":309,"featured_media":221717,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221708"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221708\/revisions"}],"predecessor-version":[{"id":221719,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221708\/revisions\/221719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/221717"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}