Common shell scripting use cases, from the online reference and after the modification of the work, in addition to the wiki in the company to write records, it is necessary for a blog to record a drop.
Combat Problem 1:
Use the For loop to create a batch of 10 files in the/oldboy directory, with the name in turn:
Oldboy-1
Oldboy-2
......
Oldboy-10
Answer:
[[Email protected]]# cat 1.sh
#!/bin/bash
For iin ' seq 1 10 '
Do
[!-d/oldboy] && Mkdir/oldboy
Cd/oldboy
mkdir oldboy-$i
Done
Extension issues:
For example, randomly create 10 files, contain Oldboy strings, end with 1-10, and the extension is HTML.
Example: afds_oldboy_1.html,khadf_oldboy_2.html
Answer:
[Email protected]]# cat 1_a.sh
#!/bin/bash
For iin ' seq 1 10 '
Do
[!-d/oldboy] && Mkdir/oldboy
Cd/oldboy
For a in ' Cat/dev/urandom|head-n |md5sum|head-c 5 '
Do
mkdir ${a}_oldboy_${i}.html
Done
Done
# generate random numbers using date
date+%s%n | md5sum | Head-c 10
# using/dev/urandom to generate random strings
Cat/dev/urandom | Head-n 10 | md5sum | Head-c 10
Combat Problem 2:
Change all the Oldboy in the above file name to Linux (implemented with a For loop).
Hint: In the future, we will try to replace the Oldboy in the file name with Linux, and the extension to uppercase
Answer:
[[Email protected]]# cat 2.sh
#!/bin/bash
Cd/oldboy
For iin ' Ls/oldboy '
Do
Rename Oldboy Linux $i
Done
Combat Problem 3:
Batch Create 10 System account OLDBOY01-OLDBOY10 and set the password (the password can not be the same, the password is a random 8-bit string).
The number plus 0 idea:http://oldboy.blog.51cto.com/2561410/788422
Answer:
[[Email protected]]# cat 3.sh
#!/bin/bash
For Iin ' Seq-w 101 110|sed-e "S#^10#0#g" |sed-e "s#^1# #g" '
Do
Useradd oldboy$i
For n in ' date|md5sum |head-c 8 '
Do
echo "$n" | passwd--stdinoldboy$i
[!-f/oldboy/passwd.txt] &&touch/oldboy/passwd.txt
echo "Oldboy$i $n" >>/oldboy/passwd.txt
Done
Done
Combat Problem 4:
Write a script that enables you to determine the IP of the current online user in the 10.0.0.0/24 network (there are many ways)
Answer: (an order)
NMAP-SP 192.168.0.0/24 >a.log && cat a.log |grep "192.168.0.*" |awk ' {print $} ' |sort-n-K 4-t.
Combat Problem 5:
Write a script to solve the DOS attack production case
Tip: Depending on the number of web logs or network connections, monitoring when an IP concurrent connection number or a short time PV reached 100, that is, call the firewall command to block the corresponding IP, monitoring frequency every 3 minutes. The firewall command is: iptables-a input-s 10.0.1.10-j DROP.
Answer:
#!/bin/sh
# Upper Limit
num=100
Iplist= ' Netstat-an |grep ^tcp.*:80|egrep-v "listen|127.0.0.1" |awk ' {print $} ' |awk-f: ' {print $} ' |sort|uniq-c|sort- Rn-k1|awk ' {if ($1> $num) {print $}} '
Fori in $iplist
Do
Iptables-t filter-i input-p tcp-s $i--dport 80-j DROP
# iptables–a Input–s $i –j DROP
Done
This article is from the "Early bird has the worm to eat" blog, please be sure to keep this source http://hejianping.blog.51cto.com/11279690/1870290
Shell Scripting Practices