策略路由的配置

最近测试DNS服务器直接和交换机跑OSPF。2上联网卡分别接入2交换机,形成邻居。服务器不设置静态的默认路由,通过和上层路由器交换路由信息的时候学习默认路由。另外的办公网接入的网卡只是绑定了IP。因为是在测试环境所以有个问题是上联的链路其实是不能访问外网的。我就单独设置了一下策略路由解决。需要达到的目的其实只是能从办公网络ssh登陆服务器,服务器上能访问部分外网(比如8.8.8.8进行DNS解析)。配置其实比较简单:1. 先新增策略路由#cat /etc/iproute2/rt_tables## reserved values#255 local254 main253 default0 unspec## local200 dns 2.给table 200增加默认的路由#cat route-eth0table dns 192.1.159.0/24 via 192.1.159.254 dev eth0table dns default via 192.1.159.254 dev eth0 ip route add 192.1.159.0/24 via 192.1.159.254 dev eth0 table dnsip route add default via 192.1.159.254 dev eth0 table dns ip rule add to 8.8.8.8 table dnsip rule add from 192.1.159.210 table dnsip rule add to 192.242.252.0/24 table dns

July 6, 2013 · 1 min · pm

gnome-shell下开机亮度的调节

gnome3下现在屏幕的默认亮度不能保存,网上搜了一圈简单的方式是在rc.local里加 echo 70 > /sys/class/backlight/acpi_video0/brightness

July 2, 2013 · 1 min · pm

服务影响时间测量

这几天配置dns集群,为了测试各种场景下的服务影响时间,需要自己写个脚本进行统计。没有什么现成的好的工具,就用python里面的dnspython模块写了个小的脚本[python]#!/usr/bin/python#****************************************************************## ScriptName: dnsquery.py# Author: GNUer# Create Date: 2013-06-28 12:52# Modify Author: GNUer# Modify Date: 2013-06-28 12:52# Function:#***************************************************************#import dns.resolverimport dns.exceptionimport timeimport datetimeimport signalimport sysdef get_time():t=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")return tdef get_mtime():t=datetime.datetime.now().strftime("%H:%M:%S.%f")return tdef get_second():return time.time()def do_exit(sig,arg):print "exit dns test"sys.exit(1)def dns_test(num,sleep_time):resolver = dns.resolver.Resolver()#resolver.timeout = 0.01resolver.lifetime =0.0013#change this time accord to reponse timeresolver.nameservers=[‘7.7.7.7′,’6.6.6.6’]testlist=("search.xxx.com","obs.xxx.com","if.xxx.com")last_flag=Truefail_start=0fail_finish=0for i in range(num):for test in testlist:try:for target in resolver.query(qname=test):print "\x1b[32m",get_time(),test,target,"\x1b[m"if last_flag == False:last_flag= Truefail_finish=get_second()errortime=fail_finish-fail_startprint "\x1b[1; 31m","error time is:",errortime,"\x1b[m"except dns.exception.Timeout:print "\x1b[31m",get_time(),test,"failed","\x1b[m"if last_flag == True:fail_start=get_second()last_flag = Falsetime.sleep(sleep_time)signal.signal(signal.SIGINT,do_exit)dns_test(10000000,0.33)[/python]需要比较注意的是需要根据自己的实际情况把resolver.lifetime设置为比dns服务器的响应时间稍微大一点的值,但是需要小于平均响应时间的2倍。不然测试的时间不是太准确。因为总共测试的域名是3个,所以我把每次sleep的时间设置为0.32左右,使得1s总共能发3个请求左右。测试的时候可以先把这个脚本一直跑着,然后去做各种操作,看中间的影响时间是多少。虽然这个脚本是测试dns的,但是也可以修改一下做HTTP请求等等,方便在做各种HA切换的时候测试影响的时间。

June 28, 2013 · 1 min · pm

一次ssh信任登录失败的排查

昨天遇到有同事帮忙看一下信任登录打通的问题,已经把跳板机的公钥加到服务器上了,但是每次登录都要输入密码。ssh -v看了一下能成功登录的机器是debug1: Next authentication method: publickeydebug1: Trying private key: /home/admin/.ssh/identitydebug1: Offering public key: /home/admin/.ssh/id_rsadebug1: Server accepts key: pkalg ssh-rsa blen 277debug1: read PEM private key done: type RSAdebug1: Authentication succeeded (publickey).debug1: channel 0: new [client-session]debug1: Entering interactive session.不能成功登录的机器是debug1: Next authentication method: publickeydebug1: Trying private key: /home/admin/.ssh/identitydebug1: Offering public key: /home/admin/.ssh/id_rsadebug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,passworddebug1: Trying private key: /home/admin/.ssh/id_dsadebug1: Next authentication method: password区别是不能成功登录的服务器没有接受到私钥。查看一下是.ssh整个目录的宿主uid是有问题:# stat .sshFile: “.ssh”Size: 4096 Blocks: 8 IO Block: 4096 目录Device: ca20h/51744d Inode: 311305 Links: 2Access: (0755/drwxr-xr-x) Uid: ( 500/ UNKNOWN) Gid: ( 500/ XXX)Access: 2013-06-15 23:04:15.000000000 +0800Modify: 2013-04-03 11:24:29.000000000 +0800Change: 2013-06-15 23:04:15.000000000 +0800 用户的UID其实不是500,但是.ssh的UID却被设置为了500,chown xxx:xxx .ssh -R后解决。 ...

June 27, 2013 · 1 min · pm

指定dns查询的超时时间

最近在做些打杂的事情,需要对dns做一下健康检查。脚本直接shell写的,以前的同事的方案只是直接检查一下named进程是否存在,存在就是表示OK的,这个策略显然是有问题的,比如进程假死之类的其实是没有办法检查的。然后我就想着改成nslookup或者host命令进行一个查询,检测返回值,由于nslookup的返回值不能体现出查询是否成功,就只有host可以用了。```bash check_dns(){local domainlocal serverdomain=“www.xxx.com"server=’127.0.0.1′ host -W1 $domain $server &>/dev/nullerror=$?if [ $error -ne $STATUS_OK ];thenwarn "can’t resolve $domain,error."fireturn $error} check_dns(){local domainlocal serverdomain="www.xxx.com"server=’127.0.0.1′dig @$server $domain +time=1 +retry=1 +tries=1 &>/dev/nullerror=$?if [ $error -ne $STATUS_OK ];thenwarn "can’t resolve $domain,error."fireturn $error

June 27, 2013 · 1 min · pm

配置nginx不暂存服务端的响应到缓冲区

nginx做代理的服务器的时候是默认等后端服务器完成响应后再一次把内容返回给用户。这个关键的配置项目是proxy_buffering.proxy_buffering 默认是on [proxy_buffering](http://wiki.nginx.org/HttpProxyModule#proxy_buffers) This directive activate response buffering of the proxied server.If buffering is activated, then nginx reads the answer from the proxied server as fast as possible, saving it in the buffer as configured by directives proxy_buffer_size andproxy_buffers. If the response does not fit into memory, then parts of it will be written to disk.

June 27, 2013 · 1 min · pm

gnome-terminal的常用快捷键

平时很山使用快捷键什么的,主要是经常记不住。不过现在买了个机械键盘就是为了多试试用快捷键提高自己的日常的操作效率(其实主要是不能用小红点了。。。)。刚看下了,其实gnome-terminal自己也带了很多常用的快捷键。我自己单独定义了一个期待term的快捷键是ctrl+alt+t.gnome-terminal的快捷键主要有:新建一个标签页:ctrl+shift+t新建一个窗口:ctrl+shift+n多个标签页的切换可以有2种方式:1.使用alt+数字N 直接切换到第N个标签页。2.使用ctrl+pageup/pagedown做前后的切换关闭一个标签页:ctrl+shift+w关闭整个窗口:ctrl+shift+q

June 21, 2013 · 1 min · pm

vichrome插件使用

今天买了个机械键盘,不过买了机械键盘后第一个感觉是之前用习惯了小红点现在非常不习惯。之前小红点完全可以代替鼠标的很多操作,现在却是很多操作需要另外去用鼠标才能搞定。想起了之前有个浏览器的vi插件就装了下。不过很久没有用这个玩意了早就忘记了。主要需要记住的操作是:t 打开新的标签页o 在当前的表情页打开新的urlctrl+u 向上一页ctrl+d 向下一页上下左右和vim里面一样的k/j h/lctrl+h转向左边的标签页ctrl+l转向右边的标签页/ 搜索``` Vichrome’s default key bindings are: j : scroll downk : scroll uph : scroll leftl : scroll right: scroll down: scroll upt : open new tabx : close tabX : Close the current tab and focus the previous tabr : reload tab/ : enter search mode? : enter backward search modea : enter link text search mode(search only text that have a link and focus it)n : next search resultN : previous search resultgg : go to top of the pageG : go to bottom of the pagegt,: move to left tabgT,: move to right tabH : back historyL : forward historyu : restore last closed tabgp : pop out current tab to new windowgs : open source of the current page in a new tabf : enter f Mode (like HIT-A-HINT)F : enter f Mode (open links with new tab)i : focus on first input areao : Open new page with awesome completionO : Open new page in new tab with awesome completions : Google SearchS : Google Search in new tabb : Open bookmarkB : Open bookmark in new tab: page down: page up: page half down: page half up: : enter Command Mode” : back to last auto page markyy : copy URL to clipboardp : Open a URL based on the current clipboard contents in the current tabP : Open a URL based on the current clipboard contents in a new tab: switch tab focus to the previously focused tab,z : toggle image size: cancel all input/command and blur focused element

June 21, 2013 · 2 min · pm

页面元素检测脚本

我们经常遇到某个页面缺少一个js之类的情况,一般使用firefox的httpfox或者chrome自带的工具都可以查看那些元素有问题.用python写了一个脚本,分析页面里面引用的元素,然后逐个进行请求,查看是否有的元素不能成功获取.不过使用urllib2有的地方异常处理也做的不完善,目前只是自己简单测试了一些,当熟练一下Python的用法了.目前从perl切换到python还是有很多的地方不是太习惯.尤其是很多列表和字符串的操作有点不一样.[python]#!/usr/bin/env pythonimport urllib2import gzipimport binasciiimport re,sysimport stringfrom StringIO import StringIOdef gunziptxt(data):buf = StringIO(data)of =gzip.GzipFile(fileobj=buf,mode="rb")outdata=of.read()return outdatadef http_code(url):request=urllib2.Request(url,headers={‘User-agent’:"python urllib browser","Accept-Encoding":’gzip’})try:response=urllib2.urlopen(request,timeout=5)return response.getcode()except urllib2.HTTPError,error:print "url:",error.reasonreturn error.codeexcept urllib2.URLError,error:print url,error.reasonreturn 1000def http_client(url):request=urllib2.Request(url,headers={‘User-agent’:"python urllib browser","Accept-Encoding":’gzip’})try:response=urllib2.urlopen(request,timeout=5)info=response.info()data=response.read()except urllib2.HTTPError,error:print "%s error:%s" %(url,error.reason)return Noneexcept urllib2.URLError,error:print error.reasonreturn None ```bash if info.get("content-encoding",None) == ‘gzip’:outdata=gunziptxt(data)else:outdata=datareturn outdatadef get_src(page):src_re=re.compile(r’src\s*=\s*["|\’]\s*(https?://[^\"\’]+?)["|\’]’)if page:link_urls=src_re.findall(page)return set(link_urls)else:return set()if len(sys.argv)<2:print "usage:\n\t",sys.argv[0],"url"exit(1)if __name__ == "__main__":urls=sys.argv[1]pages=http_client(urls)if pages:links=get_src(pages)else:exit(1)for link in links:code=http_code(link)if code >399:print "%s \x1B[1;31m%d\x1B[m"%(link,code)else:print "%s \x1B[1;32m%d\x1B[m"%(link,code)else:print "pagecheck test"

June 12, 2013 · 1 min · pm

python多线程测试

仿照之前自己写的一个perl的多线程的脚本,简单试了下python的多线程模块threading.只能说因为之前习惯了perl的很多用法,刚切换到python非常不习惯,一些小的操作上不熟练. #!/usr/bin/env python import threading import time import random import sys import string def fun_test(x): sleep_time=random.random()*1 print "args is %s sleep time is %.4f s" % (x,sleep_time) time.sleep(sleep_time) def multi_do(lists,concurrent_max,func): concurrent=0 thread_list=set() while( len( lists ) > 0 ): if len(thread_list) <= concurrent_max: ser=lists.pop() pid=threading.Thread(target=func,name=ser,args=(ser,)) thread_list.add(pid) pid.start() else: alive_threads=set(threading.enumerate()) join_threads=thread_list-alive_threads for job in join_threads: print “%s is done”% job.getName() job.join() thread_list.remove(job) while(len(thread_list)>0): alive_threads=set(threading.enumerate()) join_threads=thread_list-alive_threads for job in join_threads: print “%s is done”% job.getName() job.join() thread_list.remove(job) print “all job have been done” if name == “main”: try: fd=open(sys.argv[1],“r”) print “open %s ok” % sys.argv[1] todo_list=fd.readlines() ...

June 11, 2013 · 1 min · pm