生命之中的最大错误在于:终日担心犯错误。— 爱尔伯特·哈伯德 (Elbert Hubbard 1856-1915),《笔记》
脚本示例:
使用shell批量删除10天内没访问过的文件
find命令有一个参数可以避免特殊字符对后面执行的命令产生影响
命令变成这样:
一般来说不会有什么问题,但如果每个文件路径都特别深,那么会导致参数很多,命令行很长。
比如会看到如下提示:
那可以让xargs一次处理一条试试:
-l1是一次处理一个
-t是处理之前打印出命令,适合调试或者欣赏观看用
find . -type f -atime +10 | xargs rm -f
使用shell批量删除10天内没访问过的文件
find命令有一个参数可以避免特殊字符对后面执行的命令产生影响
-print0
True; print the full file name on the standard output, followed by a null character (instead of the new-
line character that ‘-print’ uses). This allows file names that contain newlines or other types of
white space to be correctly interpreted by programs that process the find output. This option corre-
sponds to the ‘-0’ option of xargs.
True; print the full file name on the standard output, followed by a null character (instead of the new-
line character that ‘-print’ uses). This allows file names that contain newlines or other types of
white space to be correctly interpreted by programs that process the find output. This option corre-
sponds to the ‘-0’ option of xargs.
命令变成这样:
find . -type f -atime +10 -print0 | xargs -0 rm -f
一般来说不会有什么问题,但如果每个文件路径都特别深,那么会导致参数很多,命令行很长。
比如会看到如下提示:
xargs: argument line too long
那可以让xargs一次处理一条试试:
find . -type f -atime +10 -print0 | xargs -0 -l1 -t rm -f
-l1是一次处理一个
-t是处理之前打印出命令,适合调试或者欣赏观看用
发送文本邮件
发送纯附件
注:需要用到sharutils
ubuntu下这样安装即可:
发送文本+附件
更多内容可以参阅:
http://linux.chinaunix.net/bbs/viewthread.php?tid=905943
echo "content" | mail -s "subject" {email}
发送纯附件
uuencode aslibra.com.sql aslibra.com.sql | mail -s subject {email}
注:需要用到sharutils
ubuntu下这样安装即可:
apt-get install sharutils
发送文本+附件
echo "content:www.aslibra.com" >/tmp/mail ;uuencode aslibra.com.sql aslibra.com.sql >>/tmp/mail; cat /tmp/mail | mail -s subject {email}
更多内容可以参阅:
http://linux.chinaunix.net/bbs/viewthread.php?tid=905943
1:使用 expr 外部程式
加法 r=`expr 4 + 5`
echo $r
注意! '4' '+' '5' 这三者之间要有空白
r=`expr 4 * 5` #错误
乘法 r=`expr 4 \* 5`
2:使用 $(( ))
r=$(( 4 + 5 ))
echo $r
3:使用 $[ ]
r=$[ 4 + 5 ]
echo $r
乘法
r=`expr 4 \* 5`
r=$(( 4 * 5 ))
r=$[ 4 * 5 ]
echo $r
除法
r=`expr 40 / 5`
r=$(( 40 / 5 ))
r=$[ 40 / 5 ]
echo $r
减法
r=`expr 40 - 5`
r=$(( 40 - 5 ))
r=$[ 40 - 5 ]
echo $r
求余数
r=$[ 100 % 43 ]
echo $r
乘幂 (如 2 的 3 次方)
r=$(( 2 ** 3 ))
r=$[ 2 ** 3 ]
echo $r
注:expr 沒有乘幂
加法 r=`expr 4 + 5`
echo $r
注意! '4' '+' '5' 这三者之间要有空白
r=`expr 4 * 5` #错误
乘法 r=`expr 4 \* 5`
2:使用 $(( ))
r=$(( 4 + 5 ))
echo $r
3:使用 $[ ]
r=$[ 4 + 5 ]
echo $r
乘法
r=`expr 4 \* 5`
r=$(( 4 * 5 ))
r=$[ 4 * 5 ]
echo $r
除法
r=`expr 40 / 5`
r=$(( 40 / 5 ))
r=$[ 40 / 5 ]
echo $r
减法
r=`expr 40 - 5`
r=$(( 40 - 5 ))
r=$[ 40 - 5 ]
echo $r
求余数
r=$[ 100 % 43 ]
echo $r
乘幂 (如 2 的 3 次方)
r=$(( 2 ** 3 ))
r=$[ 2 ** 3 ]
echo $r
注:expr 沒有乘幂
备忘脚本
date取得分钟数
$(()) 运算
date取得分钟数
$(()) 运算
#execute every 5 minutes
a=`date +%M`
b=$(( $a % 5 ))
if [ $b = 0 ] ; then
echo "execute it"
else
echo "Not execute"
fi
a=`date +%M`
b=$(( $a % 5 ))
if [ $b = 0 ] ; then
echo "execute it"
else
echo "Not execute"
fi
在公网的服务器不免遭到颇有居心的人尝试登录,以便控制机器,如果你没有做防范工作,那就要注意了。
至少,你可以先看看你的服务器是不是遭人喜欢:
你可以得到类似的结果:
密码的强度要注意
事情可能远比你想象的严重,可以想象,被尝试到正确的密码是多么的危险。
将近7万次的尝试,可以尝试很多组合了,但其实也未必那么轻易能够破解密码的,前提是你不要使用简单的字母组合。
简单的计算一下你用四位密码的强度:
如果单纯用小写字母,那有 26^4=456976 个可能
但小写和大写配合呢? 7311616 个可能
如果加上数字和特殊字符,那可能性是 40960000 以上
如果暴力破解,一秒一个,那可能得多久?
126小时 -> 2000小时 -> 10000小时(1年多!)
其实破解密码也没有那么容易吧?但为了安全性,务必使用复杂的密码,长度足够长~
脚本是如何运行的?
回来简单的说一下脚本,分段说明,比较基础的知识:
至少,你可以先看看你的服务器是不是遭人喜欢:
grep "Failed password for root" /var/log/secure | awk '{print $11}' | awk -F ":" '{print $4}' | sort | uniq -c | sort -nr | more
你可以得到类似的结果:
引用
68906 221.232.131.51
20352 218.78.209.253
19364 202.111.128.225
17704 61.138.179.55
15190 211.68.70.24
14166 221.7.58.101
13670 147.231.252.4
9600 210.77.146.53
7850 219.148.34.17
7668 61.152.201.69
7080 212.107.200.32
6878 202.143.142.198
6644 210.51.4.93
6504 202.30.194.186
6378 60.191.93.125
6254 60.195.250.54
6090 58.247.222.202
5806 60.217.229.228
5640 219.141.223.8
5424 218.94.72.125
5358 61.143.178.194
5292 219.150.161.20
5284 221.130.201.21
5252 211.157.98.65
5052 220.182.6.9
5028 211.142.221.204
4760 203.78.66.213
4672 59.120.111.226
4531 88.84.130.52
20352 218.78.209.253
19364 202.111.128.225
17704 61.138.179.55
15190 211.68.70.24
14166 221.7.58.101
13670 147.231.252.4
9600 210.77.146.53
7850 219.148.34.17
7668 61.152.201.69
7080 212.107.200.32
6878 202.143.142.198
6644 210.51.4.93
6504 202.30.194.186
6378 60.191.93.125
6254 60.195.250.54
6090 58.247.222.202
5806 60.217.229.228
5640 219.141.223.8
5424 218.94.72.125
5358 61.143.178.194
5292 219.150.161.20
5284 221.130.201.21
5252 211.157.98.65
5052 220.182.6.9
5028 211.142.221.204
4760 203.78.66.213
4672 59.120.111.226
4531 88.84.130.52
密码的强度要注意
事情可能远比你想象的严重,可以想象,被尝试到正确的密码是多么的危险。
将近7万次的尝试,可以尝试很多组合了,但其实也未必那么轻易能够破解密码的,前提是你不要使用简单的字母组合。
简单的计算一下你用四位密码的强度:
如果单纯用小写字母,那有 26^4=456976 个可能
但小写和大写配合呢? 7311616 个可能
如果加上数字和特殊字符,那可能性是 40960000 以上
如果暴力破解,一秒一个,那可能得多久?
126小时 -> 2000小时 -> 10000小时(1年多!)
其实破解密码也没有那么容易吧?但为了安全性,务必使用复杂的密码,长度足够长~
脚本是如何运行的?
回来简单的说一下脚本,分段说明,比较基础的知识:
grep "Failed password for root" /var/log/secure |
#取出root登录失败的记录
awk '{print $11}' |
#取出ip段(::ffff:189.19.29.21)
awk -F ":" '{print $4}' |
#取得ip(189.19.29.21)
sort |
#排序
uniq -c |
#计算每条记录的出现次数
sort -nr |
#按出现次数排序
more
#取出root登录失败的记录
awk '{print $11}' |
#取出ip段(::ffff:189.19.29.21)
awk -F ":" '{print $4}' |
#取得ip(189.19.29.21)
sort |
#排序
uniq -c |
#计算每条记录的出现次数
sort -nr |
#按出现次数排序
more
shell的编程有时候比较难懂,语法和工具要结合起来用。
我有一个主机和端口的处理,想用shell循环处理,以便以后可以随意增加要处理的内容。
简单的劳工式代码:
如果 do sth 都是一样的代码,那我们可以整理成循环:
上面的代码很好懂。
首先把要处理的内容写成规律的字符串,端口号和ip用冒号分割,然后用空格串接起来。这样我们可以随意添加而不用修改后面重复的代码,维护起来容易。
关键在于用冒号分割的怎么处理,其实就是搭配awk的处理结果就可以了!看代码就懂。
我有一个主机和端口的处理,想用shell循环处理,以便以后可以随意增加要处理的内容。
简单的劳工式代码:
ip=192.168.1.4
port=80
//do sth
ip=192.168.1.4
port=21
//do sth
ip=192.168.1.40
port=3398
//do sth
port=80
//do sth
ip=192.168.1.4
port=21
//do sth
ip=192.168.1.40
port=3398
//do sth
如果 do sth 都是一样的代码,那我们可以整理成循环:
Forwards="192.168.1.4:3389 192.168.1.40:110 192.168.1.40:25"
for forward in $Forwards
do
Forward_Port=`echo $forward | awk -F":" '{print $2}'`
Forward_IP=`echo $forward | awk -F":" '{print $1}'`
echo " Forward $Forward_IP:$Forward_Port"
//do sth
done
for forward in $Forwards
do
Forward_Port=`echo $forward | awk -F":" '{print $2}'`
Forward_IP=`echo $forward | awk -F":" '{print $1}'`
echo " Forward $Forward_IP:$Forward_Port"
//do sth
done
上面的代码很好懂。
首先把要处理的内容写成规律的字符串,端口号和ip用冒号分割,然后用空格串接起来。这样我们可以随意添加而不用修改后面重复的代码,维护起来容易。
关键在于用冒号分割的怎么处理,其实就是搭配awk的处理结果就可以了!看代码就懂。
如果你有大批的文件需要重命名,但有那么一点规律可循,那你可以简单的用shell来处理
下面举个例子,假设我有 xx-abc.txt 的文件,都要去掉前面的xx-
完成了,很简单吧~
awk整理命令的编程可以处理更加复杂的任务,自己可以灵活处理
下面举个例子,假设我有 xx-abc.txt 的文件,都要去掉前面的xx-
[root@aslibra www.aslibra.com]# ll
total 0
[root@aslibra www.aslibra.com]# touch a-b.txt
[root@aslibra www.aslibra.com]# touch a-c.txt
[root@aslibra www.aslibra.com]# ls
a-b.txt a-c.txt
#说明:文件已经创建
[root@aslibra www.aslibra.com]# ls | awk -F '-' '{print "mv "$0" "$2}'
mv a-b.txt b.txt
mv a-c.txt c.txt
#说明:用awk整理好命令,用-作为分隔符,打印出命令即可
[root@aslibra www.aslibra.com]# ls | awk -F '-' '{print "mv "$0" "$2}'| /bin/bash
#说明:测试无误则可以用管道方式交给bash执行啦
[root@aslibra www.aslibra.com]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 12 12:57 b.txt
-rw-r--r-- 1 root root 0 Jul 12 12:57 c.txt
total 0
[root@aslibra www.aslibra.com]# touch a-b.txt
[root@aslibra www.aslibra.com]# touch a-c.txt
[root@aslibra www.aslibra.com]# ls
a-b.txt a-c.txt
#说明:文件已经创建
[root@aslibra www.aslibra.com]# ls | awk -F '-' '{print "mv "$0" "$2}'
mv a-b.txt b.txt
mv a-c.txt c.txt
#说明:用awk整理好命令,用-作为分隔符,打印出命令即可
[root@aslibra www.aslibra.com]# ls | awk -F '-' '{print "mv "$0" "$2}'| /bin/bash
#说明:测试无误则可以用管道方式交给bash执行啦
[root@aslibra www.aslibra.com]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 12 12:57 b.txt
-rw-r--r-- 1 root root 0 Jul 12 12:57 c.txt
完成了,很简单吧~
awk整理命令的编程可以处理更加复杂的任务,自己可以灵活处理





