顯示具有 vbs 標籤的文章。 顯示所有文章
顯示具有 vbs 標籤的文章。 顯示所有文章

2014年1月13日 星期一

copy.com&工作排程器使用vbs引數&相對路徑與使用效能監視器監視硬碟空間

copy.com提供多個資料夾同步的功能,方法是將要同步的資料夾建立捷徑在copy資料夾內。
相對路徑是指執行程式的位置,不是vbs指向的程式位置,要注意。而在工作排程器中則必須設定開始位置來定義相對路徑。

NOTES:
“Any Directory” Syncing. Okay, we totally make up this name but it is exactly how it sounds. While Dropbox limits us to sync only files in a central folder (aptly named Dropbox), Copy.com lets you sync any directories on your computer. You can do this by creating a shortcut to that folder and place it within Copy directory. Not very intuitive like SugarSync though, which lets us do exact same thing within right-click menu.

Found a solution if any one is interested the only way it would take the parameters is by adding the path of the wscript to the scheduler to the run line in the scheduler. 
Although wscript is the default for vbs files the scheduler would still ignore the parameters. 
So to get it working the run text in the scheduler should be:

C:\WINDOWS\system32\wscript.exe C:\TASKS\test.vbs one two

I am not sure why this works when other methods dont but I have read that parameters are only passed to .exe files so this may be why they are ignored for .vbs files.

Thanks for the help from KrisSimonis & dglienna.


  • Telnet is an extremely limited tool. A much better utility would be sc.exe (http://www.securityfocus.com/data/tools/nc11nt.zip). In a batch file you could use it like so:
    @echo off
    nc -z 81.6.53.245 1724
    if %ErrorLevel% EQU 0 (echo Port is open) else (echo Port is not open)
    While you could embed nc.exe in a VB Script file, this only complicates the matter.
    Alternatively, if you run Windows 7, portqry.exe could do a similar job.
    • Marked as answer by zsustar Thursday, July 29, 2010 8:11 AM
    Thursday, July 29, 2010 5:43 AM
    Avatar of Forest brook
    (MCC)
    19,605 Points
  • Portqry is helpful to my case because the parameter -q will
    returns 0 if port is listening
    returns 1 if port is not listening
    returns 2 if port is listening or filtered 
    And i draft a script as following~
    ***************
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Return = WshShell.Run("PortQry.exe -nwww.google.com -p tcp -e 80 -q", 1, true)
    WScript.Echo Return
    *****************
    It's cool. Thank you Peqasus.
    BTW, nc.exe is not working, maybe it's killed by my Norton. Anyway i prefer the tools from MS in term of security control.


會要這個功能,是因為我朋友反應說。硬碟空間不夠,就是要再買大容量的硬碟來換。但總不能等到磁碟空間都滿了才發現吧?所以,只好上網找一下 Solution 囉!
因為之前已經做過利用 MailSend 這支小程式來發信,現在只要能夠找到觸發執行批次檔的方式就行了。
上網看了有用 Power ShellVBScript 來做的,不過,我想我還是兜一下好了。所以,就利用「效能監視器」的「警示」功能囉!
第一個動作,當然是先寫一下 batch 檔囉!因為我有切三個磁區,所以就分別監控囉!先利用 psinfo.exe -d 來檢視各磁碟的狀況。
因為對 Linux 比較熟,所以就習慣用剩餘空間百分比來做觸發條件囉!但因為三個磁區的容量差異還蠻大的,經換算後,就以空間剩 85%90%97% 來做為觸發條件。
列一下我所寫的 batch 檔:
echo Volume Type  Format     Label                            Size            Free          Free > C:\sch\alarmhdd_c.txt
C:\pstools\psinfo.exe -d |find "C: Fixed" >> C:\sch\alarmhdd_c.txt
rem 郵件標題:伺服器C磁碟使用率達 85%
C:\sch\mailsend -d msa.hinet.net -smtp mail.domain.com.tw -t admin@domain.com.tw -f hddcheck@hinet.net -sub "=?UTF-8?B?5Ly65pyN5Zmo77yj56OB56Kf5L2/55So546H6YGUIDg177yF?=" -m C:\sch\alarmhdd_c.txt
因為 E-Mail 主旨有中文,怕有編碼上的問題,就利用網路上有線上轉換 Base64 的網頁,進行 Base64 編碼的動作。
收到信的內容就大概如下:

(這是測試用的,所以在未達 85% 也會寄發)
接下來就是到「效能監視器」的「警示」來進行「新增警示設定」的動作。
輸入一個警示名稱,本例為「DriveCAlarm」。
在「註解」欄位寫入說明,再按下「新增」鈕。
依照下列的圖示進行「計數器」項目的挑選,完成後按下「新增」鈕,再按「關閉」鈕。
計數器欄位已經設定好了,接下來就是設定觸發條件。我對C磁區的設定為可用空間小於 5.86GB 就發出告警,1天檢查1次。
切換到「執行」頁籤,除了預設勾選的第一項外,再勾選「執行以下程式」。按下「瀏覽」鈕,挑選要執行的 Batch 檔,本例為:C:\sch\DriveCAlarm.bat
按下「套用」鈕後,會出現「設定密碼」的視窗,使用者欄位格式要為「Domain\User」。
確認一下「執行身分」是為之前所設定的「Domain\User」。
這樣子「警示」功能就多了一項設定。如果未啟動,就將它啟動。有問題的話,就到事件檢視器看看訊息。
為了要測試設定是否可運作?所以進行了下列的修改來確認。
在事件檢視器中,就可以看到類似下面的事件訊息。當然也會收到 E-Mail
大功告成,記得要將觸發條件改回來,要不然信就收不完啦!DE 磁區的設定也依樣畫葫蘆即可。


[Tip]使用效能監視器監控伺服器狀態,並寄送異常通知

  
當我們在管理伺服器時,常常會希望能監控伺服器狀態, 如果伺服器發生CPU持續過高,或是硬碟空間不足時能夠發出異常通知, 我們可以透過Windows內建的效能監視器來簡單實現這樣的功能。

設定效能監視器異常界線

在這邊以監控硬碟有效空間來做為範例。
  1. 首先打開我們的效能監視器,新增資料收集器集合工具
  2. 輸入名稱"伺服器效能監控",選擇使用手動建立
  3. 選擇"效能監視器警訊
  4. 我們可以看到已經出現了這個資料收集器集合
  5. 接下來開始新增我們的監控項目,新增一個資料收集器
  6. 輸入硬碟空間監控,並選擇效能計數器警訊
  7. 點選新增,選擇監控項目
  8. 選擇Logical Disk的% Free space,新增要監控的磁區
  9. 設定在20%以下時觸發警訊
  10. 勾選設定警訊內容

設定效能監視器警訊執行動作

  1. 設定抽樣間格為2分鐘 (普通應該一小時即可)
  2. 點選警訊動作頁籤,勾選在事件紀錄檔紀錄
  3. 設定發生異常時執行排程及通知訊息,點選右邊小箭頭可以瀏覽可使用的變數

設定發送警訊通知排程

  1. 進入工作排程器,新增排程工作
  2. 設定執行帳號,選擇不論是否登入都執行
  3. 點選動作頁籤,選擇新增
    當伺服器發生異常時,
  4. 輸入寄送email的資訊,$(Arg0)會自動帶入從效能監視器剛剛設定的警訊文字
  5. 設定完成,當伺服器異常時就會收到通知的Email了
※註: 使用內建的傳送電子郵件,僅支援不需驗證的SMTP Server, 此處可自由置換其他的警訊通知方法 (ex.Powershell) 只要記住可使用$(Arg0)來讀取帶入的警訊內容即可

總結


透過效能監視器,我們可以根據自己的需要實現簡單的伺服器監控機制, 當然隨著伺服器數量越來越多,可以按照需求使用更好的解決方案, 這邊提供大家一個簡單且輕量級的伺服器監控方法, 關於今天的內容,若有任何問題歡迎大家提出來一起討論!

參考資料
http://olivetjournal.com/my-honest-copy-com-review#proreviewhttp://forums.codeguru.com/showthread.php?439916-How-to-run-a-very-long-SQL-statementhttp://social.technet.microsoft.com/Forums/scriptcenter/en-US/37c5fced-38e4-4508-82a3-7e59cafce46f/how-to-use-vbscript-to-check-telnet-result?forum=ITCG

http://blog.xuite.net/jyoutw/xtech/62155589
http://www.dotblogs.com.tw/kirkchen/archive/2012/11/24/use-performance-monitor-to-monitor-server-and-send-alert-email.aspx


Xpath, xml 轉成http並且擷取需要的nodes, unicode編碼問題,vba轉vbs, bat讀入變數,篩選及比對

使用Xpath抓取xml內的資料

Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
            objXMLHTTP.Open "Get", "xml網址", False
                objXMLHTTP.setRequestHeader "Content-Type", "text/xml"
                  objXMLHTTP.setRequestHeader "charset", "UTF-8"
                    objXMLHTTP.Send

        testXML = objXMLHTTP.responseText

        Set objXMLHTTP = Nothing

        接著要注意的是XPath 2.0要加上第二行否則有些語法不能使用

        Set xmldoc = CreateObject("MSXML2.DOMDocument")
                xmldoc.setProperty "SelectionLanguage", "XPath"
                
                xmldoc.LoadXML (testXML)

        Count node數量

        Set Root = xmldoc.DocumentElement.SelectSingleNode("篩選條件")
                  
                 xmlcount = xmldoc.DocumentElement.SelectNodes("篩選條件").Length

        接著篩選的方法
        格式是路徑//channel/item/title[(contains(.,'條件')],左式是指篩選符合條件的title層級,須注意路徑要依照xml內的格式填寫,並且可以使用and和or

        如果需要新增或開啟檔案需要呼叫檔案系統物件

        Set objfso = CreateObject("Scripting.FileSystemObject")

        我在寫的時候遇到亂碼的問題,因此調用ADODB.Stream

        Set objtextfile = CreateObject("ADODB.Stream")

                        objtextfile.Charset = "utf-8"
                        objtextfile.Open
                        objtextfile.LoadFromFile ("檔案路徑")

        寫入檔案時使用

        objtextfile.writetext xmldoc.DocumentElement.SelectNodes("篩選條件").Item(i).text + Chr(13) + Chr(10)

        + Chr(13) + Chr(10)是換行的意思

        另外如果要比對字串

        Set d = CreateObject("Scripting.Dictionary")

        Do Until objtextfile.EOS
                        arr = objtextfile.ReadText(-2)
                        d.Add arr, arr
                        Loop

        EOS是讀到檔案結尾,然後將objtextfile的內容寫入Dictionary d內
        最後如果比對Dictionary跟檔案有不同的時候寫入檔案

        If Not d.Exists(Cel) Then
        fs.writetext
        end if

        bat讀入每行變數,使用%%a

        for /f %%a in (檔案.txt) do (動作,將輸入變數用%%a替代)

        參考資料
        Xpath, xml 轉成http並且擷取需要的nodes
        http://www.blueshop.com.tw/board/FUM200410061525290EW/BRD2011041810155910R.html
        http://bytes.com/topic/xml/answers/895475-how-do-i-get-value
        http://bbs.csdn.net/topics/360192227
        http://www.w3school.com.cn/xml/xml_to_html.asp
        http://michaelbaidotnet.blogspot.tw/2012/05/xpathxml-xmldocument-xmlnode-xml.html
        http://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing
        http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more
        http://www.pin5i.com/showtopic-23231.html
        http://zhidao.baidu.com/question/494955221.html
        http://web2.tcssh.tc.edu.tw/teacher/cjwu/vb/newvb6/new0g05.html
        http://msdn.microsoft.com/zh-tw/library/1k5e45de(v=vs.90).aspx
        http://stackoverflow.com/questions/7986679/classic-asp-not-recognizing-xpath-contains-function
        http://stackoverflow.com/questions/17049535/vbscript-taking-xml-node-values-and-assigning-to-variables
        http://stackoverflow.com/questions/14469414/xpath-selecting-same-level-node-with-different-name
        http://stackoverflow.com/questions/3139402/how-to-select-following-sibling-xml-tag-using-xpath
        unicode編碼問題
        http://www.blueshop.com.tw/board/show.asp?subcde=BRD20110626172457CS9&fumcde=FUM200501271723350KG&odr=cdt&odrtyp=0
        http://www.dotblogs.com.tw/rainmaker/archive/2011/05/17/25261.aspx
        http://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba
        http://tw.knowledge.yahoo.com/question/question?qid=1607092206104
        vba轉vbs
        http://www.asjh.tn.edu.tw/xupload/learning/vbscript/vbs16.htm
        http://www.visualbasicscript.com/Vbscript-to-read-txt-file-for-input-m31649.aspx
        http://www.itworld.com/nl/win_this_wk/04282003
        http://www.visualbasicscript.com/m57228-print.aspx
        http://mikenation.net/files/backupprintservers.txt
        bat讀入變數
        http://www.dotblogs.com.tw/chhuang/archive/2012/06/09/72702.aspx
        rss feed在chrome顯示亂碼
        http://blog.hsdn.net/1388.html
        讀入字串亂碼使用ADODB.Stream解決
        http://baike.baidu.com/view/1806836.htm
        http://www.lotusfans.com/forum/archive/index.php/t-43645.html
        http://stackoverflow.com/questions/15151128/read-large-file-line-by-line-with-ado-stream
        http://stackoverflow.com/questions/13851473/read-utf-8-text-file-in-vbscript

        2013年11月22日 星期五

        自己架設郵件伺服器(mail server)-hmailserver

        前陣子自己架設了一個郵件伺服器,使用的軟體是hmailserver,其實架設相當容易也沒有甚麼需要注意的,不過在新增郵件帳號時想說有無方法可以批次新增,於是找了一下發現可以使用vbs配合cvs檔處理,請參閱參考資料。

        DKIM signature設定
        http://www.port25.com/support/domainkeysdkim-wizard/

        DKIM test
        http://www.appmaildev.com/en/dkim/
        http://www.dnswatch.info/dns/dnslookup?la=en&host=_domainkey.copy.kcshao.com&type=TXT&submit=Resolve

        SPF record
        http://blog.xuite.net/tolarku/blog/233356505-DNS+%E8%A8%AD%E5%AE%9A+spf+%E8%A8%98%E9%8C%84+-+Sender+Policy+Framework

        SPF test
        http://mxtoolbox.com/spf.aspx

        需要將hmailserver的host name指向DNS反解的名稱,設定位置在
        settings-protocol-SMTP下的delivery of email,host name欄位
        Open hMailServer Administrator. 
        Go to: Settings | Protocols | SMTP. 
        Click on the "Delivery of e-mail" tab. 
        Fill-in the "hostname" field.

        可使用mxtoolbox.com來測試
        http://mxtoolbox.com/SuperTool.aspx
        或是
        https://www.mail-tester.com/

        參考資料
        http://blog.xuite.net/sendohshih/blog/41958216
        http://digitower.wordpress.com/2009/11/27/hmailserver%E6%87%B6%E4%BA%BA%E7%B6%B2%E8%B7%AF%E5%B7%A5%E7%A8%8B%E5%B8%AB%E9%83%B5%E4%BB%B6%E4%BC%BA%E6%9C%8D%E5%99%A8/
        http://www.net-chinese.com.tw/script/nc_diy3-0.asp
        http://www.clicksun.com.cn/mis/bbs/showbbs.asp?bd=5&id=951&totable=1
        http://www.hmailserver.com/forum/viewtopic.php?f=9&t=2206
        http://www.hmailserver.com/documentation/latest/?page=com_example_account_create
        http://www.hmailserver.com/forum/viewtopic.php?f=9&t=23128
        hmailserver host name
        https://www.hmailserver.com/forum/viewtopic.php?t=10422
        https://www.hmailserver.com/documentation/latest/?page=reference_protocolsmtp

        2013年10月15日 星期二

        Sendemail.exe與VBS與中華電信小烏龜與splashtop與wake on lan(internet)

        前幾天花了點時間把server整理了一下。一開始是想讓server開機時可以自動傳送email告知開機成功,以及休眠時可以回傳IP位址以達成wake on lan避免休眠時臨時需要連線之窘境。於是著手研究,傳送email本來打算用工作排程器內建功能,後來發現常常失敗,搜尋後得知內建為非常陽春的功能,甚至照網頁所說outlook須先設定電子郵件帳號我卻沒有,所以能夠傳成功很詭異。後來改用sendemail.exe小程式,有很冗長的參數,不過也一一解決了。最後是自動回傳ip部分,參考網路寫了個vbs小程式,然後配合sendemail.exe傳送附件。

        至此算是告一個段落,沒想到後來wake on lan的設定才是噩夢的開始。因為我是用虛擬router,所以並沒有另外接一個路由器而是直接接到中華電信小烏龜,並且也是用固定ip,與小烏龜的浮動ip不同。這是因為當初小烏龜內無法設定固定ip撥接,設定的話wifi會無法使用。於是我繞了路使用虛擬路由器monowall,也就是說網路線雖是接到server,但是實際上是透過虛擬路由才能夠連線,否則會是斷線狀態,算是增加安全性,這是前話。

        不過這次搜尋了一下找到了中華電信小烏龜的密碼,登入後可以更改設定,發現要達成wake on internet因為小烏龜功能陽春必須每次重開後寫入arp table,加上如果照原本設想休眠後透過wol啟動也太過狹隘,如果是跳電等路由取得新浮動ip就沒用了。因此就整個大改,把小烏龜改成固定ip連線,arp table則透過遠端控制連入更改。至此以為問題結束,卻發現回到家無法遠端控制且原本設定無誤的splashtop也無法使用。因此又開始了debug之路。

        最後結論是,McAfee不知為何把連接埠封鎖,把連接埠打開即可(記得6783,6784,6785皆要開啟,我本來只開啟6783,後來一團混亂)。另外關於回到家無法遠端連入小烏龜問題則在於Access Control下還有一個ip Addresses,必須輸入可以通過的ip才行。至此終於結束。

        後來想一想直接買一台有支援wol的router好像最省事,算了,就當學習吧。

        另外如果要達成wake on lan(internet)因為大部分路由器會檔broadcast封包,所以除非路由器有相關設定,否則需要設定NAT之後,使用參考資料最下方網址發送mask為255.255.255.255之magic packet,不然會啟動失敗。

        sendemail格式

        SENDEMAIL -s mail-server -t 收件人 -f 寄件人   -u "主旨"  -o message-file="本文檔" -a "附加檔" -o message-charset=big5 -o message-header="Disposition-Notification-To:<回信收件人>"

        參考資料
        http://alycesrestaurant.com/sendemailexe.htm
        http://zonble.net/archives/2006_07/867.php
        http://ithelp.ithome.com.tw/question/10046673
        http://www.pczone.com.tw/thread-155562.html
        http://www.depicus.com/wake-on-lan/woli.aspx
        http://tw.knowledge.yahoo.com/question/question?qid=1008100609144
        http://www.dotblogs.com.tw/rachen/archive/2010/03/31/14328.aspx