Shell Scripting App (iii)

Source: Internet
Author: User
Tags case statement control characters

Security code: Waves will sometimes, straight hanging cloud sail sea

Shell scripting App (iii)

Foreword: It is difficult to write all the code in sequential order when it is hard to satisfy the requirements when facing various lists and repeating tasks. Use other program control structures such as loops, branches, and more to make it easier to perform more complex and powerful functions.

1. Use a For Loop statement (read different variable values to execute the same set of commands one by one)

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/87/D1/wKioL1fiVKOTFYHJAABJqDBjA2w042.png-wh_500x0-wm_3 -wmp_4-s_1465146851.png "title=" Zxd$%kfg15}2yf7}}31ee3e.png "alt=" Wkiol1fivkotfyhjaabjqdbja2w042.png-wh_50 "/>

1) Bulk Add users by Name list

Prepare the Employee list file Users.txt, and then write a script called uaddfor.sh, which reads each user name from the Users.txt file, repeats the add user, and sets the initial password for the associated action.

Vi/root/users.txt

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/87/CF/wKioL1fiQY3Bum5xAAACdwPsNy4550.png-wh_500x0-wm_3 -wmp_4-s_88998224.png "title=" Rv4gjvv21qr0on[7l6b$6tk.png "alt=" Wkiol1fiqy3bum5xaaacdwpsny4550.png-wh_50 "/>

Vi uaddfor.sh

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/87/D3/wKiom1fiQaDhp9DwAAAGciR1Gbo665.png-wh_500x0-wm_3 -wmp_4-s_2119752558.png "title=" t@ '}x$2d2v]ajnr (34_C7@6.png "alt=" Wkiom1fiqadhp9dwaaagcir1gbo665.png-wh_50 "/>

Execute script: View user files after./uaddfor.sh cat/etc/passwd

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/87/D3/wKiom1fiQbKjkjzuAAAFM_u5cgQ903.png-wh_500x0-wm_3 -wmp_4-s_2259494252.png "title=" Ivwu8lbu43@s9x[noh_vll5.png "alt=" Wkiom1fiqbkjkjzuaaafm_u5cgq903.png-wh_50 "/>

2) To delete the user added by the uaddfor.sh script, simply delete the user's command from the script above.

Vi udelfor.sh

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/CF/wKioL1fiQd6ixxwmAAAFHIS1trA940.png-wh_500x0-wm_3 -wmp_4-s_276028199.png "title=" o0v4wluz2tpjq) xt20o]5a7.png "alt=" Wkiol1fiqd6ixxwmaaafhis1tra940.png-wh_50 "/>

After executing the script, the user has been removed

3) View host status based on IP address list

First establish the IP address list file Ipadds.txt, and then write a shell script named Chkhosts.sh, from the Ipadds.txt file to read the IP address of each server,

Repeat the ping connectivity test and output the appropriate prompts based on the test results.

Vi/root/ipadds.txt

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/CF/wKioL1fiQfehJVRLAAACR57_GH8653.png-wh_500x0-wm_3 -wmp_4-s_3316959554.png "title=" Ip6]ta (fa8@%$5q) 3 (8p7oj.png "alt=" Wkiol1fiqfehjvrlaaacr57_gh8653.png-wh_50 "/>

Scripting VI chkhosts.sh

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/D3/wKiom1fiQg6DiZnpAAAH75tP5TQ022.png-wh_500x0-wm_3 -wmp_4-s_1153206061.png "title=" $SPZCN _a{(} @ER}~78]0w}0.png "alt=" Wkiom1fiqg6diznpaaah75tp5tq022.png-wh_50 "/>

Execute script

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/87/CF/wKioL1fiQh6Ct2JQAAADNI2fyuI467.png-wh_500x0-wm_3 -wmp_4-s_4217572719.png "title=" ~nn (g8nz{jb$%td1h96ymxq.png "alt=" Wkiol1fiqh6ct2jqaaadni2fyui467.png-wh_50 "/>

2. Use the While Loop statement (repeat tests for a condition, as long as the condition is true). The for loop is irregular, the list is fixed, while the loop is appropriate for the number of cycles required, the operand is numbered numerically, and repeated operations are performed on a specific condition. )

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/D1/wKioL1fiVd-Q4NChAABNeCGw5iY186.png-wh_500x0-wm_3 -wmp_4-s_3202251900.png "title=" V%dw5h6}{49rd%g68b@hxhb.png "alt=" Wkiol1fivd-q4nchaabnecgw5iy186.png-wh_50 "/>


1) batch Add the regular number of users, add 20 users, the name is STU1,STU2, ... Stu20

Vi uaddwhile.sh

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/87/CF/wKioL1fiQkLxdQhiAAAGxUQbTdw798.png-wh_500x0-wm_3 -wmp_4-s_2706450819.png "title=" 3r]k_y525j8@cao4a0y6_{m.png "alt=" Wkiol1fiqklxdqhiaaagxuqbtdw798.png-wh_50 "/>

Cycle Condition: Ordinal <=20

Let + +: sequential increments to avoid dead loops

To view the user after executing the script: cat/etc/passwd

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/D3/wKiom1fiQliR7ZfcAAAFtJB_cXI360.png-wh_500x0-wm_3 -wmp_4-s_2324995621.png "title=" {2}24v%h2icry80$4i5__c3.png "alt=" Wkiom1fiqlir7zfcaaaftjb_cxi360.png-wh_50 "/>

2) Delete the added user, just modify the above code to

Vi udelwhile.sh

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/87/CF/wKioL1fiQoShQA0KAAAE9qj81jQ526.png-wh_500x0-wm_3 -wmp_4-s_1853545603.png "title=" R_9sqa]uw$_x0j3u9a84_10.png "alt=" Wkiol1fiqoshqa0kaaae9qj81jq526.png-wh_50 "/>

To see if the user was deleted after executing the script

3) Guess the price of the game

idea: By the environment variable random can get a random integer less than 2 of 16 square, calculate its remainder with 1000 to get 0-999 random price, The repeated guessing operation can be implemented by a while loop with true as the test condition, when the user guesses the actual price and terminates the loop, the process of judging the guessing price and the actual price is implemented by the IF statement, nested in the while loop body, using variables to record the number of guesses.

Vi pricegame.sh

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/87/D3/wKiom1fiQpnTpw5HAAAMd6HhtEw508.png-wh_500x0-wm_3 -wmp_4-s_1660362580.png "title=" Buhv0xm}1eh_0fysun$hw]o.png "alt=" Wkiom1fiqpntpw5haaamd6hhtew508.png-wh_50 "/>

Chmod +x pricegame.sh

./pricegame.sh Execute Script

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/D3/wKiom1fiQq_CrWFcAAAK9nFCj9k959.png-wh_500x0-wm_3 -wmp_4-s_86900492.png "title=" Vgy~ru) @OCU} (3ctbd6 (Z (f.png "alt=" Wkiom1fiqq_crwfcaaak9nfcj9k959.png-wh_50 "/>

3. Use the Case Branch statement (separate command sequences for different values of variables )

Case statements can be more clearly structured using a scripting program. )

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/87/D5/wKiom1fiVVLw3OnZAACB_RPDBiQ768.png-wh_500x0-wm_3 -wmp_4-s_2359408942.png "title=" CPIWJ2 (dv$n]h) o8$2e$ut3.png "alt=" Wkiom1fivvlw3onzaacb_rpdbiq768.png-wh_50 "/>

1) Check the user input a character, through the case statement to determine whether the character is a letter, an array or other control characters, and give the corresponding prompt information.

Vi hitkey.sh

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/87/D3/wKiom1fiRqDgULDJAAAHJyAyHEs363.png-wh_500x0-wm_3 -wmp_4-s_2807405399.png "title=" Y]m_j) 7x%) r$@122}u%ijyq.png "alt=" Wkiom1firqdguldjaaahjyayhes363.png-wh_50 "/>

The effect of executing the script:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/87/CF/wKioL1fiRtKBNl0PAAAGdySr-6Y235.png-wh_500x0-wm_3 -wmp_4-s_3961400167.png "title=" {JE8JF (8em5@n~u8w~p6]o0.png "alt=" Wkiol1firtkbnl0paaagdysr-6y235.png-wh_50 "/>

1) writing system service scripts

The Start,stop,restart,status control parameters specified by the location variable are used to start, stop, restart the sleep process, and to view the status of the sleep process, respectively.

Vi Myprog

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/CF/wKioL1fiRu_y8vxbAAALL5T48Tg572.png-wh_500x0-wm_3 -wmp_4-s_623940281.png "title="}0ag$a}) F0 (ZJ7 ' zzrw579f.png "alt=" Wkiol1firu_y8vxbaaall5t48tg572.png-wh_50 "/>

Test and confirm that the Myprog script executes the following results:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/87/CF/wKioL1fiRzOAc4WhAABod-8HTX0869.jpg-wh_500x0-wm_3 -wmp_4-s_425989920.jpg "title=" 2c98abf8-bfde-4c66-ba0a-ee52b220797d.jpg "alt=" Wkiol1firzoac4whaabod-8htx0869.jpg-wh_50 "/>

Then execute the following command to add Myprog to the system service

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/87/CF/wKioL1fiR0PgX5qNAAAFWIpB9qU505.png-wh_500x0-wm_3 -wmp_4-s_2619958813.png "title=" o8ckm (9ed7~]t$a}1{lvxin.png "alt=" Wkiol1fir0pgx5qnaaafwipb9qu505.png-wh_50 "/>

On-Machine Experiment part:

Experimental case: Shell script Application Combat

1. Writing getarp.sh script files

(1) Send ARP request via arping command, record MAC address according to feedback result.

(2) Assign a network segment address (such as 192.168.4.) to the variable nadd as a prefix for the detection address.

(3) using the While loop statement, duplicate detection target and record MAC address, host address from 1-254.

We use bridged networks to get to 0.102 and 0.103 addresses and do tests

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/87/D5/wKiom1fiV2yC9D7IAAAkE6CigMk858.png-wh_500x0-wm_3 -wmp_4-s_3727347678.png "title=" qh$097 ' ~ ' bnmh0frclkos4w.png "alt=" Wkiom1fiv2yc9d7iaaake6cigmk858.png-wh_50 "/>


1, define the network segment address, Mac list file, send ARP request, and record the feedback result (if the network segment changes, just modify madd= "192.168.x." )

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/87/D1/wKioL1fiURyhwsXdAAALu5_c_Dc838.png-wh_500x0-wm_3 -wmp_4-s_3963997786.png "title=" S}9q7$6$4@%3ooun]@l]@01.png "alt=" Wkiol1fiuryhwsxdaaalu5_c_dc838.png-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/D1/wKioL1fiUTKwIFiOAAAC2A2IMdw068.png-wh_500x0-wm_3 -wmp_4-s_1345618290.png "title=" Gx4b2%qfs ' E]}vw@dkj6]sd.png "alt=" Wkiol1fiutkwifioaaac2a2imdw068.png-wh_50 "/>

To view the/etc/ethers file after executing the script

chmod +x Getarp

./getarp

Cat/etc/ethers

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/87/D1/wKioL1fiUUCRUyJ0AAAHwT8WPLc167.png-wh_500x0-wm_3 -wmp_4-s_3640562990.png "title=" 4l[1]_myzrl40r} (3) xqxc2.png "alt=" Wkiol1fiuucruyj0aaahwt8wplc167.png-wh_50 "/>

2. Writing scanhost.sh Scripts

(1) There are many ways to detect whether a host to open the anonymous FTP service, here to take the wget download tool to access the FTP root directory, if you can successfully list, it is considered anonymous FTP is turned on, otherwise considered off.

(2) Assign a value to the variable target by filtering out all IP addresses in the/etc/ethers file via the awk command.

(3) Use the FOR Loop statement, read the IP address in the target variable, and repeatedly probe the FTP open situation.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/87/D5/wKiom1fiUfzAuLpxAAAJ4TyEmwg015.png-wh_500x0-wm_3 -wmp_4-s_563288362.png "title=" mit%2 ' p46pe% ' zlyknysavm.png "alt=" Wkiom1fiufzaulpxaaaj4tyemwg015.png-wh_50 "/>

Build the FTP service on another Linux server B192.168.1.2 (using the VSFTPD software) before executing the script, then execute the script on the Linuxa

Install VSFTPD on B, as shown in:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/87/D1/wKioL1fiUjGxf5VkAAAKVne36EQ685.png-wh_500x0-wm_3 -wmp_4-s_2210790525.png "title=" Cx{hk6lxn5 ' wb0116nf (07u.png "alt=" Wkiol1fiujgxf5vkaaakvne36eq685.png-wh_50 "/>

vim/etc/vsftpd/vsftpd.conf enable anonymous, enabled by default, let's take a look at:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/87/D5/wKiom1fiUkyyDmZlAABYApQXuhQ505.jpg-wh_500x0-wm_3 -wmp_4-s_3866324622.jpg "title=" 695c31a3-6693-4b55-8533-4b7911c39c6b.jpg "alt=" Wkiom1fiukyydmzlaabyapqxuhq505.jpg-wh_50 "/>

Open FTP Service

Service vsftpd Start

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/D5/wKiom1fiUpizMB3WAAAEh8qIEAc716.png-wh_500x0-wm_3 -wmp_4-s_3800581785.png "title=" 7vmqy%]0]@3k]i) q$}ls0li.png "alt=" Wkiom1fiupizmb3waaaeh8qieac716.png-wh_50 "/>

Log on to the FTP 192.168.1.2 test on a, if you cannot use the FTP command, you need to install ftp-0.17 ...

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/87/D1/wKioL1fiUrHgCHFRAAAIRd9v0d8089.png-wh_500x0-wm_3 -wmp_4-s_2734784804.png "title=" [51z5c23$yndsekc04 ' 9640.png "alt=" Wkiol1fiurhgchfraaaird9v0d8089.png-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/87/D1/wKioL1fiUtPjjsSUAAAqFtAaq_s240.jpg-wh_500x0-wm_3 -wmp_4-s_814790707.jpg "title=" b6b5f951-4a0f-49bf-b590-6e2dd19cf686.jpg "alt=" wkiol1fiutpjjssuaaaqftaaq_ S240.jpg-wh_50 "/>

Finally, execute the script on a./scanhost.sh

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/D1/wKioL1fiUxaA7MkCAAAbXOt5BEs082.jpg-wh_500x0-wm_3 -wmp_4-s_181339203.jpg "title=" 81b7b0ed-b8c1-4164-909d-3847c0e33807.jpg "alt=" Wkiol1fiuxaa7mkcaaabxot5bes082.jpg-wh_50 "/>

After executing the script, it is found that FTP anonymous login is 192.168.1.2 open.

Thank you for watching, hope to help you sincerely!


This article from "A Candle" blog, declined reprint!

Shell Scripting App (iii)

Related Article

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.