Sed combined with regular expression hardening exercises

Source: Internet
Author: User

1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace

[[email protected] ~]# sed -r  ' s#^[[:space:]]+# #g '  /etc/grub2.cfg## do  NOT EDIT THIS FILE## It is automatically generated by  grub2-mkconfig using templates# from /etc/grub.d and settings from / etc/default/grub#### begin /etc/grub.d/00_header ## #set  pager=1if [ -s $ prefix/grubenv ]; thenload_envfiif [  "${next_entry}"  ] ; thenset  default= "${next_entry}" Set next_entry=save_env next_entryset boot_once=trueelseset default = "${saved_entry}" Fiif [ x "${feature_menuentry_id}"  = xy ]; thenmenuentry_id_ option= "--id" elsemenuentry_id_option= "" fiexport menuentry_id_optionif [  "${prev_saved_entry}"  ]; thenset saved_entry= "${prev_saved_entry}" save_env saved_entryset prev_saved_entry =save_env prev_saved_entryset&nBsp;boot_once=truefi 

2, delete all # and white-space characters at the beginning of the line beginning with # in the/etc/fstab file followed by at least one white-space character  

[[email protected] ~]# sed -r  ' s/^#[[:space:]]+//g '  /etc/fstab#/etc/fstabcreated  by anaconda on tue jul 19 14:39:24 2016#accessible filesystems,  by reference, are maintained under  '/dev/disk ' see man pages  Fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more info#uuid= f83c52ce-5740-4f02-bb87-6e190360dc30 /                        xfs      defaults  0 0uuid=c98144e4-d1b4-45b1-bb22-3112420ea487 /boot                    xfs      defaults  0 0UUID=dbf5c483-133e-4888-bbb1-a9622d83a930 swap                     swap    defaults   0 0#end[[email protected] ~]#

2.1, delete all # and white space characters beginning with # at the beginning of the line in the/etc/fstab file followed by at least one white-space character, and delete the line followed by # that is full of whitespace characters

[[email protected] ~]# sed -r -e  ' s/^#[[:space:]]+//g '  -e  '/^#/d '  /etc/fstab/etc/fstabCreated by anaconda on Tue Jul 19 14:39:24  2016accessible filesystems, by reference, are maintained under  '/dev/disk ' See man pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for  more infouuid=f83c52ce-5740-4f02-bb87-6e190360dc30 /                        xfs      defaults  0 0UUID=c98144e4-d1b4-45b1-bb22-3112420ea487 /boot                    xfs      defaults  0 0uuid=dbf5c483-133e-4888-bbb1-a9622d83a930 swap                     swap     defaults  0 0[[email protected] ~]#

3. Add # At the beginning of each line of/etc/issue

[Email protected] ~]# cat-n/etc/issue 1\s 2Kernel \ r on a \m 3[[email protected] ~]# sed-r ' [email Protec Ted] (. *) @#&@g '/etc/issue#\s#kernel \ r on an \m#[[email protected] ~]# sed-r ' [email protected] (. *) @#\[email PROTECTE d] '/etc/issue#\s#kernel \ r on an \m#[[email protected] ~]# sed-r ' [email protected]^@#@g '/etc/issue#\s#kernel \ R on an \m#[[email protected] ~]#

4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #

[[email protected] ~]# sed -r  ' s/^[^#]/#/g '  /etc/fstab## /etc/fstab#  Created by anaconda on tue jul 19 14:39:24 2016## accessible  filesystems, by reference, are maintained under  '/dev/disk ' # see  man pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more  info# #UID =f83c52ce-5740-4f02-bb87-6e190360dc30 /                        xfs      defaults#UID=c98144e4-d1b4-45b1-bb22-3112420ea487 /boot                    xfs      defaults#UID=dbf5c483-133e-4888-bbb1-a9622d83a930 swap             &nbSp;       swap    defaults#end[[email protected]  ~]#

5, processing/etc/fstab path, use sed command to take out its directory name and base name

[[email protected] packages]# Cd[[email protected] ~]# echo "/etc/hosts" |sed-r ' s#/.*/([^/]+] #\1#g ' Hosts[[email Protected] ~]# echo "/etc/hosts" |sed-r ' s# (/.*/) [^/]+/?#\1#g '/etc/[[email protected] ~]# echo "/etc/issue" |sed-r ' s# (. * /)([^/]+/?) $#\1#g '/etc/[[email protected] ~]# echo "/etc/issue" |sed-r ' s# (. */) ([^/]+/?) $#\2#g ' Issue[[email protected] ~]#


6. Use SED to remove the IPv4 address of the ifconfig command

[Email protected] ~]# ifconfig |sed-n 2p inet 192.168.226.138 netmask 255.255.255.0 broadcast 192.168.226.255[[ Email protected] ~]# ifconfig |sed-n 2p|sed-r ' s/.*net//g ' 192.168.226.138 netmask 255.255.255.0 broadcast 192.168.226  .255[[email protected] ~]# ifconfig |sed-n 2p|sed-r ' s/.*net//g ' |sed-r ' s/net.*//g ' 192.168.226.138[[email protected] ~]# ifconfig |sed-nr ' 2s#^.*net (. *) NETM.*$#\1#GP ' 192.168.226.138[[email protected] ~]#

7. Statistics of all RPM files in the package directory on the CentOS installation CD. Number of repetitions of the second-to-last field

[email protected] packages]# ls *.rpm |sed-r ' s/^.*\. (. *) \.rpm/\1/g ' |sort |uniq-c |sort-i686 2938 noarch 4069 x86_64[[email protected] packages]# ls *.rpm |rev |cu t-d.-f2 |sort|uniq-c|uniq 4069 46_68x 686i 2938 hcraon[[email protected] packages]#



This article is from "Wake up your not alarm clock but dream" blog, please be sure to keep this source http://purify.blog.51cto.com/10572011/1836292

Sed combined with regular expression hardening exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.