<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gjw_apparitor 博客 &#187; Code</title>
	<atom:link href="http://www.apparitor.info/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.apparitor.info</link>
	<description>孤独是一种态度</description>
	<lastBuildDate>Wed, 16 May 2012 07:36:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>python 日志分析统计脚本</title>
		<link>http://www.apparitor.info/2012/03/19/python-%e6%97%a5%e5%bf%97%e5%88%86%e6%9e%90%e7%bb%9f%e8%ae%a1%e8%84%9a%e6%9c%ac/</link>
		<comments>http://www.apparitor.info/2012/03/19/python-%e6%97%a5%e5%bf%97%e5%88%86%e6%9e%90%e7%bb%9f%e8%ae%a1%e8%84%9a%e6%9c%ac/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 03:17:21 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=421</guid>
		<description><![CDATA[脚本类似shell的tail功能，用来统计每分钟的4xx、5xx的状态码数量，统计php执行时间小于1秒、1-5秒、5秒以上的数量，和每秒的并发请求。将结果放到/tmp目录下， 也可以用cacti将结果画图。 #!/usr/bin/env python import time, os #----------------------------- log_file = '/var/log/httpd/cmi_access_log' #----------------------------- log_name = log_file.split('/')[-1] file = open(log_file, 'r') lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0 Time2 = '0' st_results = os.stat(log_file) st_size = st_results[6] file.seek(st_size) size = os.path.getsize(log_file) while 1: Time = time.strftime("%Y_%m_%d",time.localtime(time.time() - 60)) where = file.tell() line = file.readline() L = line.split('"') Time1 = time.strftime("%Y_%m_%d_%H:%M",time.localtime(time.time() -]]></description>
			<content:encoded><![CDATA[<p>脚本类似shell的tail功能，用来统计每分钟的4xx、5xx的状态码数量，统计php执行时间小于1秒、1-5秒、5秒以上的数量，和每秒的并发请求。将结果放到/tmp目录下， 也可以用cacti将结果画图。</p>
<p><a href="http://www.apparitor.info/wp-content/uploads/2012/03/运维监控系统.png"><img class="alignnone size-medium wp-image-426" title="运维监控系统" src="http://www.apparitor.info/wp-content/uploads/2012/03/运维监控系统-300x154.png" alt="" width="300" height="154" /></a></p>
<pre>#!/usr/bin/env python
import time, os

#-----------------------------
log_file = '/var/log/httpd/cmi_access_log'
#-----------------------------

log_name = log_file.split('/')[-1]
file = open(log_file, 'r')
lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0
Time2 = '0'
st_results = os.stat(log_file)
st_size = st_results[6]
file.seek(st_size)
size = os.path.getsize(log_file)

while 1:
        Time = time.strftime("%Y_%m_%d",time.localtime(time.time() - 60))
        where = file.tell()
        line = file.readline()
        L = line.split('"')
        Time1 = time.strftime("%Y_%m_%d_%H:%M",time.localtime(time.time() - 60))
        if not line:
            size = os.path.getsize(log_file)
            if size &lt; where:
                file = open(log_file, 'r')
            else:
                time.sleep(1)
                file.seek(where)
        else:
            try:
                Phptime = float(L[-2])
                url = L[1]
                status = L[2][1:4]
                if '4' in status[0]:
                    status4 += 1
                elif '5' in status[0]:
                    status5 += 1
                if Time1 in Time2:
                    if 'f5.php' not in url:
                        concurrent += 1
                        if Phptime &lt; 1:
                                lt1 += 1
                        elif 1 &lt;= Phptime &lt;= 5:
                                to1_5 += 1
                        else:
                                gt5 += 1
                else:
                    concurrent = concurrent / 60
                    out = Time1 + "," + str(lt1) + "," + str(to1_5) + "," + str(gt5) + "," + str(status4) + "," + str(status5) + "," + str(concurrent)
                    F = open('/tmp/' + log_name + '_' + Time + '.data', 'a')
                    print &gt;&gt; F, out
                    F.close()
                    Time2 = Time1
                    lt1,to1_5,gt5,status4,status5,concurrent = 0,0,0,0,0,0
            except:
                pass</pre>
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2012/03/19/python-%e6%97%a5%e5%bf%97%e5%88%86%e6%9e%90%e7%bb%9f%e8%ae%a1%e8%84%9a%e6%9c%ac/">python 日志分析统计脚本</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2012/03/19/python-%e6%97%a5%e5%bf%97%e5%88%86%e6%9e%90%e7%bb%9f%e8%ae%a1%e8%84%9a%e6%9c%ac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SHELL 中几个特殊变量</title>
		<link>http://www.apparitor.info/2011/04/06/shell-%e4%b8%ad%e5%87%a0%e4%b8%aa%e7%89%b9%e6%ae%8a%e5%8f%98%e9%87%8f/</link>
		<comments>http://www.apparitor.info/2011/04/06/shell-%e4%b8%ad%e5%87%a0%e4%b8%aa%e7%89%b9%e6%ae%8a%e5%8f%98%e9%87%8f/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 06:18:43 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=211</guid>
		<description><![CDATA[$0 shell脚本的名字 $* shell的参数 $@ shell的参数 $# shell的参数个数 $_ shell的最后一个参数 $$ shell脚本自身进程的ID $! shell在后台运行的最后的工作的PID(进程ID). $? 上一条命令的返回值 原创文章，转载请注明： 转载自gjw_apparitor 博客 本文链接地址: SHELL 中几个特殊变量]]></description>
			<content:encoded><![CDATA[<p>$0 		shell脚本的名字<br />
$* 		shell的参数<br />
$@ 	shell的参数<br />
$# 		shell的参数个数<br />
$_ 		shell的最后一个参数<br />
$$ 		shell脚本自身进程的ID<br />
$! 		shell在后台运行的最后的工作的PID(进程ID).<br />
$? 		上一条命令的返回值
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2011/04/06/shell-%e4%b8%ad%e5%87%a0%e4%b8%aa%e7%89%b9%e6%ae%8a%e5%8f%98%e9%87%8f/">SHELL 中几个特殊变量</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2011/04/06/shell-%e4%b8%ad%e5%87%a0%e4%b8%aa%e7%89%b9%e6%ae%8a%e5%8f%98%e9%87%8f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSH的自动登录脚本</title>
		<link>http://www.apparitor.info/2011/04/06/ssh%e7%9a%84%e8%87%aa%e5%8a%a8%e7%99%bb%e5%bd%95%e8%84%9a%e6%9c%ac/</link>
		<comments>http://www.apparitor.info/2011/04/06/ssh%e7%9a%84%e8%87%aa%e5%8a%a8%e7%99%bb%e5%bd%95%e8%84%9a%e6%9c%ac/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 06:17:38 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[expect]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=209</guid>
		<description><![CDATA[#!/usr/bin/expect -f set ipaddress [lindex $argv 0] set passwd [lindex $argv 1] set passwd1 [lindex $argv 2] set timeout 30 spawn ssh -p 22 -c arcfour -o ConnectTimeout=30 root@$ipaddress "iptables-save &#38;&#38; service iptables restart" #spawn ssh -p 22 -c arcfour -o ConnectTimeout=30 root@$ipaddress "/etc/init.d/dnsmasq status" #spawn rsync -avpur --progress -e "ssh -p 22 -c arcfour"]]></description>
			<content:encoded><![CDATA[<pre>#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set passwd1 [lindex $argv 2]
set timeout 30
spawn ssh -p 22 -c arcfour -o ConnectTimeout=30 root@$ipaddress "iptables-save &amp;&amp; service iptables restart"
#spawn ssh -p 22 -c arcfour -o ConnectTimeout=30 root@$ipaddress "/etc/init.d/dnsmasq status"
#spawn rsync -avpur --progress -e "ssh -p 22 -c arcfour" /tmp/dnsmasq root@${ipaddress}:~/
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
#expect "]*"
expect "password:"
send "$passwd1\r"
expect "]*"</pre>
<p><span id="more-209"></span></p>
<p>##########循环######################</p>
<pre>#!/bin/bash

&gt;out.txt
for i in `cat ip.txt`
do
	echo ###########$i############ &gt;&gt;out.txt
	expect expect.sh $i 密码１　密码２ &gt;&gt;out.txt
done</pre>
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2011/04/06/ssh%e7%9a%84%e8%87%aa%e5%8a%a8%e7%99%bb%e5%bd%95%e8%84%9a%e6%9c%ac/">SSH的自动登录脚本</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2011/04/06/ssh%e7%9a%84%e8%87%aa%e5%8a%a8%e7%99%bb%e5%bd%95%e8%84%9a%e6%9c%ac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>dig 详解 (转载)</title>
		<link>http://www.apparitor.info/2011/04/06/dig-%e8%af%a6%e8%a7%a3-%e8%bd%ac%e8%bd%bd/</link>
		<comments>http://www.apparitor.info/2011/04/06/dig-%e8%af%a6%e8%a7%a3-%e8%bd%ac%e8%bd%bd/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 06:15:35 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[dig]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=205</guid>
		<description><![CDATA[转自：http://docutek.asia/faq/infoblox/296.htm dig 是 UNIX/BSD 系统都自带的 DNS 诊断工具，使用十分灵活，被很多 DNS 管理员用来做 DNS 诊断的工具。但在 Windows 系统中并没有提供 dig 程序，需要用户自行下载安装。 用法 dig [@server] [-b address] [-c class] [-f filename] [-k filename] [-m] [-p port#] [-q name] [-t type] [-x addr] [-y [hmac:]name:key] [-4] [-6] [name] [type] [class] [queryopt...] 符号说明：”[]” 表明该选项是可选项。 dig 是完全命令行的工具，使用者需要记得各个选项的用法。如果忘记可以使用 -h 选项查看所有的参数。以下就各个选项进行说明： server 指定 DNS Server 服务器。 name]]></description>
			<content:encoded><![CDATA[<p>转自：http://docutek.asia/faq/infoblox/296.htm</p>
<p>dig 是 UNIX/BSD 系统都自带的 DNS 诊断工具，使用十分灵活，被很多 DNS 管理员用来做 DNS 诊断的工具。但在 Windows 系统中并没有提供 dig 程序，需要用户自行下载安装。</p>
<p>用法<br />
dig [@server] [-b address] [-c class] [-f filename] [-k filename] [-m] [-p port#] [-q name] [-t type] [-x addr] [-y [hmac:]name:key] [-4] [-6] [name] [type] [class] [queryopt...]</p>
<p>符号说明：”[]” 表明该选项是可选项。</p>
<p>dig 是完全命令行的工具，使用者需要记得各个选项的用法。如果忘记可以使用 -h 选项查看所有的参数。以下就各个选项进行说明：<br />
server<br />
指定 DNS Server 服务器。<br />
<span id="more-205"></span>name<br />
输入我们所要查询的域名，其意思与 [-q name] 相同。<br />
type<br />
指定要查询的记录类型，其意思与 [-t type] 相同。type 类型有 a、any、mx、ns、soa、hinfo、axfr、txt 等，默认值为 a。<br />
address<br />
指定要透过那一张网卡(IP 地址)进行查询，适用于多网卡环境下指定网卡。<br />
-f filename<br />
指定 filename 文件做为 dig 批处理查询条件，该文件格式是一行一个查询，而在每行开头省略掉 “dig” 命令。<br />
-k filename<br />
指定 filename 文件为 TSIG KEY，也可以使用 -y 直接使用 TSIG 的 key。<br />
-p port<br />
指定 DNS Server 所使用的端口，可用於当服务器不是使用标准 DNS 端口的状况。<br />
-x addr<br />
表示要进行反向查询。<br />
-y [hmac:]name:key<br />
指定查询所用的 TSIG Key，基于安全考虑不建议在命令行使用 TSIG key。<br />
queryopt&#8230;：<br />
queryoptions，用以指定细步查询设置和显示项目，使用 “+” 来标识。<br />
+[no]tcp：是否使用 TCP 协议查询，一般情况下(不使用 AXFR 和 IXFR )都是用 UDP 协议。<br />
+[no]ignore ：UDP 协议若没有响应，是否改用 TCP 协议重新查询，默认为 “是”。<br />
+[no]search：是否使用 resolv.conf 中定义的列表进行查询，默认为 “否”。<br />
+[no]defname：等同于 +[no]search，不提倡使用。<br />
+[no]cl ：是否在查询结果中显示记录的类别。<br />
+[no]ttlid ：是否在查询结果中显示 TTL 号。<br />
+[no]recurse：是否使用递归查询查询，默认为 “是”。但是当加入 +nssearch 或 +trace 选项时自动取消递归查询。<br />
+[no]nssearch：是否在查询结果中显示 SOA 记录和名称服务器。<br />
+[no]trace：是否在查询结果中显示查询中的跳转(根据根服务器提示查到合适的名称服务器)。<br />
+[no]cmd：是否在查询结果中显示 dig 版本和输入的命令。<br />
+[no]short：是否在查询结果中提供精简显示，默认为 “否”。<br />
+[no]comments：是否在查询结果中显示注释。<br />
+[no]stats：是否在查询结果中显示统计信息，默认为 “是”。<br />
+[no]question：是否在查询结果中显示查询内容。<br />
+[no]answer：是否在查询结果中显示应答段。<br />
+[no]authority：是否在查询结果中显示权威服务器信息。<br />
+[no]additional：是否在查询结果中显示附加段的内容。<br />
+[no]all：设置/取消所有显示标记。<br />
+time=T：设置查询超时时间，默认为 5 秒。<br />
+tries=T：设置查询重试次数，默认是 3，如果小于 1 则设置为1。<br />
+retry=T：设置 UDP 查询重试次数，默认为 2，不包括初次查询。<br />
+bufsize=B ：设置查询是的高速缓冲区大小(0～65535)。<br />
+[no]multiline：是否在查询结果中显示一条记录为多行，默认为单行。<br />
+[no]fail：遇到服务器失败的时候是否查询下一个服务器。默认为 “是”。<br />
+[no]besteffort：是否显示不完整的应答信息。默认为 “是”。<br />
+trusted-key=####：指定包含有认证码的文件。如果没有指定，dig 会在当前目录中寻找 trusted-key.key，需要编译时使用 -DDIG_SIGCHASE 选项。
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2011/04/06/dig-%e8%af%a6%e8%a7%a3-%e8%bd%ac%e8%bd%bd/">dig 详解 (转载)</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2011/04/06/dig-%e8%af%a6%e8%a7%a3-%e8%bd%ac%e8%bd%bd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>shell 条件判断语句</title>
		<link>http://www.apparitor.info/2011/04/06/shell-%e6%9d%a1%e4%bb%b6%e5%88%a4%e6%96%ad%e8%af%ad%e5%8f%a5/</link>
		<comments>http://www.apparitor.info/2011/04/06/shell-%e6%9d%a1%e4%bb%b6%e5%88%a4%e6%96%ad%e8%af%ad%e5%8f%a5/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 06:12:50 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=201</guid>
		<description><![CDATA[1 字符串判断 str1 = str2　　　 当两个串有相同内容、长度时为真 str1 != str2　　 当串str1和str2不等时为真 -n str1　　　　 当串的长度大于0时为真(串非空) -z str1　　　　 当串的长度为0时为真(空串) str1　　　　　 当串str1为非空时为真 2 数字的判断 int1 -eq int2　　 两数相等为真 int1 -ne int2　　 两数不等为真 int1 -gt int2　　 int1大于int2为真 int1 -ge int2　　 int1大于等于int2为真 int1 -lt int2　　 int1小于int2为真 int1 -le int2　　 int1小于等于int2为真 3 文件的判断 -r file　　　　　 用户可读为真 -w file　　　　　 用户可写为真 -x file　　　　　 用户可执行为真]]></description>
			<content:encoded><![CDATA[<p>1 字符串判断<br />
str1 = str2　　　	当两个串有相同内容、长度时为真<br />
str1 != str2　　	当串str1和str2不等时为真<br />
-n str1　　　　	当串的长度大于0时为真(串非空)<br />
-z str1　　　　	当串的长度为0时为真(空串)<br />
str1　　　　　	当串str1为非空时为真</p>
<p>2 数字的判断<br />
int1 -eq int2　　	两数相等为真<br />
int1 -ne int2　　	两数不等为真<br />
int1 -gt int2　　	int1大于int2为真<br />
int1 -ge int2　　	int1大于等于int2为真<br />
int1 -lt int2　　	int1小于int2为真<br />
int1 -le int2　　	int1小于等于int2为真</p>
<p>3 文件的判断<br />
-r file　　　　　	用户可读为真<br />
-w file　　　　　	用户可写为真<br />
-x file　　　　　	用户可执行为真<br />
-f file　　　　　	文件为正规文件为真<br />
-d file　　　　　	文件为目录为真<br />
-c file　　　　　	文件为字符特殊文件为真<br />
-b file　　　　　	文件为块特殊文件为真<br />
-s file　　　　　	文件大小非0时为真<br />
-t file　　　　　	当文件描述符(默认为1)指定的设备为终端时为真</p>
<p>3 复杂逻辑判断<br />
-a 　 　　　　　 	与<br />
-o　　　　　　	或<br />
!　　　　　　　	非
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2011/04/06/shell-%e6%9d%a1%e4%bb%b6%e5%88%a4%e6%96%ad%e8%af%ad%e5%8f%a5/">shell 条件判断语句</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2011/04/06/shell-%e6%9d%a1%e4%bb%b6%e5%88%a4%e6%96%ad%e8%af%ad%e5%8f%a5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IP段掩码换算脚本</title>
		<link>http://www.apparitor.info/2011/04/06/ip%e6%ae%b5%e6%8e%a9%e7%a0%81%e6%8d%a2%e7%ae%97%e8%84%9a%e6%9c%ac/</link>
		<comments>http://www.apparitor.info/2011/04/06/ip%e6%ae%b5%e6%8e%a9%e7%a0%81%e6%8d%a2%e7%ae%97%e8%84%9a%e6%9c%ac/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 06:00:01 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=193</guid>
		<description><![CDATA[由于公司的DNS IP库比较陈旧，打算升级一下。跟朋友要了个最新的全国的IP库（他们是做CDN的） 打开一看都是32掩码的。貌似这个bind的不支持呀 221.14.122.0 221.14.127.255 河南省网通 221.13.128.0 221.15.255.255 河南省网通 221.192.168.0 221.192.171.255 河北省网通 61.237.195.0 61.237.195.255 黑龙江省铁通 219.147.36.0 219.147.36.255 山西省电信 202.98.247.0 202.98.248.255 西藏电信 218.21.128.0 218.21.255.255 内蒙古联通 61.171.112.0 61.171.127.255 上海市电信 61.171.160.0 61.171.179.255 上海市电信 218.21.0.0 218.21.47.255 宁夏电信 就写了个掩码转换的脚本。用法 ./Mask.sh 起始IP 终止IP #!/bin/bash Bin_Begin() { IPa=`echo "obase=2;ibase=10;$1" &#124;bc` IPaa=`expr 100000000 + $IPa` Begin_IP1=${IPaa#1} IPb=`echo "obase=2;ibase=10;$2" &#124;bc` IPbb=`expr 100000000 + $IPb`]]></description>
			<content:encoded><![CDATA[<p>由于公司的DNS IP库比较陈旧，打算升级一下。跟朋友要了个最新的全国的IP库（他们是做CDN的）<br />
打开一看都是32掩码的。貌似这个bind的不支持呀</p>
<pre>221.14.122.0		221.14.127.255		河南省网通
221.13.128.0		221.15.255.255		河南省网通
221.192.168.0		221.192.171.255		河北省网通
61.237.195.0		61.237.195.255		黑龙江省铁通
219.147.36.0		219.147.36.255		山西省电信
202.98.247.0		202.98.248.255		西藏电信
218.21.128.0		218.21.255.255		内蒙古联通
61.171.112.0		61.171.127.255		上海市电信
61.171.160.0		61.171.179.255		上海市电信
218.21.0.0		218.21.47.255    	宁夏电信</pre>
<p><span id="more-193"></span>就写了个掩码转换的脚本。用法 ./Mask.sh 起始IP 终止IP</p>
<pre>#!/bin/bash 

Bin_Begin() {
	IPa=`echo "obase=2;ibase=10;$1" |bc`
	IPaa=`expr 100000000 + $IPa`
	Begin_IP1=${IPaa#1}
	IPb=`echo "obase=2;ibase=10;$2" |bc`
	IPbb=`expr 100000000 + $IPb`
	Begin_IP2=${IPbb#1}
	IPc=`echo "obase=2;ibase=10;$3" |bc`
	IPcc=`expr 100000000 + $IPc`
	Begin_IP3=${IPcc#1}
	IPd=`echo "obase=2;ibase=10;$4" |bc`
	IPdd=`expr 100000000 + $IPd`
	Begin_IP4=${IPdd#1}
}

Bin_End() {
	IPe=`echo "obase=2;ibase=10;$1" |bc`
	IPee=`expr 100000000 + $IPe`
	End_IP1=${IPee#1}
	IPf=`echo "obase=2;ibase=10;$2" |bc`
	IPff=`expr 100000000 + $IPf`
	End_IP2=${IPff#1}
	IPg=`echo "obase=2;ibase=10;$3" |bc`
	IPgg=`expr 100000000 + $IPg`
	End_IP3=${IPgg#1}
	IPh=`echo "obase=2;ibase=10;$4" |bc`
	IPhh=`expr 100000000 + $IPh`
	End_IP4=${IPhh#1}
}

Bin_Begin `echo $1 | awk -F '.' '{print $1" "$2" "$3" "$4}'`
Bin_End `echo $2 | awk -F '.' '{print $1" "$2" "$3" "$4}'`

Bin_Begin_ip=$Begin_IP1$Begin_IP2$Begin_IP3$Begin_IP4
Bin_End_ip=$End_IP1$End_IP2$End_IP3$End_IP4

Mask=0
for ((x=0;x&lt;=31;x++))
do
	if [ ${Bin_Begin_ip:x:1} -eq ${Bin_End_ip:x:1} ];
	then
		((Mask++))
	else
		break;
	fi
done

echo $Mask</pre>
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2011/04/06/ip%e6%ae%b5%e6%8e%a9%e7%a0%81%e6%8d%a2%e7%ae%97%e8%84%9a%e6%9c%ac/">IP段掩码换算脚本</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2011/04/06/ip%e6%ae%b5%e6%8e%a9%e7%a0%81%e6%8d%a2%e7%ae%97%e8%84%9a%e6%9c%ac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SED 教程</title>
		<link>http://www.apparitor.info/2011/04/06/sed-%e6%95%99%e7%a8%8b/</link>
		<comments>http://www.apparitor.info/2011/04/06/sed-%e6%95%99%e7%a8%8b/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 05:53:55 +0000</pubDate>
		<dc:creator>gjw_apparitor</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.apparitor.info/?p=191</guid>
		<description><![CDATA[1、基本命令 sed是一个流文本编辑器。 保持空间（hold space ）：又叫保留空间或叫预留缓冲区。模式空间可以复制到保持空间，保持空间 的内容也可以复制到模式空间。 模式空间（pattern space ）：是容纳当前输入行的缓冲区。sed命令将当前处理的行读入模式空间进行处理，在该行上执行完所有命令后就将处理好的行打印到屏幕上（除非之前的命令删除了该行），sed处理完一行就将其从模式空间中删除，然后将下一行读入模式空间，进行处理、显示。处理完文件的最后一行，sed便结束运行。sed在模式空间对文件进行处理，所以不会修改原文件，除非显示指明-i选项。 s 替换 [address]s/pattern/replacement/[n&#124;g&#124;p&#124;w] 用replacement替换pattern,如果replacement是&#038;符号，它代表pattern的内容。 n:表示第n个pattern。n是1-512之间的任意一个数字，默认为1. g:表示全局的，指所有的pattern。 p:表示打印成功替换的那行。 w file:将成功替换的行写到file里。sed &#8216;s/pattern/replacement/w file1&#8242; file s/\( \) \(\)\(\)/\n\n\n #可指定位置。 例： cat text 1*2*3*4 如果想让3*4在令一行。 sed &#8216;s/*/\ /2&#8242;text -n 打印。把模式空间当前行送到标准输出。 Sed &#8216;/address/{ = }&#8217; -n file 显示匹配行的行号。 d 删除。删除一行的内容。 Sed /address/d a 追加。放在在匹配的下一行。 [address]a\superaddition sed &#8216;/address/a\superaddition&#8217; file i插入。在配匹配的上一行。 sed &#8216;/address/i\superaddition&#8217;]]></description>
			<content:encoded><![CDATA[<p>1、基本命令<br />
sed是一个流文本编辑器。<br />
保持空间（hold space ）：又叫保留空间或叫预留缓冲区。模式空间可以复制到保持空间，保持空间<br />
的内容也可以复制到模式空间。<br />
模式空间（pattern space ）：是容纳当前输入行的缓冲区。sed命令将当前处理的行读入模式空间进行处理，在该行上执行完所有命令后就将处理好的行打印到屏幕上（除非之前的命令删除了该行），sed处理完一行就将其从模式空间中删除，然后将下一行读入模式空间，进行处理、显示。处理完文件的最后一行，sed便结束运行。sed在模式空间对文件进行处理，所以不会修改原文件，除非显示指明-i选项。</p>
<p>s 替换<br />
[address]s/pattern/replacement/[n|g|p|w]<br />
用replacement替换pattern,如果replacement是&#038;符号，它代表pattern的内容。<br />
n:表示第n个pattern。n是1-512之间的任意一个数字，默认为1.<br />
g:表示全局的，指所有的pattern。<br />
p:表示打印成功替换的那行。<br />
w file:将成功替换的行写到file里。sed &#8216;s/pattern/replacement/w file1&#8242; file<br />
<span id="more-191"></span><br />
s/\( \) \(\)\(\)/\n\n\n #可指定位置。</p>
<p>例：<br />
cat text<br />
1*2*3*4<br />
如果想让3*4在令一行。<br />
sed &#8216;s/*/\<br />
/2&#8242;text</p>
<p>-n 打印。把模式空间当前行送到标准输出。<br />
Sed &#8216;/address/{<br />
=<br />
}&#8217; -n file 显示匹配行的行号。</p>
<p>d 删除。删除一行的内容。<br />
Sed /address/d</p>
<p>a 追加。放在在匹配的下一行。<br />
[address]a\superaddition<br />
sed &#8216;/address/a\superaddition&#8217; file</p>
<p>i插入。在配匹配的上一行。<br />
sed &#8216;/address/i\superaddition&#8217; file</p>
<p>c 替换。替换匹配的那一行。<br />
sed &#8216;/address/c\superaddition&#8217; file</p>
<p>y 转换。<br />
Sed &#8216;y/abc/ABC&#8217; file 将小写字母改成大写字母。<br />
＝ 打印行号。打印匹配行的行号。<br />
Sed &#8216;/address/=&#8217; file</p>
<p>p 打印。打印寻址的行。<br />
Sed &#8216;/address/p&#8217; file</p>
<p>-n 读取下一行到模式空间。<br />
Sed &#8217;1,/address/{ #从第一行开始匹配第一个address<br />
/address/i\ok<br />
n<br />
}&#8217; -i 1.txt</p>
<p>r 读文件。<br />
Sed &#8216;/address/r file1/&#8217; file ＃读取file1文件把内容追加到address行后边。</p>
<p>w 写文件。<br />
Sed &#8216;/address/w file1/&#8217; file ＃把file文件的address里的内容写到file1里去。</p>
<p>q 退出。<br />
Sed &#8216;/address/q&#8217; file ＃到比配的行，运行命令让好退出。</p>
<p>2、高级命令－多模式空间。<br />
N 将多行读入到模式空间。（$!N）这个是防止在读到最后一行时没有下一行时出现的错误。<br />
Sed &#8216;/address/{ ＃将address和他的下一行都读到模式空间。<br />
N ＃用s///把他的换行符\n换成＋号。使两行<br />
s/\n/＋/g ＃合成一行。<br />
｝‘ test.txt</p>
<p>D 多模式删除。它只删除模式空间从开始到第一个换行符的内容。多用于删除多余的空行。<br />
Sed &#8216;/address/{<br />
N<br />
D ＃他只删除从开始到第一个换行符的内容<br />
}&#8217; test.txt ＃如果是d会把模式空间里的内容全部清掉。<br />
P 多行打印。它只打印模式空间从开始到第一个换行符的内容。<br />
Sed &#8216;/address/{<br />
N<br />
P ＃他只打印从开始到第一个换行符的内容<br />
}&#8217; test.txt ＃如果是p会打印模式空间的全部内容。</p>
<p>3、模式空间和保持空间之间的转换。<br />
h 将模式空间的内容复制到保持空间。<br />
H 将模式空间的内容追加到保持空间。默认保持空间是空，追加是在空行后再加一行。<br />
例：\n追加的内容<br />
g 将保持空间的内容复制到模式空间。<br />
G 将保持空间的内容追加到模式空间。默认保持空间是空，追加是在空行后再加一行。<br />
例：\n追加的内容<br />
x 交换保持空间和模式空间的内容。<br />
Sed &#8216;/2/{         ＃找到匹配2的那行<br />
H             ＃模式空间为2追加到保持空间，现在模式空间为2，保持空间为空2<br />
x             ＃交换模式空间和保持空间。现在模式空间为“空2”保持空间为2。<br />
}&#8217; test.txt</p>
<p>将语句的某个词改变大小写。<br />
例：<br />
/the .*statement/ ‘{<br />
h<br />
s/.*the \(.*\)statement.*/\1/<br />
y/abcdefghigklmnopqrstuvwsyz/ABCEDFGHIJKLMNOPQRSTUVWXYZ/<br />
G<br />
/s\(.*\)\n\(.*the \).* \(statement.*\)/\2\1\3/<br />
}’ file</p>
<p>解释：<br />
h 将配陪行从保持空间复制到模式空间.<br />
s/.*the \(.*\)statement.*/\1/ 从保持空间提取要改变词的内容取代模式空间。<br />
y 大小写替换。<br />
G 将模式空间的内容追加到保持空间。（模式空间内容\n保持空间内容）<br />
/s 从新排列组合。</p>
<p>4、高级的流控制命令<br />
b 分支。脚本无条件的转向下一个分支指向处。<br />
[address]b[label] #label 是可选的，如果没有给出label。则跳到脚本的结尾处。<br />
例：<br />
:label<br />
Command1<br />
Command2<br />
/pattern/b label<br />
Command3</p>
<p>t 测试。测试在寻址范围内是否成功执行替换。如果是则转移到有label标志的行,没有制定label则跳转到脚本结尾处。如果替换不成功则执行下一条命令<br />
[address]t[label]<br />
例：<br />
/address/ ‘{<br />
s/abc/ABC #如果成功替换就打印，没有替换就跳到结尾处。<br />
t<br />
p<br />
}’ test</p>
<p>其他<br />
l 列出模式空间的内容，将不可打印的字符以ASCLL码表示。</p>
<p>Sed 语法<br />
Sed [-n][-e] ’command’ file<br />
Sed [-f] scriptfile file<br />
-n 仅打印p命令s命令标记的行。<br />
-e 接着下一个命令。用于多脚本使用。<br />
-f 一个包含sed命令的文件。</p>
<p>[address]! Command #不匹配
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://www.apparitor.info/">gjw_apparitor 博客</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.apparitor.info/2011/04/06/sed-%e6%95%99%e7%a8%8b/">SED 教程</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.apparitor.info/2011/04/06/sed-%e6%95%99%e7%a8%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

