發表文章

目前顯示的是 3月, 2016的文章

Python 的虛擬環境及多版本開發利器─Virtualenv 與 Pythonbrew

作者是 Mosky Virtualenv 和 Pythonbrew 都是可以創造虛擬(獨立)Python 環境的工具,只是虛擬(獨立)標的不同。 Virtualenv 可以隔離函數庫需求不同的專案,讓它們不會互相影響。在建立並啟動虛擬環境後,透過  pip  安裝的套件會被放在虛擬環境中,專案就可以擁有一個獨立的環境。 簡而言之,Virtualenv 可以幫你做到: 在沒有權限的情況下安裝新套件 不同專案可以使用不同版本的相同套件 套件版本升級時不會影響其他專案 Pythonbrew 則可以在家目錄中安裝多個 Python,並迅速地切換版本;也可以在指定的 Python 版本下批次測試你的 Python 程式;另外更整合了 Virtualenv。 這篇文章會詳細介紹這兩個工具,讓你在多人開發及多版本開發的環境中更得心應手。 事前準備 Python 的 package 通常會上傳至  PyPI ,有很多工具都可以從  PyPI  安裝 package。下面會使用 easy_install  這個工具(由  setuptools  提供)來安裝 Virtualenv 和 Pythonbrew。 01. Linux 如果不知道  easy_install  或還沒安裝 setuptools,在 Debian/Ubuntu 可以用下列指令安裝: $ sudo apt-get install python-setuptools 在 Fedora/CentOS/Redhat/openSUSE,則可以使用: $ su - # yum install python-setuptools 02. Windows 在 Windows 則可以從  setuptools  的頁面找到  *.exe  格式的安裝檔案。安裝完後,可以在 C:\PythonX.Y\Scripts\ (X.Y 是 Python 的版本)下找到  easy_install.exe 。記得把這個路徑放進 Windows 環境變數中的 PATH。 接著就可以輕鬆安裝任何在  PyPI ...

建立 OpenVAS 6 伺服環境

圖片
Debian7 + OpenVAS 6 Server 方案 本版是以 GNU/Linux Debian 7 為基底,加上 2014 年 2 月的 OpenVAS 6 ,本版特色如下: 1.        Debian 7 只提供文字介面。 2.        OpenVAS 具有強力掃描能力,可分析出大多數主機弱點。 3.        可由 Windows 連線至 OpenVAS6 Server ,直接掃描並下載報表。 運作簡介 Debian7 + OpenVAS6 VM 檔下載 §   http://120.116.12.4/download/DebVas-20140219.ova 請下載回來後,先用 MD5 檢查工具查一下 MD5 碼是否一致,若不同請重新下載。 §   檔案 MD5 碼: 4cf245ff3568fdd636bc246582d3558c §   Win7 MD5 檢查工具介紹:   http://briian.com/?p=6457 §   Linux MD5 檢查指令 root@mylinux:~#  md5sum DebVas-20140219.ova 滙入 Virtualbox 1.        「檔案」/「滙入應用裝置」 2.        「開啟應用裝置」→挑 DebVas-20140219.ova →「下一步」 3.        「滙入」 ..... 等 .... 等 ... 等 啟動 VM §   注意 1.        採用 DHCP 自動取得 IP ,建議在有 DHCP 的網路環境下使用。 2. ...

Python Language, Using "subprocess" module control process

import subprocess,os child = subprocess.Popen(["ping","-c","10","www.google.com"]) child.wait() # key man print("parent process") os.system("rm iperf2-client.bat")

Python Language , using "ps" and "grep" to find the processes and kill

#!/usr/bin/python import subprocess, re from subprocess import Popen from subprocess import PIPE keyword = "sample"  # Setting "grep" keyword ptn = re.compile("\s+") p1 = Popen(["ps", "-aux"], stdout=PIPE) p2 = Popen(["grep", keyword], stdin=p1.stdout, stdout=PIPE) p1.stdout.close() output = p2.communicate()[0] print("Output:\n{0}".format(output)) lines = output.strip().split("\n") for line in lines:     items = ptn.split(line)     print("kill {0}...".format(items[1], subprocess.call(["kill", items[1]])))

使用python 監控程式, 實現watchdog功能

import os, sys, time while True:     time.sleep(4)     try:         ret = os.popen('ps -C sample -o pid,cmd').readlines() # 所需監控程式         if len(ret) < 2:             time.sleep(3)             os.system("~/remote/sample host SID rdz.orbwebsys.com") # 程式重新啟動     except:         print "Error", sys.exc_info()[1]