寻觅生命中的那一片浅草......

文章属于类别 Python

SecureCRT批量配置使用会话key

部分服务器使用单独的key,为了方便,写了这2个脚本

2个版本,都需要Python
Windows版
win_crt_conf.py

cygwin版
cygwin_crt_conf.py

cygwin下可以用find+sed+unix2dos实现,但真的太慢了,Python快啊

2个文件要修改的地方是一样的

需要使用单独key的SecureCRT配置文件目录
crt_conf_dir = ‘E:\\CRT_CONF’

定义key文件的位置,identify为private key的名字
key_path_new = ‘S:”Identity Filename”=E:\CRT_KEY\identify’

原理,每个服务器配置都定义了使用全局key还是会话key
这个是使用全局key
D:”Use Global Public Key”=00000001
定义使用的key文件路径
S:”Identity Filename”=

定义使用会话key
D:”Use Global Public Key”=00000000

E:\CRT_KEY\identify为要使用的会话key
S:”Identity Filename”=E:\CRT_KEY\identify
win_crt_conf.py
[python]
#!D:\\Python27\\python
#-*-coding=utf-8-*-

import os,sys,re

crt_conf_dir = ‘E:\\CRT_CONF’
global_public_key_true = ‘D:”Use Global Public Key”=00000001’
global_public_key_false = ‘D:”Use Global Public Key”=00000000’
key_path_old = ‘S:”Identity Filename”=’
key_path_new = ‘S:”Identity Filename”=E:\CRT_KEY\identify’

re_global_public_key = re.compile(global_public_key_true,re.DOTALL)
re_key_path = re.compile(‘S:”Identity Filename”=(.*)’)

os.chdir(crt_conf_dir)

c1 = os.walk(os.getcwd())

filelist = []

for c2 in c1:
for c3 in c2[2]:
filelist.append(os.path.join(c2[0],c3))

for filename in filelist:
fileread = open(filename,’r’)
filer = fileread.read()
pub_key = re.sub(re_global_public_key,global_public_key_false,filer,0)
key_path = re.sub(re_key_path,key_path_new,pub_key,0)
fileread.close()
fileok = open(filename,’w’)
fileok.write(key_path)
fileok.close()
print filename,’替换成功!’
[/python]

cygwin_crt_conf.py

[python]
#!/usr/bin/python
#-*-coding=utf-8-*-

import os,sys,re

crt_conf_dir = ‘/cygdrive/e/CRT_CONF’
global_public_key_true = ‘D:”Use Global Public Key”=00000001’
global_public_key_false = ‘D:”Use Global Public Key”=00000000’
key_path_old = ‘S:”Identity Filename”=’
key_path_new = ‘S:”Identity Filename”=E:\CRT_KEY\identify’

re_global_public_key = re.compile(global_public_key_true,re.DOTALL)
re_key_path = re.compile(‘S:”Identity Filename”=(.*)’)

os.chdir(crt_conf_dir)

c1 = os.walk(os.getcwd())

filelist = []

for c2 in c1:
for c3 in c2[2]:
filelist.append(os.path.join(c2[0],c3))

for filename in filelist:
fileread = open(filename,’r’)
filer = fileread.read()
pub_key = re.sub(re_global_public_key,global_public_key_false,filer,0)
key_path = re.sub(re_key_path,key_path_new,pub_key,0)
fileread.close()
fileok = open(filename,’w’)
fileok.write(key_path)
fileok.close()
print filename,’Replace successful!’
[/python]

主要参考了http://blog.591by.com/show-214-1

python_ping.py

1、学习Python处理中文的方法
2、学习正则表达式

[python]
#!D:\\Python27\\python
#coding=gbk

“””System : Windows 7″””

import subprocess
import re
import sys
print “ping “+sys.argv[2]+”次 “+sys.argv[1]

##设置为unicode编码
p = re.compile(u'(\W\W = \d+ms)’)
out = subprocess.Popen(“ping -n %d %s” % (int(sys.argv[2]),sys.argv[1]),stdout=subprocess.PIPE,shell=True)

#将gbk编码的输出解码为unicode,并查找关键字
match = p.findall(out.stdout.read().decode(“gbk”))

for result in match:
print result

[/python]

Python处理keepass导出的xml文件

用keepass管理密码
现在有员工需要将用户名和密码导出来
格式如下
username password

我觉得最难的地方是根据Value取出来的list中,有空值,如果是空值,取其值或者类型时,都会提示
“AttributeError: ‘NoneType’ object has no attribute ‘nodeType’”
可以加if i.firstChild is None:continue进行判断,如果是空值则跳出本次循环
if i.firstChild is None:continue这个是非常pythonic的写法哦

程序

[python]
#!/bin/env python
import sys
from xml.dom import minidom
if len(sys.argv[0:]) != 2:
print “./keepass.py your xml file’s path”
sys.exit()

list=[]
xmldoc = minidom.parse(sys.argv[1])
reflist = xmldoc.getElementsByTagName(‘Value’)
for i in reflist[::-1]:
if i.firstChild is None:continue
list.append(i.firstChild.nodeValue)

for j in range(0,len(list),3):
print list[j]+” “+list[j+2]

[/python]

输出结果
lisi BiPqIcbHVfUDxaONKjNV
zhangsan h5LGhEYc3OASZf1VcGbD

keepass.xml

[xml]



KeePass

2010-10-18T06:08:30Z

2010-10-18T06:08:30Z

2010-10-18T06:08:30Z
365
2010-11-12T05:41:29Z
-1
-1

False
False
True
False
False

True
c8kwCXpGOE+MBy1jzCOT/g==
2010-10-18T06:08:30Z
AAAAAAAAAAAAAAAAAAAAAA==
2010-10-18T06:08:30Z
NHp/A6+c1kqAdPJrncQ5mQ==
Vgbin7aoeEuGRJXKiJk47g==




NHp/A6+c1kqAdPJrncQ5mQ==
keepass

48

2010-12-20T13:00:58Z
2010-12-20T13:00:52Z
2010-12-20T13:01:06Z
2010-12-18T10:04:30Z
False
3
2010-12-20T13:00:52Z

True

null
null
NMjEkhZudUiMAnPQOYQa+Q==

NMjEkhZudUiMAnPQOYQa+Q==
0





2010-12-20T13:01:18Z
2010-12-20T13:01:07Z
2010-12-20T13:01:18Z
2010-12-18T10:04:30Z
False
1
2010-12-20T13:01:07Z


Notes



Password
h5LGhEYc3OASZf1VcGbD


Title
张三


URL



UserName
zhangsan


True
0




b3KWP7kC+UmZnnSA4d0byg==
0





2010-12-20T13:01:31Z
2010-12-20T13:01:22Z
2010-12-20T13:01:31Z
2010-12-18T10:04:30Z
False
1
2010-12-20T13:01:22Z


Notes



Password
BiPqIcbHVfUDxaONKjNV


Title
李四


URL



UserName
lisi


True
0






9LVoDcHI9UquYgzy1tRXzg==
2010-11-12T05:49:57Z


gimxTDSBBkWdSIFORZUx0w==
2010-11-12T05:50:02Z


7/7Rd5KBT0CyY9O9A2RDpA==
2010-11-12T05:50:05Z


tPT9aQPrNkOM2mmj6kP3CA==
2010-11-16T02:58:07Z


gN/uXsDF206oc1g783tsLQ==
2010-11-16T02:58:11Z


jMYxMK3iakOWgPJGVz9lSg==
2010-11-22T08:55:46Z


j8UaFYCZbE2drG+gNysIwg==
2010-11-26T08:30:23Z


13QkVRyovk+9TNqxneYO5w==
2010-11-26T08:33:04Z


1+DFhfzAYUmL74gsRFzZNw==
2010-12-20T12:41:13Z




[/xml]

Parallel Install Python26 on CentOS

[bash]
cd /etc/yum.repos.d/
wget http://mirrors.geekymedia.com/centos/geekymedia.repo
yum install python26.i386

python26 -V
Python 2.6
[/bash]
References:
http://www.geekymedia.com/tech-articles/rhel5-centos5-rpms-for-python-2-5-and-2-6/
http://mirrors.geekymedia.com/centos/

Python+bash下载“不许联想”的mp3

主要是学习Python处理xml的方法
先上一个纯Python的,有问题,部分歌曲下载不到,返回404错误,待解决

2010-12-15
终于找到404的原因了,今天早上回来,又执行了一下脚本,发现能下载的文件名都是没有空格的,而404的则都有空格,对文件名进行了编码,解决了,另外,由于有首歌的歌名有个中文的单引号,故需要将它编码为gbk,才发现Python2.4.3不支持gbk,所以,最后的程序,开头用的是Python2.7.1 ,修改的程序见页面底部

[python]
#!/bin/env python
import os
import urllib
from xml.dom import minidom
import time
download_time=time.strftime(‘%Y%m%d’,time.localtime())
download_path=os.getcwd()+”/”+”wxf”+download_time

if not os.path.isdir(download_path):
os.mkdir(download_path)

usock = urllib.urlopen(‘http://www.wangxiaofeng.net/mp3player.xml’)
xmldoc = minidom.parse(usock)
usock.close()
songlist = xmldoc.getElementsByTagName(‘song’)
for i in range(0,len(songlist),1):
url=songlist[i].attributes[“path”].value.encode(‘utf-8’)
name=songlist[i].attributes[“title”].value
urllib.urlretrieve(url,download_path+”/”+name+”.mp3″)
[/python]

改良下,结合bash处理

[python]
#!/bin/env python
import os
import urllib
from xml.dom import minidom
import time
download_time=time.strftime(‘%Y%m%d’,time.localtime())
download_path=os.getcwd()+”/”+”wxf”+download_time

if not os.path.isdir(download_path):
os.mkdir(download_path)

usock = urllib.urlopen(‘http://www.wangxiaofeng.net/mp3player.xml’)
xmldoc = minidom.parse(usock)
usock.close()
songlist = xmldoc.getElementsByTagName(‘song’)
f=open(‘mp3list.txt’,’w’)
for i in range(0,len(songlist),1):
url=songlist[i].attributes[“path”].value.encode(‘utf-8’)
f.write(url+”\n”)

f.close()

[/python]

[bash]
#!/bin/bash

mkdir /home/python_code/wxf20101214
cd !$
cat /home/python_code/mp3list.txt |while read line
do
wget “${line}”
done
[/bash]

url编码后的程序
[python]
#!/usr/local/python271/bin/python
import os
import urllib
from xml.dom import minidom
import time
download_time=time.strftime(‘%Y%m%d’,time.localtime())
download_path=os.getcwd()+”/”+”wxf”+download_time

if not os.path.isdir(download_path):
os.mkdir(download_path)

usock = urllib.urlopen(‘http://www.wangxiaofeng.net/mp3player.xml’)
xmldoc = minidom.parse(usock)
usock.close()
songlist = xmldoc.getElementsByTagName(‘song’)
for i in range(0,len(songlist),1):
url=urllib.quote(songlist[i].attributes[“path”].value.encode(‘gbk’),safe=”:/”)
name=songlist[i].attributes[“title”].value
urllib.urlretrieve(url,download_path+”/”+name+”.mp3″)

[/python]

2025年六月
« 5月    
 1
2345678
9101112131415
16171819202122
23242526272829
30