2009年12月21日 星期一

Google App Engine文件 備份下載

Link from: http://www.manatlan.com/blog/zipme___download_sources_of_your_gae_website__as_a_zip_file

修改app.yaml加入下列二行 :


- url: /zipme
script: zipme.py



以下存檔為zipme.py放置於根目錄下 例:http://gae-id.appspot.com/zipme


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
##########################################################################
ZipMe : GAE Content Downloader
##########################################################################
Just add this lines in your app.yaml :

- url: /zipme
script: zipme.py

##########################################################################
""" # manatlan

from google.appengine.ext import webapp
from google.appengine.api import users

import wsgiref.handlers
import zipfile
import os,re,sys,stat
from cStringIO import StringIO

def createZip(path):

def walktree (top = ".", depthfirst = True):
names = os.listdir(top)
if not depthfirst:
yield top, names
for name in names:
try:
st = os.lstat(os.path.join(top, name))
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
for (newtop, children) in walktree (os.path.join(top, name),
depthfirst):
yield newtop, children
if depthfirst:
yield top, names

list=[]
for (basepath, children) in walktree(path,False):
for child in children:
f=os.path.join(basepath,child)
if os.path.isfile(f):
f = f.encode(sys.getfilesystemencoding())
list.append( f )

f=StringIO()
file = zipfile.ZipFile(f, "w")
for fname in list:
nfname=os.path.join(os.path.basename(path),fname[len(path)+1:])
file.write(fname, nfname , zipfile.ZIP_DEFLATED)
file.close()

f.seek(0)
return f


class ZipMaker(webapp.RequestHandler):
def get(self):
if users.is_current_user_admin():
folder = os.path.dirname(__file__)
self.response.headers['Content-Type'] = 'application/zip'
self.response.headers['Content-Disposition'] = \
'attachment; filename="%s.zip"' % os.path.basename(folder)
fid=createZip(folder)
while True:
buf=fid.read(2048)
if buf=="": break
self.response.out.write(buf)
fid.close()
else:
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write("<a href=\"%s\">You must be admin</a>." %
users.create_login_url("/zipme"))

def main():
application = webapp.WSGIApplication(
[('/zipme', ZipMaker)],
debug=False)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
main()





注意事項:
www.google.coma/a/mydomain 的用戶沒有辦法登入GAE
所以請另外開一個app帳戶, 請mydomain的管理者邀請
Administration
Developers <== Invite a Developer to Collaborate on this Application:
Enter a complete email address

2009年8月29日 星期六

Python Compiler (cx_freeze) for Linux

SourceForge下載: http://sourceforge.net/projects/cx-freeze/files/

Projects http://cx-freeze.sourceforge.net/


# wget cx_Freeze-4.1.tar.gz
# tar xzvf cx_Freeze-4.1.tar.gz
# cd cx_Freeze-4.1

記得看一下說明檔!!
# cat README.txt
------------
Please see cx_Freeze.html for documentation on how to use cx_Freeze.

To build:

python setup.py build
python setup.py install

On Windows I have used the MinGW compiler (http://www.mingw.org)

python setup.py build --compiler=mingw32
python setup.py build --compiler=mingw32 install
------------
安裝
# python setup.py build
# python setup.py install

參考一下範例 samples/
[advanced] [matplotlib] [relimport] [simple] [wx]

測試-先來個簡單的
# cd samples/simple
# ls 看一下有什麼東西
hello.py setup.py 只有二個檔案,內容都很容易懂的

setup.py 內容
------------
# A very simple setup script to create a single executable
#
# hello.py is a very simple "Hello, world" type script which also displays the
# environment in which the script runs
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
# subdirectory that contains the files needed to run the script without Python

from cx_Freeze import setup, Executable

setup(
name = "hello",
version = "0.1",
description = "Sample cx_Freeze script",
executables = [Executable("hello.py")])

------------

測試編譯
# python setup.py build
# ls 看一下多出什麼東西
[build] hello.py setup.py 多出一個 build的目錄
# cd build/
# ls
[exe.linux-x86_64-2.5] 這個目錄的名稱會依使用的核心作命名

# cd exe.linux-x86_64-2.5
# ls -l
-rw-r--r-- 1 root root 41416 2009-08-29 12:34 array.so
-rwxr-xr-x 1 root root 11888 2009-08-29 12:34 hello 編譯好的檔案
-rw-r--r-- 1 root root 42112 2009-08-29 12:34 itertools.so
-rw-r--r-- 1 root root 1473760 2009-08-29 12:34 libpython2.5.so.1.0
-rw-r--r-- 1 root root 74373 2009-08-29 12:34 library.zip
-rw-r--r-- 1 root root 23352 2009-08-29 12:34 zlib.so

ps. 除了hello是剛才測試後的執行檔,其它的都是必須的不能刪除
ps. 記得如果要拷貝到其它地方執行,其它的檔案都要拷貝才能正確執行

# ./hello 測試編譯好的檔案是否有正常執行

ps. 範例中的項目最好都去看一下setup.py
例. [relimport] setup.py 一看就明白,自制模組要如何放置

Python Compiler (freeze) for Linux

資料來源:http://wiki.python.org/moin/Freeze

安裝套件: apt-get install python2.5-examples
套件路徑: /usr/share/doc/python2.5/examples/Tools/freeze/freeze.py
指令參數: python freeze.py -h

測試編譯: python /path/to/freeze.py [參數] 編譯檔名.py

# python /path/to/freeze.py -o dist test1.py
# cd dist
# make
測試編譯後的linux執行檔
# ./test1

---
錯誤提示:
缺少python2.6/config/config.c.in
安裝以下套件即可
apt-get install python2.6-dev

錯誤提示:
/usr/lib/python2.6/config/libpython2.6.a(posixmodule.o): In function `posix_tmpnam':
(.text+0x783): warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
/usr/lib/python2.6/config/libpython2.6.a(posixmodule.o): In function `posix_tempnam':
(.text+0x865): warning: the use of `tempnam' is dangerous, better use `mkstemp'
config.o:(.data+0x98): undefined reference to `init_warnings'
collect2: ld returned 1 exit status
make: *** [client] Error 1

安裝以下套件即可
apt-get install ?????

---
安裝 psyco 加速模組
# wget http://downloads.sourceforge.net/project/psyco/psyco/1.6/psyco-1.6-linux.i386-2.5.tar.gz?use_mirror=nchc
# tar xzvf psyco-1.6-linux.i386-2.5.tar.gz
# cd psyco-1.6
# cp -rf psyco /usr/lib/python2.5/site-packages/

#加入 import psyco
try:
import psyco
psyco.profile()
except:
pass

加入psyco模組,再用freeze作編譯,正常可執行

2009年8月11日 星期二

正規表示式2

引用 http://fannys23.pixnet.net/blog/trackback/e150b26b3d/15367167


檢查工具 http://osteele.com/tools/rework/

1. 對正規表示式來說,任意字元是以點 (.) 表示。
e.g.
a...e 表示 a 與 e 之間有任意三個字元
ae←比對失敗
a12e←比對失敗,因為不足三個字元
abcde←比對成功

2. 中括號中表示指定特定的字元,若其中一個符合則符合
e.g.
a[abc]e
比對成功的例子: aae, abe, ace
比對失敗的例子: aze

3. 小寫的檢查: a-z,大寫的檢查: A-Z,數字的檢查: 0-9
e.g.
a[0-9a-zA-Z]e
在 a 與 e 間插入多個英數字或插入一個以上的特殊符號外,其他都符合規則

4. 在中括號之中的點 (.) ,僅代表一個點。
e.g.
a[.]e
只有 a.e 符合

5. 符號 ^ 在第一個字元出現時有 not 的意思
e.g.
a[^0-9a-zA-Z]e 表示英數字以外的符號符合此項比對

6. 符號 ^ 在第一個字元以外的地方出現,代表 ^ 本身這個字
e.g.
a[0-9^a-zA-Z]e 表示 a 和 e 間出現一個 ^ 或一個英數字均符合

7. 修飾詞:
星號 (*) 可用來代表零或多個
e.g.
a.*z 若字詞頭為 a 尾為 z 則符合
ab*z 若字詞頭為 a 尾為 z ,且中間出現一個以上的 b 則符合
ab.*z 字詞頭為 ab 尾為 z 則符合

問號 (?) 代表零個或一個
w.?e
符合的範例:we、wie
不符合的範例:willie

加號 (+) 代表一個或多個
e.g.
a.+z 在 a 和 z 之間出現一個或以上的字元即符合

若希望在 a 與 z 之間有一個以上非英文大寫的任意字元,
寫法為: a[^A-Z]+z

8. 大括號用來精確比對前一個字
ab{5}z ←僅 abbbbbz 符合
ab{1,5}z ←表示 b 出現最少一次、最多五次
a[A-Z]{1,5}z ←中間可以有一到五個大寫英文字

9. 意義相同的正規表示式:
b{1,} = b+
b{0,} = b*

10. 逸出字元前要加上反斜線
a\.b
a\[b
a\\b

11. 群組(grouping): 用小括號包起來
a(abc)*z ←表示 a 開頭、z 結尾,中間出現任意次數的 "abc"
另外也有記憶小括號的功能
e.g.
import re
m = re.search('it is (fine (today))', 'it is fine today')
m.group(0)
m.group(1)
m.group(2)
#以上程式會依續印出完整字串、左起第一組小括號、第二組小括號

12. 較短的表示方式:
http://www.amk.ca/python/howto/regex/
\w = [a-zA-Z0-9_]
\s = [\t\n \r\f\v]
\d = [0-9]      ←所以 IP 比對可以改寫成 \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
            還可以再縮寫成 \d{1,3}(\.\d{1,3}){3}

大寫則有反義的用途,
例如 \D 是非數字,\W 代表非英數字、\s 代表非空白字元

13. 字和空白之間的交會點是 \b
因此 "Will and Willie are good friends."
可以利用 Will\b 找出 Will (以免同時也比對到 Willie)

14. 正規表示式預設有「貪多」的特性
import re
reg = re.search("t.*d", "today is fine")
reg.group()

這樣的搜尋會一路找到結尾、再找回來、才取出 tod,
會造成效能上的耗費,因此有不貪多演算法,
\.*? ←代表抓取任意字元、任意次數、不貪多
\.+? ←代表抓取任意字元、一次以上、不貪多

不貪多演算法的說明
http://www.gais.com.tw/article.php?u=DeeR&i=20080225

15. 把沒有明顯分隔符號的字串切割重組
e.g.
import re
text = "willie123good456"
"".join(re.split(r"\d+", text))

16. 使用其他人寫好的套件剖析 XML 與 HTML

HTML:
Beautiful Soup
http://www.crummy.com/software/BeautifulSoup/

Parsing HTML 的說明
http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing%20HTML

XML:
ElementTree
http://effbot.org/zone/element-index.htm
Parsing XML 的說明
http://docs.python.org/lib/module-xml.etree.ElementTree.html



【實作練習: 剖析 log 中異常的 IP】
#假設 IP 為 200 開頭的是異常 IP
#!/usr/bin/python

import re
f = open('/tmp/auth.log')
for i in f:
regex = re.search(r'200\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', i)
if regex:
print regex.group()
f.close()

#re.search 表示比對正規表示式與輸入結果
#第一個參數是正規表示式,第二個是輸入內容
#regex.group() 預設參數是 0

tips:
小括號包起來的東西在 python 裡會被記憶,
因此若只想取 ip 最後一段,可改寫如下:

import re
f = open('/tmp/auth.log')
for i in f:
regex = re.search(r'200\.[0-9]{1,3}\.[0-9]{1,3}\.([0-9]{1,3})', i)
#加上小括號
if regex:
print regex.group(1)
#加上小括號所出現的 index (從 1 開始算)
f.close()



【實作練習:分析 log 檔中的非法使用者想入侵的帳號】

#!/usr/bin/python

import re
f = open('/tmp/auth.log')
for i in f:
regex = re.search(r'Invalid user ([^ ]+) from ([^ ]+)', i)
if regex:
print regex.group(1) + " => " + regex.group(2),
f.close()



【實作練習:分析 log 檔中的非法使用者想入侵的帳號 (改善執行效能)】

#!/usr/bin/python

import re
f = open('/tmp/auth.log')
rec = re.compile(r'Invalid user ([^ ]+) from ([^ ]+)')
for i in f:
regex = rec.search(i)
if regex:
print regex.group(1) + " => " + regex.group(2),
f.close()



【實作練習:分析 log 檔中的非法使用者想入侵的帳號 (縮短正規表示式)】

#!/usr/bin/python

import re
f = open('/tmp/auth.log')
rec = re.compile(r'Invalid user (\w+) from ([^ ]+)')
for i in f:
regex = rec.search(i)
if regex:
print regex.group(1) + " => " + regex.group(2),
f.close()



【實作練習:取出 HTML 的部分內容】

from BeautifulSoup import BeautifulSoup
f = open('test.htm')
html = f.read()
f.close()
soup = BeautifulSoup(html)
soup.html.body.span.string         #取出span標籤內夾記的內容
soup.html.body.a.string          #預設會取出第一個找到的 a 標籤夾記的內容
soup.html.body('div')[1].a.string     #取得第二組 div 內的 a 標籤
soup.html.body.div.a['href'] #抓出 a 標籤中的屬性 href



【實作練習:取出 XML 的部分內容】(for python 2.5)

from xml.etree.ElementTree import XML
myxml = open('test.xml').read()
seek = XML(myxml)
seek.getchildren() #確認 seek 可找到哪些子節點
seek.find('staff').find('name').text #取出子節點 staff 中的 name 裡頭的內容
for i in seek.findall('staff'): #找出所有的 staff
print i.find('name').text #取出 staff 中的 name 內容

正規表示式1 regular expressions


出處 http://osteele.com/tools/rework/

For more information about how to use regular expressions, including examples, additional documentation, and additional tools, see:
Regular-Expressions.info (online)
RegExLib (online)
Jeffrey Freidl's Mastering Regular Expressions (Amazon)

Quick Reference:
. any character except newline. If DOTALL, matches newline.
^ the start of the string. In multiline, matches start of each line.
$ the end of the string or just before the last newline. In multiline, matches before each newline.
\d,\w,\s digit, word, or whitespace, respectively
\D,\W,\S anything except digit, word, or whitespace
\. a period (and so on for \*, \(, etc.)
[ab] characters a or b
[a-c] a through c
[^ab] any character except a or b
expr* zero or more repetitions of expr
expr+ one or more repetitions of expr
expr? zero or one repetition of expr
*?,+?,?? ...same as above, but as little text as possible
expr{m} m copies of expr
expr{m,n} between m and n copies of the preceding expression
expr{m,n}? ...but as few as possible
a|b either a or b
(expr) same as expr, but captures the match for use in \1, etc.
(?:expr) doesn't capture the match
(?=expr) followed by expr
(?!expr) not followed by expr

Rsync 參數說明


使用方式:
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC [DEST]
rsync [OPTION]... SRC [SRC]... DEST
rsync [OPTION]... [USER@]HOST::SRC [DEST]
rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

參數說明: (網路上的中文說明好像都這份~~所以也不知道真正的出處在那。)

-h , --help 顯示rsync求助資訊.
--version 顯示rsync版本.
-v , --verbose 複雜的輸出訊息.
-q , --quiet 安靜模式,幾乎沒有訊息產生.常用在以cron執行rsync.
-I, --ignore-times 通常rsync為了加快速度會忽略同樣檔案大小且同樣存取時間點的檔案.可以透過此參數關閉此快速檢查.
--size-only rsync只檢查檔案大小是否改變,不管時間存取點是否改變.通常用在mirror,且對方時間不太正確時.
-c, --checksum 在傳送之前透過128bit的md4檢查碼來檢查所有要傳送的檔案.(會拖慢速度.)
-a, --archive archive mode 權限保存模式,相當於 -rlptgoD 參數.
很快速的保存幾乎所有的權限設定,除了硬式連結(透過-H設定).
-r, --recursive 複製所有下層的資料(遞迴)
-R, --relative 使用相對路徑.
如:
rsync foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo.c檔案
rsync -R foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo/bar/foo.c 檔案
-R, --no-relative 不使用相對路徑.
-b, --backup 目的地端先前已經存在的檔案在傳輸或刪除前會被備份.
--backup-dir=DIR 設定備份的資料夾.
--suffix=SUFFIX 指定備份的檔案名稱字尾形式(預設為~).
-K, --keep-dirlinks 接收方將連結到資料夾的檔案視為資料夾處理
-l, --links 複製所有的連結
-H, --hard-links 保留硬式連結
-p, --perms 保留檔案權限
-o, --owner 保留檔案擁有者(root only)
-g, --group 保留檔案群組
-D, --devices 保留device資訊(root only)
-t, --times 保留時間點
-n, --dry-run 不實際執行傳送,只顯示將會有的傳輸動作
-S, --sparse 嘗試去處理稀疏的檔案,讓這些檔案在目的端佔去較少的磁碟空間.
-W, --whole-file 複製所有的檔案,不額外作檢查.
--no-whole-file 關閉 --whole-file 參數
-x, --one-file-system 不要跨越檔案系統分界(只在一個檔案系統處理)
-B, --block-size=SIZE 強制透過rsync程式去比對修復block-sizeforce
-e --rsh=COMMAND 定義所使用的remote shell
--rsync-path=PATH 定義rsync在遠端機器存放資料的路徑
--existing 只比對更新目的端已經存在的檔案
--ignore-existing 忽略目的端已經存在的檔案(也就是不更新)
--delete 刪除傳送端已經不存在,而目的端存在的檔案
--delete-excluded 除了把傳送端已經不存在,而目的端存在的檔案刪除之外,也刪除 --exclude 參數所包含的檔案.
--delete-after rsync預設會在檔案傳送前進行相關刪除動作確保接收端有足夠的檔案空間,但可以透過 --delete-after 讓刪除動作在檔案傳送後再行刪除.
--ignore-errors 忽略任何錯誤既使是I/O error 也進行 --delete 刪除動作.
--max-delete=NUM 定義rsync不要刪除超過 NUM 個檔案.
--partial rsync若遇到傳輸過程中斷時,會把那些已經傳輸的檔案刪除.在某種狀況下保留那些部分傳送的檔案是令人高興的.你可以透過 --partial 參數達到這個目的.
--partial-dir=DIR 在 --partial 參數啟動時,你還可以定義rsync把那些部分傳送的檔案寫入定義的資料夾,而非直接寫入目的端.需要注意的是,此資料夾不應該被其他使用者可以寫入.(如:/tmp)
--force 當目的端資料夾被傳送端非資料夾名稱覆蓋時,強制rsync刪除資料夾,即使該資料夾不是空的.
--numeric-ids 不將傳送端檔案的uid及gid值,與目的端的使用者/群組進行配對.若傳送端並沒有uid及gid的對應名稱(如:原帳號群組被刪除的遺留檔案),或目的端沒有相對應的帳號/群組,保留數字型態的uid/gid
--timeout=TIMEOUT 設定 I/O 逾時的時間(秒). 超過這個秒數而沒有資料傳送,rsync將會結束.預設為0,也就是沒有定義逾時時間.
-T, --temp-dir=DIR 定義rsync在接收端產生暫時性的複製檔案時使用資料夾暫存.預設是直接在接收端資料夾直接產生暫存檔案.
--compare-dest=DIR 定義rsync在目的端建立資料夾來比對傳送過來的檔案.
--link-dest=DIR 與 --compare-dest 相同,但同時會針對無法改變的檔案建立硬式連結.
-z, --compress 壓縮模式,當資料在傳送到目的端進行檔案壓縮.
-P -P參數和 --partial --progress 相同.只是為了把參數簡單化.
-C, --cvs-exclude 排除那些通常不希望傳送的檔案.定義的方式與CVS傳送相同:
RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS .make .state .nse_depinfo *~ #* .#* ,* _$* *$ *.old *.bak *.BAK *.orig *.rej .del-* *.a *.olb *.o *.obj *.so *.exe *.Z *.elc *.ln core .svn/ 符合以上條件的都會被忽略而不傳送.
--exclude=PATTER 符合PATTERN(規則表示式)樣式的檔案不進行傳送
--exclude-from=FILE 和--exclude參數相同,不過是把不進行傳送的檔案事先寫入某一檔案.執行時,透過此參數讓rsync讀取.(; #開頭的行列或空白行會被rsync忽略)
--include=PATTERN 定義rsync不要排除符合pattern樣式的檔案.
--include-from=FILE 和--include參數相同,只不過把要包含的檔案寫到某一檔案.
--files-from=FILE 把要傳送的檔案名稱都精確的寫入某一檔案,讓rsync讀取.
如: rsync -a --files-from=/tmp/foo /usr remote:/backup
-0 --from0 定義檔案所要讀取的檔案是null字元結尾.
--version 顯示版本訊息.
--daemon 定義rsync以daemon型態執行.
--no-detach 當以daemon型態執行時,不要進行分裂且變成背景程序.
--address=ADDRESS 定義所要連結(bind)的ip位址或是host名稱(daemon限定)
--config=FILE 定義所要讀取的設定檔rsyncd.conf位置(daemon限定) 預設值為 /usr/local/etc/rsyncd.conf
--port=PORT 定義rsyncd(daemon)要執行的port(預設為tcp 873)
--blocking-io 使用blocking I/O連結遠端的shell,如rsh , remsh
--no-blocking-io 使用non-blocking連結遠端的shell,如ssh (預設值)
--stats 顯示檔案傳送時的資訊狀態
--progress 顯示傳送的進度.(給檔案傳送時,怕無聊的人用的..)
--log-format=FORMAT 定義log的格式(在rsyncd.conf設定)
--password-file=FILE 從檔案讀取與遠端rsync伺服器連結的密碼
--bwlimit=KBPS 定義傳輸頻寬的大小(KBytes/秒)
--write-batch=FILE 把紀錄資料寫入一個檔案(給其他相同環境且相同需求的機器使用)
--read-batch=FILE 透過讀取紀錄檔案來進行傳輸.(檔案由 --write-batch 參數產生)
--checksum-seed=NUM 定義檔案 checksum-seed 的大小(byte)
-4 --ipv4 使用IPv4協定
-6 --ipv6 使用IPv6協定

2009年8月4日 星期二

Python 套件管理

http://sourceforge.net/projects/pythonpkgmgr/

Python Package Manager is a cross platform tool for Python to assist with the downloading and installation of python packages. Coded in Python, and using wxWidgets, this program is a GUI that drives easy_install and/or pip.




psyco python加速模組


#Download From:
#python 2.6 win32
# http://www.voidspace.org.uk/python/modules.shtml#psyco
#
#python 2.5 win32
# http://sourceforge.net/projects/psyco/files/
#
# psyco is a python accelerator which speeds up pysync by 33%

#說明網頁
#http://psyco.sourceforge.net/psycoguide/node8.html

#For larger applications, try:
import psyco
psyco.profile()

#------
try:
import psyco
psyco.profile()
except:
pass

#other import
from xxxx import *
import xxx,xxxx

#------
if __name__ == '__main__':
# Import Psyco if available
try:
import psyco
psyco.full()
except ImportError:
pass
# ...your code here...

#------
psyco.full()#對所有函數用psyco進行編譯
psyco.bind(myfunction1)#對選擇的函數用psyco進行編譯

g = psyco.proxy(f) #對函數 f用psyco進行編譯
g(args) #Psyco-accelerated call #編譯後 g函數速度會有提升
f(args) #regular slow call #f函數保持原來的調用速度

psyco.log # 用來紀錄log #Enable logging to a file named xxx.log-psyco by default, where xxx is the name of the

psyco.profile() # 可用來替代 psyco.full()

#------
轉貼:: http://www.cnpython.org/docs/300/p_240.html

使用時,在需要做效率優化的源文件前面加入以下兩句就一切OK了
import psyco
psyco.full()

psyco.profile()可以對大程序進行適當分析,以確定哪些函數最值得編譯。
psyco.log()函數用來記錄profile()得到的信息,下次就可以運行就能更快一點。
psyco.bind(myfunc)指定對函數myfunc進行編譯,可以做到比full()更精細的控制。
psyco.proxy(f)創建一個新的函數,它的代碼是由f編譯得到二進制碼。

import math, timeit, psyco

def TestA():
res, loopcnt = 0.0, 100
for i in range(loopcnt):
for j in range(loopcnt):
for k in range(loopcnt):
res = res + math.sin(i+j+k) # loopcnt^3 times

if __name__=='__main__':
TestB = psyco.proxy(TestA)
ta = timeit.Timer("TestA()", "from __main__ import TestA")
tb = timeit.Timer("TestB()", "from __main__ import TestB")
print("TestA(): %.2fs" % (ta.timeit(10)))
print("TestB(): %.2fs" % (tb.timeit(10)))

運行結果如下:
$ python test.py
TestA(): 15.84s
TestB(): 1.82s

很明顯,使用Psyco處理過的函數執行速度快了大約7~8倍,比Psyco作者宣稱的平均加速4倍的倍率更大些。
Psyco的工作原理類似JIT,只不過是on-the-fly的,對用戶而言基本透明。Psyco的主要缺點是耗費內存資源較多,在我看來不是什麼大問題,也許做網絡服務和企業應用的人才需要考慮這些吧。

tw.archive.ubuntu.com apt-get掛站處理方法

## source.list 將tw.archive.ubuntu.com取代為mirror.nttu.edu.tw

/etc/apt# vim sources.list
:1,$s/tw.archive.ubuntu.com/mirror.nttu.edu.tw/g


VIM字串搜尋與取代
s(substitute)指令可搜尋某行列範圍。
g(global)指令則可搜尋整個編輯緩衝區的資料。
s指令以第一個滿足該條件的字串為其取代的對象,若該行有數個滿足該條
件的字串,也僅能取代第一個,若想取代所有的字串則需加上g參數。
:1,$s/old/new/g 將檔案中所有的『old』改成『new』。
:10,20s/^/ / 將第10行至第20行資料的最前面插入5個空白。
:%s/old/new/g 將編輯緩衝區中所有的『old』改成『new』。

2009年7月24日 星期五

[python] 字串處理 去除空白strip() 分割split(' ')

python 3.0
strip(),split()己經被 str模組取代 或使用 string模組


>>> AA = " 1 abc "
>>> AA.strip()
'1 abc'
>>> print AA
' 1 abc '
>>> AA.strip().split(' ')
['1', 'abc']
>>>



fobj = file('datafile.txt', 'r')
#使用迴圏抓取文字檔內容,依空白作為分隔,取出資料後插入資料庫中.
# Insert the data to database.
for line in fobj:
#讀入一行,使用strip()去除(頭/尾)的空白,使用split('空白')分割為序列items[]
items = line.strip().split(' ')
#代入items[]序列,產生一個字典型態變數values{'key1':'data1','key2':'data2'}
values = {'id': items[0], 'name': items[1]}

#關閉開檔
fobj.close()

2009年7月23日 星期四

[Ubuntu] 取得硬碟 UUID

[Ubuntu] 取得硬碟 UUID
Ubuntu fstab 是採用 uuid 處理,所以有幾個方法可以取得 uuid

vol_id 指令
sudo vol_id /dev/sda1

用 ls 指令
ls -l /dev/disk/by-uuid/

用blkid
blkid /dev/sda1

2009年7月18日 星期六

[紀錄]找回Linux丟失的RAID設備

[紀錄]找回Linux丟失的RAID設備
轉載:http://blog.csdn.net/shaohui/archive/2009/03/13/3985804.aspx

最近遇到一件讓我很頭疼的事情,自己維護的Linux文件服務器一下子無緣無故崩掉了。 根文件系統裏面很多東西文件消失了。所有的數據全在一個RAID設備上面,但是/etc/下的mdadm.conf也沒有了。UUID丟失了。

這個RAID上面有1T 的數據,備份服務器空間不足,沒有全部備份,數據丟失了還不讓我瘋掉才怪呢。

直覺告訴我RAID上的數據是可以找回來的, 於是把系統重新安裝了一下,查了一下RAID的資料,其實只有一條命令就可以恢複安裝系統以前的RAID設備.

mdadm -A /dev/md0 /dev/sdb1 /dev/sdc1 /dev/sdd1

其中/de/sdb1 /dev/sdc1 /dev/sdd1,是以前RAID當中的幾塊硬盤。

#RAID的配置文件
/etc/mdadm.conf 的文件主要提供我們方便管理,編輯這個文件可以讓RAID更好的為我們工作,當然這個步驟不是必要的.不經過編輯配置文件也可以讓RAID工作。
首先掃描系統中的全部陣列

mdadm --detail -scan

關於UUID,也很容易用mdadm重新找回,然後重新生成以下mdadm.conf,問題居然就這麼解決了。

不過,這樣驚險的時候,以後再也不敢這麼折騰了。

2009年6月23日 星期二

SAMBA建立密碼登入

系統中須先存在使用者
# useradd user
# useradd user1

# smbpasswd -a user1
# 新增使用者user,並要求輸入密碼

# smbpasswd -a user -n
# 新增使用者user,並且使用空白密碼


smb.conf 部份設定內容
----------------
[global]
dos charset = cp950
unix charset = cp950
workgroup = USER
netbios name = SAMBA00
server string = SAMBA-SERVER

#使用者認證
security = share
guest ok = Yes
encrypt passwords = yes
smb passwd file = /etc/samba/smbpasswd

#關閉印表機
load printers = No
printing = bsd
show add printer wizard = No
disable spoolss = yes


[user]
path = /home/user
browseable = yes
read only = yes ##唯讀
create mode = 0777
directory mode = 0777
veto oplock files = /*.ldb/*.dat/

[user1]
path = /home/user1
browseable = yes
read only = No ##可讀可寫
valid users = user1 ##使用者
create mode = 0777
directory mode = 0777
veto oplock files = /*.ldb/*.dat/

2009年6月15日 星期一

BattleNet會鎖序號!!

駭害不要來列表
1=!xSpeed
2=!xSpeed.Net
3=anti-hack
4=CDKey Grabber
5=Cheatengine speed hack
6=Chicken hack
7=crash hack
8=D2HackIt
9=Diablo 2 bot scripts
10=EasyMap v
11=EasyPlay v
12=Exzap's
13=GearNT
14=Gearnt speedhack
15=hack variant
16=iMap map hack
17=lineage speedhack
18=Map Hack
19=Map_Hack
20=MapHack
21=Map-Hack
22=Monster Map Hack
23=Mousepad's
24=SF W3 CDKey Grabber
25=SF W3/W3X CDKey Grabber
26=ShadowFrench's
27=SpeederXP
28=SpeedGear
29=Sting's Hackmap
30=Teleport hack
31=W3 / W3X MapHack
32=W3 CDKey Grabber
33=W3/W3X CDKey Grabber
34=W3XMapHack
35=W3XMH
36=W3XMH
37=Xliqz's
38=Zerocraft
39=zMaphack
40=Nano maphack
41=Skelletor multi
42=iNHALE
43=Aspeeder

2009年5月12日 星期二

UFW簡易設定

#開啟
ufw disable
ufw default deny
ufw logging OFF
ufw enable
ufw allow 22/tcp
ufw allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 3260
ufw allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 20000
ufw allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 139
ufw allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 445
ufw allow proto udp from 192.168.1.0/24 to 192.168.1.117 port 137
ufw allow proto udp from 192.168.1.0/24 to 192.168.1.117 port 138


#關閉/刪除 條件
ufw delete allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 3260
ufw delete allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 20000
ufw delete allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 139
ufw delete allow proto tcp from 192.168.1.0/24 to 192.168.1.117 port 445
ufw delete allow proto udp from 192.168.1.0/24 to 192.168.1.117 port 137
ufw delete allow proto udp from 192.168.1.0/24 to 192.168.1.117 port 138

Bonding in Ubuntu

How to Set up Network Bonding in Ubuntu 6.10
Why you may want to do this:
Network Bonding, otherwise known as port trunking allows you to combine multiple network ports into a single group, effectively aggregating the bandwidth of multiple interfaces into a single connection. For example, you can aggregate two gigabyte ports into a two-gigabyte trunk port. Bonding is used primarily to provide network load balancing and fault tolerance. First, we will run two different network tools to check for network connectivity and capability. Run mii-tool to check your interfaces for connectivity:

mii-tool

For our purposes, we will assume you have three interfaces. The result of the mii-tool command is listed below:

eth0: negotiated 100baseTx-HD, link ok
eth1: negotiated 100baseTx-HD, link ok
eth2: negotiated 100baseTx-HD, link ok

Next run ethtool for each interface to check to see what capabilities:

ethtool eth0 && ethtool eth1 && ethtool eth3

The result of the ethtool command is listed below:

Settings for eth0:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Half
Port: MII
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: g
Wake-on: g
Current message level: 0x00000007 (7)
Link detected: yes

Settings for eth1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised auto-negotiation: Yes
Speed: Unknown! (65535)
Duplex: Unknown! (255)
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: umbg
Wake-on: d
Current message level: 0x00000007 (7)
Link detected: no

Settings for eth3:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised auto-negotiation: Yes
Speed: Unknown! (65535)
Duplex: Unknown! (255)
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: umbg
Wake-on: d
Current message level: 0x00000007 (7)
Link detected: no

Next, we need to install ifenslave. It’s a simple install:

apt-get update && apt-get install ifenslave

Options for mode types:
You can set up your bond interface according to your needs. In order to do this, you simply change the mode type depicted in the examples below (mode=X). There are seven mode types available. They are as follows:

mode=0

This mode uses the Round-robin policy: Transmit packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.

mode=1

This mode uses an Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance. The primary option affects the behavior of this mode.

mode=2

Transmit based on [(source MAC address XOR'd with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.

mode=3

Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

mode=4

IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.

*Pre-requisites:

1. Ethtool support in the base drivers for retrieving the speed and duplex of each slave.

2. A switch that supports IEEE 802.3ad Dynamic link aggregation. Most switches will require some type of configuration to enable 802.3ad mode

mode=5

Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.

*Prerequisite: Ethtool support in the base drivers for retrieving the speed of each slave.

mode=6

Adaptive load balancing: includes balance-transmit load balancing plus receive load balancing for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the source hardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server.

Now append the following items to your aliases file:

pico /etc/modprob.d/aliases

# Append to the bottom of this file:
alias bond0 bonding
alias eth0 e100
alias eth1 e100
alias eth2 e100
options bonding mode=0 miimon=100

Next, append the following items to your i386 file:

pico /etc/modprob.d/arch/i386

# Append to the bottom of this file:
alias bond0 bonding
options bonding mode=0 miimon=100 downdelay=200 updelay=200

Now we have to modify the interface file. Start off by commenting out any information on the physical interfaces, eth0, eth1, etc, and create a virtual interface such as bond0, configure it similar to below, and be sure to choose a unique hwaddress. Be sure to leave the loopback interface configuration intact.

pico /etc/network/interfaces

It should look something like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto eth0
#iface eth0 inet static
# address 192.168.0.120
# netmask 255.255.255.0
# network 192.168.0.0
# broadcast 192.168.0.255
# gateway 192.168.0.1
auto bond0
iface bond0 inet static
address 192.168.0.120
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
hwaddress ether 00:03:B3:48:50:2C
post-up ifenslave bond0 eth0 eth1

Save the file and then reboot the system:

shutdown -r now

BASH測試條件

VariableName?=VariableValue? ,指定變數。使用 $VariableName? 來取用變數。
使用 \$$VariableName? 來 在變數前加上 $。
可使用 export VariableName?1 VariableName?2 來當作其它的程式的參數使用。
VariableName?=$Command,指定變數為 Command 執行後的結果。

使用 $0 取得執行的指令名稱 。
使用 $1 取得第一個參數,其餘依此類推。
使用 $# 取得參數的總數。
使用 $? 取得上一個指令的傳回值。
使用 $* 或是 $@ 來得到所有的參數,中間以空白隔開。
使用 $Environment 來 取得環境變數。
read VariableName? , 讓使用者輸入變數的內容。
 
條件測試:

test Expression
echo $?(1 為假,0 為真。和一般的 C 習慣相反。)
 
[Expression]
echo $?
Expression 的類型:

字串運算式,不得使用萬用字元。
String1 = String2 :如果 String1 和 String2 一樣,則回傳值為真。
String1 == String2 :如果 String1 和 String2 一樣,則回傳值為真。
String1 != String2 :如果 String1 和 String2 不一樣,則回傳值為真。
String :如果 String 不是 Null,則回傳值為真。
-n String :如果 String 的長度大於 0,則回傳值為真。
-z String :如果 String 的長度等於 0,則回傳值為真。
數值運算式
integer1 -eq integer2 :如果 integer1 等於 integer2 ,則回傳值為真。
integer1 -ge integer2 :如果 integer1 大於等於 integer2 ,則回傳值為真。
integer1 -gt integer2 :如果 integer1 大於 integer2 ,則回傳值為真。
integer1 -le integer2 :如果 integer1 小於等於 integer2 ,則回傳值為真。
integer1 -lt integer2 :如果 integer1 小於 integer2 ,則回傳值為真。
integer1 -ne integer2 :如果 integer1 不等於 integer2 ,則回傳值為真。
檔案運算式
-d FileName? :若 FileName? 存在,並且為目錄,則回傳值為真。
-f FileName? :若 FileName? 存在,並且為一般檔案,則回傳值為真。
-s FileName? :若 FileName? 存在,並且的長度大於 0,則回傳值為真。
-r FileName? :若 FileName? 存在,並且可讀取,則回傳值為真。
-w FileName? :若 FileName? 存在,並且可寫入,則回傳值為真。
-x FileName? :若 FileName? 存在,並且可執行,則回傳值為真。
-b FileName? :若 FileName? 存在,並且為 block,則回傳值為真。
-c FileName? :若 FileName? 存在,並且為 character,則回傳值為真。
-e FileName? :若 FileName? 存在,則回傳值為真。
-g FileName? :若 FileName? 存在,並且為 set-group-id,則回傳值為真。
-k FileName? :若 FileName? 存在,並且設定了 sticky 字元,則回傳值為真。
-L FileName? :若 FileName? 存在,並且為連結檔,則回傳值為真。
-p FileName? :若 FileName? 存在,並且為 named pipe (FIFO),則回傳值為真。
-S FileName? :若 FileName? 存在,並且為 socket,則回傳值為真。
-u FileName? :若 FileName? 存在,並且為 set-user-id,則回傳值為真。
邏輯運算式
! Expression :如果 Expression 的結果值為假,則回傳值為真。
Expression1 -a Expression1 :將 Expression1 和Expression2 的結果值做And 運算。
Expression1 -o Expression1 :將 Expression1 和Expression2 的結果值做Or 運算。

MDADM-raid指令

#停止磁碟陣列
mdadm --manage --stop /dev/md0

#啟動磁碟陣列
mdadm --assemble --run /dev/md0

#查看RAID狀態
# mdadm --detail /dev/md0

#建立RAID5
mdadm --create /dev/md1 --level=raid5 --raid-devices=5 --chunk=4 /dev/sd{b,c,d,e,f}1
mdadm --create /dev/md1 --level=raid5 --raid-devices=5 --chunk=4 /dev/sd{g,h,i,j,k}1

#建立 RAID0
mdadm --create /dev/md0 --level=raid0 --raid-devices=3 /dev/sda4 /dev/sdb1 /dev/sdc1
mdadm --create /dev/md0 --level=raid0 --raid-devices=5 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde3

#RAID的配置文件
/etc/mdadm.conf 的文件主要提供我們方便管理,編輯這個文件可以讓RAID更好的為我們工作,當然這個步驟不是必要的.不經過編輯配置文件也可以讓RAID工作。
首先掃描系統中的全部陣列

# mdadm --detail -scan

ARRAY /dev/md0 level=raid5 num-devices=3 UUID=37230e69:51bcf476:9889f244:18b07644
devices=/dev/sda,/dev/sdb,/dev/sdd,/dev/sdc
# vi /etc/mdadm.conf
-------------------------------------------------------------------------------------------------------
DEVICE /dev/sdb1 /dev/sdc1 /dev/sdd1
ARRAY /dev/md0 level=raid5 num-devices=3 UUID=37230e69:51bcf476:9889f244:18b07644
devices=/dev/sda,/dev/sdb,/dev/sdd,/dev/sdc
-------------------------------------------------------------------------------------------------------
其中的資料就如同 mdadm --detail -scan 一樣定義了RAID基本資料.
如果你不想手動設定用下面的指令同樣也可以.
mdadm --detail -scan >> /etc/mdadm.conf

#RAID的管理
mdadm /dev/md0 --add /dev/sdd1
mdadm /dev/md0 --fail /dev/sdc1
mdadm /dev/md0 --remove /dev/sdc1
mdadm --manage --run /dev/md0 重建指令 !!小心使用!!

#spare-group

#Multipath
mdadm -C /dev/md0 --level=multipath --raid-devices=2 /dev/sda1 /dev/sdb1


ARRAY /dev/md0 level=raid5 num-devices=5 metadata=00.90 spares=1 UUID=90dcc677:e6ef7c62:9baf8322:4f9d823d
ARRAY /dev/md1 level=raid5 num-devices=5 metadata=00.90 spares=1 UUID=981e1b70:736d1fb5:9baf8322:4f9d823d

VBOX222-ubuntu安裝流程說明

#下載VBOX
# http://www.virtualbox.org/wiki/Linux_Downloads
http://download.virtualbox.org/virtualbox/2.2.2/virtualbox-2.2_2.2.2-46594_Ubuntu_jaunty_i386.deb

#安裝網路橋接原件
sudo apt-get install bridge-utils uml-utilities

#安裝常用套件
sudo apt-get install wget cron openssh-server portmap nfs-common vsftpd gpm

#加入使用者到vboxuser 群組
usermod -G vboxusers -a user name

======== vboxbridge.sh
#!/bin/sh
#/usr/bin/gksudo /root/vboxbridge eth0 tap1 start
#/usr/bin/gksudo /root/vboxbridge eth0 tap2 start
#/usr/bin/gksudo /root/vboxbridge eth0 tap3 start

#Put your username here
USERNAME= user name

TAP_INTERFACE="$2"
HOST_INTERFACE="$1"
BRIDGE_INTERFACE="br0"
TAP_COUNT=`ifconfig | grep -c tap`
case "$3" in
start)
if [ `ifconfig | grep -c $TAP_INTERFACE` = 0 ]; then
#create the bridge if it does not exist
if [ `ifconfig | grep -c $BRIDGE_INTERFACE` = 0 ]; then
brctl addbr $BRIDGE_INTERFACE
#check if we are using DHCP and retrieve configuration if IP is static
if [ `ps ax | grep -c "dhclient $HOST_INTERFACE"` = 2 ]; then
ifconfig $HOST_INTERFACE 0.0.0.0 promisc
brctl addif $BRIDGE_INTERFACE $HOST_INTERFACE
dhclient $BRIDGE_INTERFACE
else
IP_ADDRESS=`ifconfig $HOST_INTERFACE | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
BROADCAST_ADDRESS=`ifconfig $HOST_INTERFACE | grep 'inet addr:' | cut -d: -f3 | awk '{ print $1}'`
SUBNET_MASK=`ifconfig $HOST_INTERFACE | grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'`
DEFAULT_GATEWAY=`route -n |tail -n1|cut -d' ' -f10`
ifconfig $HOST_INTERFACE 0.0.0.0 promisc
brctl addif $BRIDGE_INTERFACE $HOST_INTERFACE
ifconfig $BRIDGE_INTERFACE $IP_ADDRESS netmask $SUBNET_MASK broadcast $BROADCAST_ADDRESS
route add default gw $DEFAULT_GATEWAY
fi

fi
#add tap interface to bridge
tunctl -t $TAP_INTERFACE -u $USERNAME
brctl addif $BRIDGE_INTERFACE $TAP_INTERFACE
ifconfig $TAP_INTERFACE up
chmod 0666 /dev/net/tun
else
echo "Interface $TAP_INTERFACE already configured"
fi
;;
stop)
if [ `ifconfig | grep -c $TAP_INTERFACE` = 0 ]; then
echo "Interface $TAP_INTERFACE does no exist"
else
#shut down tap interface and remove it from bridge
ifconfig $TAP_INTERFACE down
brctl delif $BRIDGE_INTERFACE $TAP_INTERFACE
tunctl -d $TAP_INTERFACE
#we remove the bridge if this was the last tap interface
if [ $TAP_COUNT = 1 ]; then
brctl delif $BRIDGE_INTERFACE $HOST_INTERFACE
ifconfig $HOST_INTERFACE 0.0.0.0 -promisc
#check if we are using DHCP and retrieve configuration if IP is static
if [ `ps ax | grep -c "dhclient $BRIDGE_INTERFACE"` = 2 ]; then
ifconfig $BRIDGE_INTERFACE down
brctl delbr $BRIDGE_INTERFACE
dhclient $HOST_INTERFACE
else
IP_ADDRESS=`ifconfig $BRIDGE_INTERFACE | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
BROADCAST_ADDRESS=`ifconfig $BRIDGE_INTERFACE | grep 'inet addr:' | cut -d: -f3 | awk '{ print $1}'`
SUBNET_MASK=`ifconfig $BRIDGE_INTERFACE | grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'`
DEFAULT_GATEWAY=`route -n |tail -n1|cut -d' ' -f10`
ifconfig $BRIDGE_INTERFACE down
brctl delbr $BRIDGE_INTERFACE
ifconfig $HOST_INTERFACE $IP_ADDRESS netmask $SUBNET_MASK broadcast $BROADCAST_ADDRESS
route add default gw $DEFAULT_GATEWAY
fi
fi
fi
;;
*)
echo "Usage: vboxbridge hostinterface tapinterface {start|stop}" >&2
exit 1
;;
esac
==========

2009年1月5日 星期一

修正Ubuntu console-kit-daemon錯誤

修正Ubuntu console-kit-daemon錯誤
出處[Ubuntu]CRITICAL: cannot initialize libpolkit
現象
/var/log/syslogを見てみたら10分おきにエラーメッセージが出てた。

Dec 27 10:40:01 xxxxx console-kit-daemon[22376]: CRITICAL: cannot initialize libpolkit
Dec 27 10:50:01 xxxxx console-kit-daemon[22463]: CRITICAL: cannot initialize libpolkit

対処方法
CRITICALなんて出てるんでびびって調べてみた。対処方法は下記の通り。

Ubuntu 8.10 - libpolkit error | Martin Bergek
$ sudo apt-get install policykit

原因
PolicyKitとは
PolicyKit - Wikipedia
Wikipediaに出てた。びっくり。権限とかを細かく管理できるんですね。どうりでcron周りでエラーが出てたわけだ。

libpolkit
デフォルトだとlibpolkitとPolicyKitは関連パッケージとして入らないのに、依存関係はあるいというのが原因でした。この辺をざっと眺めただけですけどね。

Bug #275432 in policykit (Debian): 徑ibpolkit requires files from policykit for polkit_context_init to work?