2014年6月

使用tcpdump抓包

基本上都是摘抄网上的实例,原文连接在最后。
Tcpdump 中的关键字主要有以下几种
1.关于类型的关键字主要包括:host、net、port
2.确定传输方向的关键字主要包括:src、dst、dst or src、 dst and src。
3.第三种是协议的关键字,主要包括ip、arp、rarp、tcp、udp、icmp等类型。
4.其他重要的关键字:gateway, broadcast,less,greater。
5.三种逻辑运算:取非运算是'not ' ,'! ';与运算是'and','&&';或运算 是'or','││'。多条件时可以用括号,但是要用\转义。

- 阅读剩余部分 -

ipad越狱了

2014年6月27日对国内的果粉来说可能是一个新的里程碑,因为在这一天,盘古团队打破了国外黑客对越狱的垄断,发布了自己的越狱工具,此次越狱共用了6个漏洞,基本上主要漏洞都是盘古团队发现的,不得不说确实是给国人长脸了。我不是果粉,买了IPad后发现,我对苹果的硬件很喜欢,对系统就有很多不爽的地方了,例如没有文件管理器,我用迅雷下个文档,还要用迅雷打开,蓝牙基本上是鸡肋等等等等。Ipad买到手之后就是IOS 7.1的系统,所以这次工具一出,观望了一天看到大家反响都还不错,就马上试了一下,整个过程很简单,真正的一键越狱。接下来就是慢慢改进IOS系统了,感谢盘古团队!

yum报错小记

新系统centos6.5,按照之前的方法安装epel源:
http://opswill.com/articles/apache-rRedirecting-base-on-geoip.html

后无论安装什么软件都会报错:

[root@localhost ~]# yum install ansible Loaded plugins: fastestmirror,
refresh-packagekit, security Loading mirror speeds from cached
hostfile Error: Cannot retrieve metalink for repository: epel. Please
verify its path and try again

google了一下,解决方法:
编辑:/etc/yum.repos.d/epel.repo
将所有的

#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch       mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch

取消所有的baseurl的注释,并注释掉mirrorlist即可。
之后 yum makecache.

参考文章:
http://www.rackspace.com/knowledge_center/article/installing-rhel-epel-repo-on-centos-5x-or-6x
http://teddysun.com/153.html

tomcat使用apr模式

优化tomcat时都会更改tomcat的运行模式,tomcat支持三种模式:BIO、NIO、APR。默认的BIO性能最差;NIO模式是内置的,使用的是java的异步io,性能强于bio,并且简单改一下配置即可使用;APR模式是使用JNI(Java Native Interface)提升静态文件和SSL的效率,是三种模式中性能最强的,也是配置最“复杂”的。这三种模式的原理和性能这里就不多说了,tomcat官网和网上都有很多说明和测评,这里记录一下APR的安装过程

需要一下三个库:

  1. APR library
  2. JNI wrappers for APR used by Tomcat (libtcnative)
  3. OpenSSL libraries

1.下载:
apr相关组件下载地址:https://apr.apache.org/download.cgi
wget http://psg.mtu.edu/pub/apache//apr/apr-1.5.1.tar.gz
wget http://psg.mtu.edu/pub/apache//apr/apr-util-1.5.3.tar.gz
libtcnative 这个不用下载,在tomcat的bin目录下有;openssl可以用系统的包管理安装,不用专门下载;有些特殊情况下还要编译apr-iconv这个包。

2.编译安装
a.用系统的包管理安装openssl组件,centos系统使用:

yum install openssl openssl-devel

debian系列使用:

apt-get install openssl openssl-dev

b.开始编译,先编译apr再编译apr-util,使用经典三步来编译:
apr :

./configure && make && make install

在编译apr-util时要指定apr的路径

configure  --with-apr=/usr/local/apr/ && make && make install

c.编译libtcnative,解压tomcat的bin目录下的tomcat-native.tar

./configure  --with-apr=/usr/local/apr/ --with-java-home=/opt/JDK1.6/ && make && make install

3.配置tomcat使用apr
a.在tomcat的bin目录下的catalina.sh中添加

CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=/usr/local/apr/lib"

b.修改server.xml文件
修改虚拟主机的protocol为: protocol="org.apache.coyote.http11.Http11AprProtocol"
上面这些步骤做完之后就可以启动tomcat了,如果在catalina.sh中没有异常且有类似下面的日志就说明配置成功了

un 17, 2014 12:29:06 PM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.29 using APR version 1.5.0.
Jun 17, 2014 12:29:06 PM org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Jun 17, 2014 12:29:07 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized (OpenSSL 1.0.0-fips 29 Mar 2010)
Jun 17, 2014 12:29:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-8080"]
Jun 17, 2014 12:29:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-apr-8009"]
Jun 17, 2014 12:29:07 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 653 ms

注意:
1.tomcat模式改为apr之后,配置https时不在是keystore+password的方式,而是和apache一样的crt+key方式了。
2.如果觉得apr模式麻烦,可以使用NIO模式,直接将protocol改为: protocol="org.apache.coyote.http11.Http11NioProtocol"即可,不需要编译任何东西。为追求性能只靠更改tomcat运行模式是不够的,使用apache+tomcat、nginx+tomcat搞横向集群或者纵向集群才是靠谱的方案。
参考文章:
http://tomcat.apache.org/tomcat-6.0-doc/apr.html#Linux
http://hi.baidu.com/injava/item/b896db21028c8e102a0f1c40
http://phl.iteye.com/blog/910996

nginx使用GeoIP限制访问并支持白名单

要使用GeoIP,需要重新编译Nginx,我的系统是centos6.5,nginx用的是tengine,需要的软件包:
gcc、gcc-c++、 openssl、 openssl-devel、geoIP library、GeoLite Country、GeoLite City、pcre、tengine2

1.下载需要的软件包

wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.zip

2.安装编译用到的软件

yum install gcc gcc-c++ openssl openssl-devel

3.编译GeoIP library

gunzip GeoIP.tar.gz && tar -xvf GeoIP.tar && cd GeoIP-1.4.8 
./configure && make && make install

如果不编译GeoIP library,在编译nginx时会提示

the GeoIP module requires the GeoIP library

4.编译nginx
先解压pcre在执行已下命令:

./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --without-select_module --without-poll_module --with-http_geoip_module --with-http_ssl_module --with-openssl-opt=enable-tlsext --with-pcre=../pcre-8.33
make && make install

5.配置GeoIP

gunzip GeoLiteCity.dat.gz  && gunzip GeoIP.dat.gz

将这两个解压后的库文件移到nginx的conf目录下,之后在nginx.conf中加入:

geoip_country       /usr/local/nginx/conf/GeoIP.dat;
geoip_city          /usr/local/nginx/conf/GeoLiteCity.dat;
#geoIP的白名单
geo $remote_addr $ip_whitelist {
    default 0;
    include ip.conf;
}
#map $geoip_country_code $allowed_country {
#default no;
#CN yes;
#HK yes;
#}

在要使用geoIP的虚拟主机中的location中加入GeoIP配置,这里直接贴一个配置

location / {
        proxy_redirect         off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        #白名单配置
        if ($ip_whitelist = 1) {
            proxy_pass http://web;
            break;
        }
        #屏蔽的国家返回403
        if ($geoip_country_code ~ "(HK|TW|PH|MO|US)") {
            return 403;
        }
        #if ($allowed_country = no) {
        #    return 403;
        #}

        proxy_pass http://web;
    }

在conf下新建一个ip.conf作为Geoip的白名单,支持ip段,内容和格式为:

8.8.8.8 1;
8.8.8.8/24 1;

检查配置

/usr/local/nginx/sbin/nginx -t

如果是64位系统可能会报:

/nginx: error while loading shared libraries: libGeoIP.so.1: cannot open shared object file: No such file or directory

解决方法:

ln -s /usr/local/lib/libGeoIP.so* /lib64/

之后

ldd /usr/local/nginx/sbin/nginx 

确认下没有not found的库文件即可。
这样就配置好了nginx,并且通过GeoIP限制了国家和城市的访问,并且支持白名单。

kill -USR1 参数详解

高俊峰的《高性能linux服务器构建实战》中提到一个nginx的日志分割脚本

#!/bin/bash
savepath_log='/home/nginx/logs'
nglogs='/opt/nginx/logs'
mkdir -p $saveptah_logs/$(data +%Y)/$(date +%m)
mv $nglogs/access.log $savepath_log/$(data +%Y)/$(date +%m)/access.$(date +%Y%m%d).log
mv $nglogs/error.log  $savepath_log/$(data +%Y)/$(date +%m)/error.$(date +%Y%m%d).log
kill -USR1 `cat /opt/nginx/logs/nginx.pid`

脚本很简单,但是最后一行kill 的 -USR1 没有见过,kill -l 看了一下,-USR1 等于 kill -10,是用户定义的进程信号,在维基百科中查到:

USR1亦通常被用来告知应用程序重载配置文件;例如,向Apache HTTP服务器发送一个USR1信号将导致以下步骤的发生:停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改。

从脚本可以看出,当执行脚本时,会把access.log和error.log移到日志目录,所以原来nginx的logs目录下就没有access.log和error.log了,作者使用kill -USR1 nginx.pid 重新平滑的生成了这两个日志文件,简单说等于nginx -s reload.

参考文章:
http://zh.wikipedia.org/wiki/SIGUSR1%E5%92%8CSIGUSR2
《高性能linux服务器构建实战》 高俊峰 Page 18

ansible报错小记

安装ansible时报的错误:

/pycrypto-2.6.1/egg-dist-tmp-HelzWa
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
src/MD2.c:31:20: 错误:Python.h:没有那个文件或目录
src/MD2.c:131: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
In file included from src/MD2.c:147:
src/hash_template.c:48: 错误:expected specifier-qualifier-list before ‘PyObject_HEAD’
src/hash_template.c:59: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyTypeObject’
src/hash_template.c: 在函数‘newALGobject’中:
src/hash_template.c:69: 警告:隐式声明函数‘PyObject_New’
src/hash_template.c:69: 错误:expected expression before ‘ALGobject’
src/hash_template.c: 在文件层:
src/hash_template.c:76: 错误:expected ‘)’ before ‘*’ token
src/hash_template.c:91: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:110: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:122: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:162: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:188: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:190: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ALG_methods’
src/hash_template.c:199: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:225: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ALGtype’
src/hash_template.c:271: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
src/hash_template.c:304: 错误:数组元素的类型不完全
src/hash_template.c:305: 错误:‘PyCFunction’未声明(不在函数内)
src/hash_template.c:305: 错误:expected ‘}’ before ‘ALG_new’
src/hash_template.c: 在函数‘init_MD2’中:
src/hash_template.c:339: 错误:‘PyObject’未声明(在此函数内第一次使用)
src/hash_template.c:339: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
src/hash_template.c:339: 错误:所在的函数内也只报告一次。)
src/hash_template.c:339: 错误:‘m’未声明(在此函数内第一次使用)
src/hash_template.c:351: 错误:‘ALGtype’未声明(在此函数内第一次使用)
src/hash_template.c:351: 错误:‘PyType_Type’未声明(在此函数内第一次使用)
src/hash_template.c:352: 警告:隐式声明函数‘Py_InitModule’
src/hash_template.c:356: 错误:‘o’未声明(在此函数内第一次使用)
src/hash_template.c:356: 警告:隐式声明函数‘PyInt_FromLong’
src/hash_template.c:356: 警告:隐式声明函数‘PyDict_SetItemString’
src/hash_template.c:356: 警告:隐式声明函数‘PyModule_GetDict’
src/hash_template.c:356: 警告:隐式声明函数‘Py_DECREF’
src/hash_template.c:360: 警告:隐式声明函数‘PyErr_Occurred’
src/hash_template.c:361: 警告:隐式声明函数‘Py_FatalError’
error: Setup script exited with error: command 'gcc' failed with exit status 1

解决方法:
安装python-dev即可,虽然运行python setup.py install时还有警告,但是已经没有错误了
centos:

yum install python-devel

ubuntu:

apt-get install python-dev

使用nc 测试网络连接

在linux下经常需要测试服务器到其它服务器某个端口的连接情况,可以使用nc来测试,命令:

seq 100 | while read line; do nc -v -w 10 -z oracle.local 1521; done

把 oracle.local 1521 换成你想测试的主机和端口。
如果想结果更详细,可以用下面的命令把每次执行的时间输出:

seq 100 | while read line; do (time nc -v -w 10 -z www.google.com  80) 2>>/tmp/1.txt; done

在/tmp/1.txt下会看到下面的内容:

real 0m0.074s
user 0m0.005s
sys 0m0.004s

nc测试网络只是大炮打蚊子,附nc的一些参数说明:

-v:输出详细信息
-w:超时时间
-z:关闭输入/输出模式,常在扫描端口时使用,不发送任何数据包
-d 后台模式
-e prog 程序重定向,一旦连接,就执行 [危险!!]
-g <网关> 设置路由器跃程通信网关,最多可设置8个
-G <指向器数目> 设置来源路由指向器,其数值为4的倍数
-h 帮助信息
-i <延迟秒数> 设置时间间隔,以便传送信息及扫描通信端口
-l 监听模式,用于入站连接
-n 直接使用IP地址,而不通过域名服务器
-o <输出文件> 指定文件名称,把数据以16进制字码保存至文件
-p <通信端口> 设置本地主机使用的通信端口
-r 随机本地及远程端口
-s <来源位址> 设置本地主机送出数据包的IP地址
-t 使用TELNET交互方式
-u UDP模式
-v 详细输出--用两个-v可得到更详细的内容

如果只是测试网络联通性,tcping是个不错的工具,效果和上面的nc是一样的。

#tcping www.52os.net 80

Probing 180.76.3.151:80/tcp - Port is open - time=1.287ms
Probing 180.76.3.151:80/tcp - Port is open - time=0.838ms
Control-C
Ping statistics for 180.76.3.151:80
    2 probes sent.
    2 successful, 0 failed.
Approximate trip times in milli-seconds:
Minimum = 0.838ms, Maximum = 1.287ms, Average = 1.062ms

参考文章:
http://blog.sina.com.cn/s/blog_613596840100ym43.html

最新文章

最近回复

  • immortal: 大佬好强!!!
  • myogg: 您好,这个能不能将发...
  • 大青蛙子: 照着大佬的命令行复刻...
  • see: 把虚拟机平台下面其他...
  • see: 感觉是兼容性问题,我...
  • see: 做了。因为inter...
  • see: 遇到一个ipv6问题...
  • pluveto: 这功能很好
  • mgt: 从fail2ban的...
  • see: 设置容器 allo...

分类

归档

统计

  • 文章总数:169篇
  • 分类总数:5个
  • 评论总数:130条
  • 页面总数:173个
  • 本站运行:5200天

其它