awk学习笔记

一、使用awk

1、调用awk

awk [options] -f progfile [–] file …

awk [options] [–] ‘program’ file …

2、命令行选项

-F fs

–field-separator fs

设置字段分隔符,如打印用户:

awk -F : ‘{print $1}’ /etc/passwd

-f source-file

–file source-file

从文件读取程序,如:awk -f file /etc/passwd

file内容为:

#!/bin/awk -f

BEGIN {FS=”:”}

{print $1}

-v var=val

–assign var=val

设置var的值为val,如:awk -v foo=hello -v bar=world ‘BEGIN {print foo,bar}’

3、包含其它文件到awk程序

@include “test1”

BEGIN {

print “This is script test2.”

}

二、正则表达式

1、如何使用正则表达式

搜索含foo字符的行,并打印第二列:

awk ‘/foo/ { print $2 }’ BBS-list

-| 555-1234

-| 555-6699

-| 555-6480

-| 555-2127

也可以使用如下形式:

exp ~ /regexp/或exp !~/regexp/

打印第1列含J的行

awk ‘$1 ~ /J/’ inventory-shipped

Jan 13 25 15 115

Jun 31 42 75 492

Jul 24 34 67 436

Jan 21 36 64 620

2、转义字符

有些字符需要转义,如双引号”

awk ‘BEGIN { print “He said ”hi!” to her.” }’

下面是一些转义字符代表的意思:

\

代表

b

Backspace, Ctrl-h, ASCII code 8 (BS).

n

换行符

r

回车

t

TAB键

3、正则表达式操作符

抑制有特殊命令的字符,如$表示$本身。

^

表示字符开始

$

表示字符结束

.

表示单个字符

[…]

中括号表达式,匹配方括号号任一字符

[^ …]

反向匹配中括号内任一字符

|

或,如^P|M匹配P开头或M

(…)

对字符分组,里面可使用|,如@(samp|code){[^}]+}匹配@code{foo}和@samp{bar}

*

匹配之前字符的0个或更多

+

与*相比,只是匹配1个或更多

?

匹配前面字符的0个或1个

{i}

匹配前面字符的i个,i是整数

{i,j}

匹配前面字符的i至j个,包含i和j

{i,}

匹配前面字符的i个或i个以上

4、gawk特有的正则操作符

s

表示空格字符

S

非空格字符

w

匹配字母或数字或下划线或汉字等

W

匹配任意不是字母,数字,下划线,汉字的字符

/匹配stow而不匹配stowaway

y

5、大小写敏感

x = “aB”

if (x ~ /ab/) … # this test will fail

IGNORECASE = 1

if (x ~ /ab/) … # now it will succeed

IGNORECASE = 1可以使用在命令行中,也可以使用在BEGIN中

三、读取输入文件

awk读取文件是一行行读取,并默认以空格分割成一个个字段。

涉及读取文件的有几个变量:

FS:

字段分隔符,默认是空格

RS:

记录分隔符,默认是换行符

NF:

记录每行的字段数,变量。

NR:

记录数,变量。

1、打印linux所有用户名

awk ‘BEGIN {FS=”:” } {print $1}’ /etc/passwd

2、打印最后一列

awk ‘BEGIN {FS=”:” } {print $NF}’ /etc/passwd

3、纵向排列每个字段

awk ‘BEGIN {RS=”:”} {print}’ /etc/passwd

4、统计行数

awk ‘END {print NR}’ /etc/passwd

四、打印输出

1、print使用方法

语法:print item1, item2, …

示例:

awk ‘BEGIN { print “line onenline twonline three” }’

awk ‘{ print $1, $2 }’ inventory-shipped

2、输出分隔符

OFS:

输出字段分隔符

ORS:

输出记录分隔符

以百分号分隔字段,记录隔一空白行输出。

awk ‘BEGIN{FS=”:”;OFS=”%”;ORS=”nn”} {print $1,$2}’ /etc/passwd

3、printf用法

语法:printf format, item1, item2, …

格式符号:

%c

转换数字成ASCII,如printf “%c”, 65结果为A。

%d, %i

打印十进制整数,如printf “%dn”, 6.5’结果为6.。

%e, %E

转换数字为科学(指数)符号,如printf “%4.3en”, 1950结果为1.950e+03。

%f

以浮点表示法打印数字,如 printf “%4.3f”, 1950结果为1950.000

%s

打印字符串,如printf “%10sn”, 1950,结果为十个空格加1950。

可更改的格式:

N$

位置指示符,可调整字符串的输出位置。printf “%s %s %sn”, “linux”, “like”,”I”输出为:linux like I,我们调整一下位置,printf “%3$s %2$s %1$sn”, “linux”, “like”,”I”,输出结果为:I like linux

负号,用在宽度前面,用来设置左对齐,因为默认是右对齐,如printf “%-4s”, “foo”,输出则是向左对齐了。

空格

待解

+

待解

示例:

1)第1列10个宽度并向左对齐:

$ awk ‘{ printf “%-10s %sn”, $1, $2 }’ BBS-list

-| aardvark 555-5553

-| alpo-net 555-3412

-| barfly 555-7685

-| bites 555-1675

-| camelot 555-0542

-| core 555-2912

-| fooey 555-1234

-| foot 555-6699

-| macfoo 555-6480

-| sdace 555-3430

-| sabafoo 555-2127

4、print和printf输出重定向

print items > output-file

保存items到文件,如分别保存用户和家目录,awk -F: ‘{ print $1 > “username”;print $6 > “home” }’ /etc/passwd

print items | command

管道重定向items到命令,如统计用户数量,awk -F: ‘{ print $1 |”wc -l” }’ /etc/passwd

五、表达式

1、操作符

算术操作符

– x

负运算

+ x

正运算,转换成数字。

x ^ y

x ** y

指数运算。

x * y

相乘。

x / y

相除,结果为浮点数字,如3 / 4 为:0.75

x + y

相加

x – y

相减

赋值

lvalue += increment Adds increment to the value of lvalue.

lvalue -= decrement Subtracts decrement from the value of lvalue.

lvalue *= coefficient Multiplies the value of lvalue by coefficient.

lvalue /= divisor Divides the value of lvalue by divisor.

lvalue %= modulus Sets lvalue to its remainder by modulus.

lvalue ^= power

lvalue **= power Raises lvalue to the power power. (c.e.)

递增和递减操作

++lvalue

Increment lvalue, returning the new value as the value of the expression.

lvalue++

Increment lvalue, returning the old value of lvalue as the value of the expression.

–lvalue

Decrement lvalue, returning the new value as the value of the expression. (This expression is like ‘++lvalue’, but instead of adding, it subtracts.)

lvalue–

Decrement lvalue, returning the old value of lvalue as the value of the expression. (This expression is like ‘lvalue++’, but instead of adding, it subtracts.)

2、真值与条件

在awk中,任何非0数值或非空字符串值为真,其它的值(0或空字符串)为假。

BEGIN {

if (3.1415927)

print “A strange truth value”

if (“Four Score And Seven Years Ago”)

print “A strange truth value”

if (j = 57)

print “A strange truth value”

}

变量类型与比较表达式

$ echo ‘ +3.14’ | gawk ‘{ print $0 == ” +3.14″ }’ True

-| 1

$ echo ‘ +3.14’ | gawk ‘{ print $0 == “+3.14” }’ False

-| 0

$ echo ‘ +3.14’ | gawk ‘{ print $0 == “3.14” }’ False

-| 0

$ echo ‘ +3.14’ | gawk ‘{ print $0 == 3.14 }’ True

-| 1

$ echo ‘ +3.14’ | gawk ‘{ print $1 == ” +3.14″ }’ False

-| 0

$ echo ‘ +3.14’ | gawk ‘{ print $1 == “+3.14” }’ True

-| 1

$ echo ‘ +3.14’ | gawk ‘{ print $1 == “3.14” }’ False

-| 0

$ echo ‘ +3.14’ | gawk ‘{ print $1 == 3.14 }’ True

-| 1

比较运算符

Expression Result

x < y True if x is less than y.

x y True if x is greater than y.

x >= y True if x is greater than or equal to y.

x == y True if x is equal to y.

x != y True if x is not equal to y.

x ~ y True if the string x matches the regexp denoted by y.

x !~ y True if the string x does not match the regexp denoted by y.

subscript in array True if the array array has an element with the subscript subscript.

布尔表达式

boolean1 && boolean2

boolean1 和boolean2 两个为真时,整个表达式才为真。

boolean1 || boolean2

至少一个为真,此表达式为真。

! boolean

boolean为真时,此表达式为假。

条件表达式

selector ? if-true-exp : if-false-exp

x >= 0 ? x : -x

x>=0时,x的值不变,x<=0时,x=-x。

3、函数调用

awk ‘{ print “The square root of”, $1, “is”, sqrt($1) }’

4、操作优先级

下面的操作符,由高到低排列:

(…)

分组

$

字段引用

++ —

递增,增减

^ **

取幂

+ – !

加,减,逻辑非

* / %

乘,除,取余

+ –

加,减

字符连接

没有特殊的符号,仅仅根据并排写。

< >= >> | |&

~ !~

匹配,不匹配

in

数组成员

&&

逻辑与

||

逻辑或

?:

条件。

= += -= *= /= %= ^= **=

赋值。

六、模式,动作,变量

1、模式元素

/regular expression/

/foo|bar|baz/ { buzzwords++ }

expression

$ awk ‘$1 == “foo” { print $2 }’ BBS-list

$ awk ‘$1 ~ /foo/ { print $2 }’ BBS-list

$ awk ‘/2400/ && /foo/’ BBS-list

$ awk ‘/2400/ || /foo/’ BBS-list

pat1, pat2

awk ‘$1 == “on”, $1 == “off”‘ myfile

BEGIN

END

$ awk ‘

> BEGIN { print “Analysis of ”foo”” }

> /foo/ { ++n }

> END { print “”foo” appears”, n, “times.” }’ BBS-list

-| Analysis of “foo”

-| “foo” appears 4 times.

BEGINFILE

ENDFILE

Special patterns for you to supply startup or cleanup actions to done on a per file basis. (See BEGINFILE/ENDFILE.)

empty

匹配所有输入。

2、在程序中使用shell变量

printf “Enter search pattern: ”

read pattern

awk -v pat=”$pattern” ‘$0 ~ pat { nmatches++ }

END { print nmatches, “found” }’ /path/to/data

3、动作

[pattern] { action }

pattern [{ action }]

function name(args) { … }

一个动作可以由一个语句或多个语句组合,包含在大括号里。各语句可以由新行或者分号分隔。默认的动作是打印记录。

Awk支持如下语句:

表达式:

调用函数或给变量赋值。

控制语句:

if, for, while,do

复合语句:

if, while, do的组合。

输入语句:

Getline

输出语句:

Print和printf

删除语句:

删除数组。

4、控制语句 in Actions

If-else语句:

if (condition) then-body [else else-body]

if (x % 2 == 0)

print “x is even”

else

print “x is odd”

While语句:

while (condition)

Body

awk ‘{

i = 1

while (i <= 3) {

print $i

i++

}

}' inventory-shipped

Do-while语句:

do

body

while (condition)

{

i = 1

do {

print $0

i++

} while (i <= 10)

}

For语句:
for (initialization; condition; increment)

Body

awk ‘{

for (i = 1; i <= 3; i++)

print $i

}' inventory-shipped

Switch语句:

switch (expression) {

case value or regular expression:

case-body

default:

default-body

}

switch (NR * 2 + 1) {

case 3:

case “11”:

print NR – 1

break

case /2[[:digit:]]+/:

print NR

default:

print NR + 1

case -1:

print NR * -1

}

Break语句:

# find smallest divisor of num

{

num = $1

for (div = 2; div * div num) {

printf “%d is primen”, num

break

}

}

}

Continue语句:

只在for语句里面使用。

BEGIN {

for (x = 0; x <= 20; x++) {

if (x == 5)

continue

printf "%d ", x

}

print ""

}

Next语句:

强制awk立即停止处理当前记录,而处理下一条记录。

NF != 4 {

err = sprintf(“%s:%d: skipped: NF != 4n”, FILENAME, FNR)

print err > “/dev/stderr”

next

}

Exit语句:

exit [return code]

BEGIN {

if ((“date” | getline date_now) “/dev/stderr”

exit 1

}

print “current date is”, date_now

close(“date”)

}

5、内置变量

用来控制awk的内置变量:

FS

字段分隔符,默认是空格

IGNORECASE

IGNORECASE为非0或者非空,则大小写不敏感。

OFS

输出字段分隔符。

ORS

输出记录分隔符。

RS

记录分隔符。

传递信息的内置变量:

FNR

当前文件记录数,当一个新文件读入时,清空此变量。

NF

字段数量

NR

记录数,新文件读入时不清空。

七、函数

http://www.gnu.org/software/gawk/manual/gawk.html#Functions

官方文档:http://www.gnu.org/software/gawk/manual/gawk.html

sed学习笔记

一、sed的行选择

打印第三行(3表示等号,p表示打印)

  1. sed -n ‘3p’ /etc/passwd

打印2至5行

  1. sed -n ‘2,5p’ /etc/passwd

从第2行开始,每隔2行打印一行

  1. sed -n ‘2~3p’ /etc/passwd

打印最后一行

  1. sed -n ‘$p’ /etc/passwd

打印包含或不包含root的行

  1. sed -n ‘/root/p’ /etc/passwd
  2. sed -n ‘/root/! p’ /etc/passwd

打印包含/bin/bash的行(%regexp%可以不用转义斜杠)

  1. sed -n ‘%/bin/bash%p’ /etc/passwd

忽略大小写的搜索

  1. sed -n ‘%/BIN/bash%Ip’ /etc/passwd或sed -n ‘//BIN/bash/Ip’ /etc/passwd

搜索从开始到sshd字符的范围

  1. sed -n ‘0,/sshd/p’ /etc/passwd

二、sed的正则表达式语法

char
只匹配本身char
*
匹配之前字符的0个或更多
+
与*相比,只是匹配1个或更多
?
匹配前面字符的0个或1个
{i}
匹配前面字符的i个,i是整数
{i,j}
匹配前面字符的i至j个,包含i和j
{i,}
匹配前面字符的i个或i个以上
(regexp)
分组字符,两个作用1、(abcd)*会搜索0个或多个abcd字符,abcd是作为一个整体2、作为反向引用,可以使用1 2等引用。
.
匹配任何字符,包括换行符
^
匹配行的开始
$
匹配行的结束
[list]
匹配list中的任单字符,如l,i,s,t
[^list]
反向匹配l,i,s,t
regexp1|regexp2
匹配regexp1或者regexp2
regexp1regexp2
匹配regexp1与regexp2连接
n
匹配换行字符
char
匹配特殊字符,如$, *, ., [, , ,^

三、常用命令

在之前sed的行选择我们使用了p,即打印命令,下面介绍更多的命令
q
退出命令,sed ’11 q’ /etc/passwd 相当于head /etc/passwd
d
删除命令,sed ‘1 d’ /etc/passwd 删除第一行
p
打印命令,一般与-n选项连用

四、替换命令s

s是sed中最常用到且最重要的命令了,下面详细介绍此命令的用法。
sed最简单的用法如下:
把root替换成rooter
sed ‘s/root/rooter/’ /etc/passwd

命令的格式为:s/regexp/replacement/flags

replacement可以是n(n为1到9的数字),表示引用regexp里的(matching):如
echo “123abc” | sed ‘s/([0-9]*).*/1/’
([0-9]*).*匹配了123abc,而([0-9]*)匹配123,所以1内容为123,所以这个例子的结果为123。

replacement也可以是&,表示reqexp匹配到的内容,如:
echo “123 abc” | sed ‘s/[0-9]*/& &/’
结果为:
123 123 abc

replacement还可以是L, l, U, u, E,意思如下:
L
把replacement字母转换成小写,直到U或E出现。
l
把下个字符转换成小写。
U
把replacement字母转换成大写,直到L或E出现。
u
把下个字符转换成大写。
E
停止以L或U开始的大小写转换。
一些例子如下:

  1. 1、把ABC转换为小写
  2. echo "ABC" | sed ‘s/[A-Z]*/L&E/’ 或
  3. echo "ABC" | sed ‘s/[A-Z]/l&/g’
  4. 把abc转换为大写
  5. echo "abc" | sed ‘s/[a-z]*/U&E/’ 或
  6. echo "abc" | sed ‘s/[a-z]/u&/g’

flags的可选值如下:
g
将replacement应用于所有reqexp所匹配的,不仅仅是第一个的匹配。
number
只替换第n个的匹配
p
当替换已经完成,打印新的结果
w file-name
当替换完成,把新结果保存到文件
I
i
忽略大小写。
M
m
使^和$为空字符

五、少用的命令

y/source-chars/dest-chars/
source-chars与dest-chars长度要相同,转换是一一对应的,如a转换成A,b转换成B:sed ‘y/ab/AB/’

a
text
在指定位置增加内容,如在含有root下一行增加I am root:
sed ‘/root/ aI am root’ /etc/passwd

i
text
在指定位置插入内容,如在含有root上一行增加I am root:
sed ‘/root/ iI am root’ /etc/passwd

c
text
用text替换匹配的行,如把含有root的行内容替换为I am root:
sed ‘/root/ cI am root’ /etc/passwd
=
打印行号,如显示含有root所在的行号:sed -n ‘/root/=’ /etc/passwd

l n
以n个长度分割匹配到的行,并以结尾,如以长度为4个字符分隔含有root的行:sed ‘/root/ l 4’ /etc/passwd

r filename
读取文件到输出,如在含有root的行下面插入file文件:sed ‘/root/r file’ /etc/passwd

w filename
保存输出流到文件,如保存含有root的行到file文件:sed ‘/root/w file’ /etc/passwd

N
增加一行并附下一行内容,如两行合并成一行:sed ‘N;s/n//’ /etc/passwd

六、一些示例

文本间隔:
——–
# 在每一行后面增加一空行

  1. sed G /etc/passwd

# 将原来的所有空行删除并在每一行后面增加一空行。
# 这样在输出的文本中每一行后面将有且只有一空行。

  1. sed ‘/^$/d;G’

# 在每一行后面增加两行空行

  1. sed ‘G;G’

# 在匹配式样“regex”的行之后插入一空行

  1. sed ‘/regex/G’

编号:
——–
# 为文件中的每一行进行编号(简单的左对齐方式)。这里使用了“制表符”
# (tab,见本文末尾关于’t’的用法的描述)而不是空格来对齐边缘。

  1. sed = /etc/passwd | sed ‘N;s/n/t/’

# 对文件中的所有行编号,但只显示非空白行的行号。

  1. sed ‘/./=’ filename | sed ‘/./N; s/n/ /’

# 计算行数 (模拟 “wc -l”)

  1. sed -n ‘$=’

选择性地显示特定行:
——–
# 显示文件中的前10行 (模拟“head”的行为)

  1. sed 10q /etc/passwd

# 显示文件中的第一行 (模拟“head -1”命令)

  1. sed q

# 只显示匹配正则表达式的行(模拟“grep”)

  1. sed -n ‘/regexp/p’               # 方法1
  2.  sed ‘/regexp/!d’                 # 方法2

# 显示包含“AAA”、“BBB”或“CCC”的行(任意次序)

  1. sed ‘/AAA/!d; /BBB/!d; /CCC/!d’  # 字串的次序不影响结果

# 显示包含65个或以上字符的行

  1. sed -n ‘/^.{65}/p’

# 显示包含65个以下字符的行

  1. sed -n ‘/^.{65}/!p’            # 方法1,与上面的脚本相对应
  2.  sed ‘/^.{65}/d’                # 方法2,更简便一点的方法

# 从第3行开始,每7行显示一次

  1. sed -n ‘3~7p’                   # 只对GNU sed有效
  2.  sed -n ‘3,${p;n;n;n;n;n;n;}’     # 其他sed

示例摘自:http://sed.sourceforge.net/sed1line_zh-CN.html
官方文档:http://www.gnu.org/software/sed/manual/sed.html

DSN: Service unavailable

自从换了vps,博客的评论邮件提醒功能一直有问题,今天有空,我们来解决它。
无法发送邮件的日志如下:

  1. Jun 26 07:24:23 MyVPS1976 sendmail[31760]: q5PNOMeP031760: from=<www@MyVPS1976>, size=1393, class=0, nrcpts=1, msgid=<[email protected]>, proto=SMTP, daemon=MTA, relay=MyVPS [127.0.0.1]
  2. Jun 26 07:24:25 MyVPS1976 sendmail[31762]: q5PNOMeP031760: to=<[email protected]>, ctladdr=<www@MyVPS1976> (501/501), delay=00:00:02, xdelay=00:00:02, mailer=esmtp, pri=121393, relay=mxdomain.qq.com. [64.71.138.90], dsn=5.0.0,stat=Service unavailable
  3. Jun 26 07:24:25 MyVPS1976 sendmail[31762]: q5PNOMeP031760: q5PNOPeP031762: DSN: Service unavailable
  4. Jun 26 07:24:25 MyVPS1976 sendmail[31762]: q5PNOPeP031762: to=root, delay=00:00:00, xdelay=00:00:00, mailer=local, pri=32578, dsn=2.0.0, stat=Sent

根据relay=mxdomain.qq.com. [64.71.138.90], dsn=5.0.0,stat=Service unavailable这一段,我们知道邮件已经发送出去,但由于某种原因邮件被拒绝,于是更换hostname,重启sendmail,解决问题。
更换hostname方法:
1、编辑/etc/sysconfig/network,更换文件中的hostnmae。
2、把hostname写入/etc/hosts
3、执行hostname devops.webres.wang立即生效

rpmbuild在centos 5与centos 6用法的异同

之前是在centos 5上对软件进行rpm打包,今天需要在centos 6上打包,发现File not found: /root/rpmbuild/BUILDROOT/…的错误,看是centos 6中的rpmbuild topdir已经改变,为了能兼容centos 5的spec文件,需要对topdir进行修改:
打开/usr/lib/rpm/macros文件:

  1. %_topdir                %{getenv:HOME}/rpmbuild

更改为:

  1. %_topdir                %{_usrsrc}/redhat

另外还需要定义buildroot
在spec文件中的make install后面加上DESTDIR=%{buildroot},即:

  1. make install DESTDIR=%{buildroot}

DESTDIR是Makefile文件中定义的一个安装路径的变量,根据实际情况修改,比如mysql和nginx的是DESTDIR,而php的是INSTALL_ROOT。

openssl undefined reference to `SSLv2_client_method’

今天在Ubuntu 11.10编译php-5.2.17的时候出现如下的错误:

  1. php-5.2.17/ext/openssl/xp_ssl.c:357: undefined reference to `SSLv2_server_method’
  2. php-5.2.17/ext/openssl/xp_ssl.c:337: undefined reference to `SSLv2_client_method’
  3. collect2: ld returned 1 exit status
  4. make: *** [sapi/cgi/php-cgi] 错误 1

这个需要一个补丁禁用openssl的SSLv2_client_method,方法如下:

  1. cd php-5.2.17/
  2. wget http://devops.webres.wang/wp-content/uploads/2012/06/debian_patches_disable_SSLv2_for_openssl_1_0_0.patch
  3. patch -p1 < debian_patches_disable_SSLv2_for_openssl_1_0_0.patch

然后再重新编译php

  1. make clean
  2. make && make install

Linux源码编译安装rrdtool

1、定义变量

  1. BUILD_DIR=/tmp/rrdbuild
  2. INSTALL_DIR=/usr/local/rrdtool
  3. mkdir -p $BUILD_DIR
  4. cd $BUILD_DIR

2、安装rrdtool。

  1. wget http://oss.oetiker.ch/rrdtool/pub/rrdtool.tar.gz
  2.  gunzip -c rrdtool-x.y.z.tar.gz | tar xf –
  3.  cd rrdtool-x.y.z
  4.  ./configure –prefix=$INSTALL_DIR && make && make install

如果安装失败,请继续第三步安装所需依赖。
3、安装依赖
定义变量

  1. export LDFLAGS="-Wl,–rpath -Wl,${INSTALL_DIR}/lib"
  2. export PKG_CONFIG_PATH=${INSTALL_DIR}/lib/pkgconfig
  3. export PATH=$INSTALL_DIR/bin:$PATH

3.1、安装pkgconfig

  1. cd $BUILD_DIR
  2. wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.26.tar.gz
  3. gunzip -c pkg-config-0.26.tar.gz | tar xf –
  4.  cd pkg-config-0.26
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC"
  6.  make
  7.  make install
  8. export PKG_CONFIG=$INSTALL_DIR/bin/pkg-config

3.2、安装zlib

  1. cd $BUILD_DIR
  2. wget http://oss.oetiker.ch/rrdtool/pub/libs/zlib-1.2.3.tar.gz
  3.  gunzip -c zlib-1.2.3.tar.gz | tar xf –
  4.  cd zlib-1.2.3
  5.  ./configure –prefix=$INSTALL_DIR  –shared
  6.  make
  7.  make install

3.3、安装libpng

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/libpng-1.2.18.tar.gz
  3.  gunzip -c libpng-1.2.18.tar.gz | tar xf –
  4.  cd libpng-1.2.18
  5.  env CFLAGS="-O3 -fPIC" ./configure –prefix=$INSTALL_DIR
  6.  make
  7.  make install

3.4、安装freetype

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/freetype-2.3.5.tar.gz
  3.  gunzip -c freetype-2.3.5.tar.gz | tar xf –
  4.  cd freetype-2.3.5
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC"
  6.  make
  7.  make install

3.5、安装LibXML2

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/libxml2-2.6.32.tar.gz
  3.  gunzip -c libxml2-2.6.32.tar.gz | tar xf –
  4.  cd libxml2-2.6.32
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC"
  6.  make
  7.  make install

3.6、安装fontconfig

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/fontconfig-2.4.2.tar.gz
  3.  gunzip -c fontconfig-2.4.2.tar.gz   | tar xf –
  4.  cd fontconfig-2.4.2
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC" –with-freetype-config=$INSTALL_DIR/bin/freetype-config
  6.  make
  7.  make install

3.7、安装Pixman

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/pixman-0.10.0.tar.gz
  3.  gunzip -c pixman-0.10.0.tar.gz  | tar xf –
  4.  cd pixman-0.10.0
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC"
  6.  make
  7.  make install

3.8、安装Cairo

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/cairo-1.6.4.tar.gz
  3.  gunzip -c cairo-1.6.4.tar.gz   | tar xf –
  4.  cd cairo-1.6.4
  5.  ./configure –prefix=$INSTALL_DIR
  6.     –enable-xlib=no
  7.     –enable-xlib-render=no
  8.     –enable-win32=no
  9.     CFLAGS="-O3 -fPIC"
  10.  make
  11.  make install

3.9、安装Glib

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/glib-2.15.4.tar.gz
  3.  gunzip -c glib-2.15.4.tar.gz  | tar xf –
  4.  cd glib-2.15.4
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC"
  6.  make
  7.  make install

3.10、安装Pango

  1. cd $BUILD_DIR
  2.  wget http://oss.oetiker.ch/rrdtool/pub/libs/pango-1.21.1.tar.bz2
  3.  bunzip2 -c pango-1.21.1.tar.bz2 | tar xf –
  4.  cd pango-1.21.1
  5.  ./configure –prefix=$INSTALL_DIR CFLAGS="-O3 -fPIC" –without-x
  6.  make
  7.  make install

3.11、重新安装rrdtool

  1. cd $BUILD_DIR/rrdtool-1.4.6
  2. ./configure –prefix=$INSTALL_DIR
  3.  make clean
  4.  make
  5.  make install
  6. ln -s /usr/local/rrdtool/bin/* /usr/local/bin/

WARNING: Unable to find a corresponding IP address for xxx

在之前开始使用fail2ban防止vsftpd或ssh暴力破解之后,有一定的效果,但在查看fail2ban.log日志时,发现一个问题:

  1. fail2ban.filter : WARNING Unable to find a corresponding IP address for ns.rs996.com

我们再查看secure日志:

  1. vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=devops.webres.wang rhost=ns.rs996.com

fail2ban是根据rhost取得客户端的ip,并使用iptables禁止它,但我们发现ns.rs996.com无法解析出ip地址,那它的ip去哪了呢?
经过分析得出结论:vsftpd默认对客户端ip进行反向解析,如果查询有结果,则把ip解析出来的域名记录secure日志中的rhost中,但是此反向解析出来的域名,本身又没有作A记录的解析,所以fail2ban再进行此域名的解析时,当然就无法获取到IP地址,也就无法禁止此客户端的暴力破解行为。
解决方法:
在vsftpd.conf中添加下面的代码禁止反向解析:

  1. reverse_lookup_enable=NO

之后重启vsftpd即可。生效之后不仅解决了这个问题,也使得登录vsftpd不再停顿几秒了。
但是/var/log/secure里记录ftp登录失败有一个缺点,就是用相同的用户名,不同的密码登录多次,只是记录首次失败的信息,而之后的就只显示:last message repeated x times这样的信息,无法满足登录失败三次的条件,所以就不能阻止此暴力破解,下面是解决这个问题的方法:
1、打开vsftpd.conf,添加如下代码:

  1. dual_log_enable=YES
  2. use_localtime=YES

2、打开/etc/fail2ban/jail.local,修改以下内容:

  1. [vsftpd-iptables]
  2. logpath = /var/log/vsftpd.log

编译mysql出现的问题undefined reference to tgoto|tputs

部分编译参数为:
./configure
–enable-assembler
–with-mysqld-ldflags=”-all-static”
–with-client-ldflags=”-all-static”
出现如下错误:
../cmd-line-utils/libedit/libedit.a(term.o): In function `term_echotc’:
term.c:(.text+0×1557): undefined reference to `tputs’
term.c:(.text+0×1580): undefined reference to `tgetstr’
term.c:(.text+0×1676): undefined reference to `tgoto’
term.c:(.text+0x169a): undefined reference to `tputs’
term.c:(.text+0×1761): undefined reference to `tgoto’
term.c:(.text+0×1781): undefined reference to `tputs’
解决方法:
./configure
–enable-assembler
–with-mysqld-ldflags=”-all-static”
–with-client-ldflags=”-all-static -ltinfo”
在–with-client-ldflags增加 -ltinfo。

centos-6 /usr/bin/ld: cannot find lc

在centos 6 64位系统上编译mini_sendmail时出现/usr/bin/ld: cannot find lc错误,查找了一下发现libc.so是存在的,于是使用strace -o -f lc.strace make语句跟踪了一下,发现缺少libc.a文件,而在centos 5就没有这种情况,执行以后命令安装:

  1. yum install glibc-static