2014年1月13日 星期一

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

        sendemail.exe發送html格式郵件

        這是google的頁庫存檔,連結已無法存取,留個紀錄。

        本文主要讨论Python如何使用sendEmail发(垃圾)邮件?
             Linux用户常用sendmail发送电子邮件,当您看了本文后可能会改用sendEmail去发送邮件了,呵呵。

        1 下载sendEmail

               sendEmail有Linux和windows版本软件包,依据自己的平台选择下载好了
              登录上边的网址找到:
           

             Download

        Official Release: sendEmail-v1.56.tar.gz    (29kb Sep 29th, 2009)    Changelog    Screen Shot

        Windows Download:
        Free sendEmail.exe for Windows. To use simply run sendEmail.exe from a console / command line.
        sendEmail-v156-notls.zip   (677kb Sep 29th, 2009)   No TLS support 
        sendEmail-v156.zip   (1.4mb Sep 29th, 2009)   TLS supported 

        RPM Package: sendEmail rpm
        选择适合自己的下载好了

        2 安装sendEmail

          sendEmail不需要安装直接解压到某目录即可直接使用。
            (Linux用户) tar -zxvf sendEmail-x-y-z.tar.gz
         本文选择windows平台sendEmail软件包,下载后解压到c:\sendEmail目录下。


        3 sendEmail命令帮助

           sendEmail软件是命令行软件,需要在Dos(shell)下执行使用。

        [jeapedu]# sendEmail --help
        
        sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net>
        
        Synopsis:  sendEmail -f ADDRESS [options]
        
        Required:
        -f ADDRESS                from (sender) email address
        * At least one recipient required via -t, -cc, or -bcc
        * Message body required via -m, STDIN, or -o message-file=FILE
        
        Common:
        -t ADDRESS [ADDR ...]     to email address(es)
        -u SUBJECT                message subject
        -m MESSAGE                message body
        -s SERVER[:PORT]          smtp mail relay, default is localhost:25
        
        Optional:
        -a   FILE [FILE ...]      file attachment(s)
        -cc  ADDRESS [ADDR ...]   cc  email address(es)
        -bcc ADDRESS [ADDR ...]   bcc email address(es)
        -xu  USERNAME             username for SMTP authentication
        -xp  PASSWORD             password for SMTP authentication
        
        Paranormal:
        -b BINDADDR[:PORT]        local host bind address
        -l LOGFILE                log to the specified file
        -v                        verbosity, use multiple times for greater effect
        -q                        be quiet (i.e. no STDOUT output)
        -o NAME=VALUE             advanced options, for details try: --help misc
        -o message-content-type=<auto|text|html>
        -o message-file=FILE         -o message-format=raw
        -o message-header=HEADER     -o message-charset=CHARSET
        -o reply-to=ADDRESS          -o timeout=SECONDS
        -o username=USERNAME         -o password=PASSWORD
        -o tls=<auto|yes|no>         -o fqdn=FQDN
        
        Help:
        --help                    the helpful overview you're reading now
        --help addressing         explain addressing and related options
        --help message            explain message body input and related options
        --help networking         explain -s, -b, etc
        --help output             explain logging and other output options
        --help misc               explain -o options, TLS, SMTP auth, and more

              测试用例

             我(sender@163.com)要给他/她(receiver@sina.com)发电子邮件,邮件的主题是"subjectTitle",邮件的内容是说句“hello”,带了附件attach.txt文件,我邮箱sender@163.com的密码是“123456”,则使用sendEmail的命令如下:
         sendEmail -f sender@163.com -t receiver@sina.com -s smtp.163.com -xu sender@163.com -xp 123456 -u subjectTitle -m hello -a attach.txt
                  注:本示例在windows平台下测试,需手动使用dos即运行->cmd->cd c:/sendEmail目录(假设下载文件解压到本目录)

           -f    后指定在邮箱里显示谁发的邮件
           -t    后指定发给谁
           -s   指定发送smtp服务器,本例用163的smtp服务器
           -xu 后需指定smtp服务器上的授权帐号(sender@163.com),
                 实际上就是用参数xu后边的邮箱来真正发送邮件
           -xp 后则是smtp服务器上的授权帐号(sender@163.com)所对应的密码,
                 smtp服务器要检查合法性
           -u   邮件主题
           -m  邮件正文内容
           -a   邮件携带附件(attach.txt)
              各位网友在自我测试时,需对应替换。比如想用“se@163.com(密码abcdef)”发邮件给"re@sina.com",邮件主题“hello”,邮件内容"world",携带附件"b.txt",我来替换一下如下:
         sendEmail -f se@163.com -t re@sina.com -s smtp.163 -xu se@163.com -xp abcdef -u hello -m world -a b.txt



        4 编写发送邮件程序

            写个Python程序来用用吧:发几百封邮件给他/她/Ta.程序名sm.py,请保存在sendEmail.exe同一目录下。
        import os
        
        from = "sender@163.com"
        to = "reveiver@sina.com"
        subject = "say hello"
        msg = "I like you"
        attachfile = "a.txt"
        c = 0
        while c < 498:
         os.system("sendEmail.exe -f "+from+" -t "+to+" -xu "+from+" -xp 123456 -u "+subject+" -s smtp.163.com -a "+attachfile+" -m "+msg)
         c = c + 1
            据说163邮件一天最多发500封信,之后。。。呵呵,何时解封时间不确定,我看可以多申请几个163邮箱。
            在sendEmail目录下执行python sm.py即可发499封邮件给他/她/Ta.

        5. 怎样使发送邮件内容为Html的呢?

            首先在sm.py文件所在目录下创建一个html文件,例如a.html,代码如下:

        <html>
        <body>
        <B>测试网页</B><br>
        <a href= "http://www.csdn.net"><font color = 'blue' size = '7'><u>CSDN就是牛!</u></font></a>
        </body>
        </html>

            接着,需要在sendEmail命令行里增加几个可以发送html文本作为邮件正文内容的参数选项。

             -o message-content-type=html 
             -o message-charset=CHARSET 
             -o message-header=HEADER  
             -o message-file=afile
        
             -o message-content-type = html 
                         告诉邮件服务器发送的是html格式邮件内容
             -o message-charset = CHARSET 
                        这里不能用utf-8否则邮件内容显示乱码
             -o message-file=某文件  
                        是用来指定携带的那个html网页文件作为邮件正文, 这样邮件正文里就可以有超级链接了。

             最后运行测试。


        參考資料

        es檔案瀏覽器無法開啟網路芳鄰問題

        發現es檔案瀏覽器突然無法開啟本來可以開啟的區網上的電腦,研究了一下發現是ipv6的問題,因為之前把路由器設定了v4 to v6 tunnel的功能,可能因此造成android無法map v4 ip。將ipv6關閉就回復正常。

        參考資料
        自己測試結果

        ubuntu virtualbox 共用資料夾 plexWatch, splashtop streamer, xrdp on ubuntu 13.10

        接續上篇使用ubuntu設定plexwatch web時也遇到一些問題,整理如下。

        • virtualbox共用資料夾位置在/media
          • 需要先將使用者加入群組
          • virtualbox預設為vboxsf
          • 終端機輸入指令sudo gpasswd -a 使用者帳號 vboxsf
        • 在設定crontab工作時不要輸入使用者名稱
        • twitter則需填入consumer_key, consumer_secret, access_token, access_token_secret四項,並且create my access token
        • ubuntu軟體中心位置 /usr/bin/software-center
        • remotebox不知為何無法成功使用,使用phpvirtualbox取代
        • virtualbox如果要使用遠端控制(非windows內建的遠端桌面)則需安裝extension pack
        • xrdp如果遇到灰色畫面無法進入則輸入以下指令,原因是gnome更動了設定
          • sudo apt-get install xrdp
          • sudo add-apt-repository ppa:xubuntu-dev/xfce-4.10
          • sudo apt-get update
          • sudo apt-get install xfce4
          • echo xfce4-session >~/.xsession  
        • ubuntu上使用splashtop streamer需要輸入以下指令否則目前無法使用於13.10(官方尚未支援)
          • sudo apt-get install aptitude
            sudo aptitude update
            sudo aptitude safe-upgrade
          • python /opt/splashtop-streamer/SRStreamer.pyc
          • sudo aptitude install python-appindicator

        參考資料
        共用資料夾權限
        http://ubuntuforums.org/showthread.php?t=1921707
        http://www.mobile01.com/topicdetail.php?f=300&t=3122670
        plexWatch on ubuntu
        http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
        https://github.com/ljunkie/plexWatch/blob/master/README.md
        crontab
        sudo gedit /etc/cron.d/anacron
        http://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
        xrdp 灰畫面
        http://c-nergy.be/blog/?p=4448
        http://askubuntu.com/questions/266271/xubuntu-12-04-desktop-grey-with-no-icons
        plexWatch email notification
        https://forums.plex.tv/index.php/topic/72552-plexwatch-plex-notify-script-send-push-alerts-on-new-sessions-and-stopped/
        ubuntu splashtop streamer on 13.10
        https://support-splashtoppersonal.splashtop.com/entries/30097576-Splashtop-Streamer-on-Ubuntu-13-10
        http://qiita.com/nyarla/items/4202ddb8e29e2d0ddd65
        http://timothyandrewbarber.blogspot.tw/2011/02/finding-config-folder-in-ubuntu.html
        RemoteBox
        http://knobgoblin.org.uk/downloads.cgi
        http://www.openfoundry.org/tw/foss-programs/8319--virtualbox-remotebox
        http://www.ibm.com/developerworks/cn/linux/l-cn-remotebox/
        http://www.openfoundry.org/tw/foss-programs/8319--virtualbox-remotebox
        phpvirtualbox
        http://www.arthurtoday.com/2011/11/windows-phpvirtualbox.html#.Usl3t_QW2tQ
        http://blog.segmentfault.com/jifei/1190000000346904
        vboxwebsrv.exe
        https://forums.virtualbox.org/viewtopic.php?f=6&t=38449
        http://nikolas.demiridis.gr/post/10280216759/how-to-run-vboxwebsrv-as-a-service-on-windows-2008
        virtualbox rdp extension pack
        http://www.virtualbox.org/manual/ch07.html#vbox-auth
        ubuntu切換2D
        http://www.mobileai.tw/2013/01/08/ubuntu-12-10-2d/
        PlexWatch ip address必須與PMS在同一台電腦上(localhost)才可正常抓取
        https://forums.plex.tv/index.php/topic/72552-plexwatch-plex-notify-script-send-push-alerts-on-new-sessions-and-stopped/page-21#entry483408


        The locale codeset (cp1252) isn't one that perl can decode, stopped at Encode/Locale.pm line 94.

        使用plexwatch web時出現標題的錯誤,查詢了一下發現是程式本身從linux轉換到windows時沒有轉換好,向作者反映他說沒時間處理,於是只好自力救濟。到控制台-地區及語言-系統管理-非unicode程式的語言-變更系統地區設定改成英文(美國)可以解決問題,但是會產生後遺症就是其他程式可能出現亂碼無法閱讀。因此最後我的解決方式是再建立一個VM-ubuntu來使用。

        參考資料
        http://forge.fusioninventory.org/issues/2197
        http://contest-start.blogspot.tw/2009/12/apache-22-php531-mysql.html
        http://www.luar.com.hk/flashbook/archives/001425.php
        http://www.perlmonks.org/?node_id=981095
        http://www.php5.idv.tw/html.php?mod=article&do=show&shid=39
        http://blog.linym.net/archives/264
        http://idaiwan.pixnet.net/blog/post/28156513-apache-%E5%8A%A0-php-%E5%AE%89%E8%A3%9D%E9%85%8D%E7%BD%AE%E6%95%99%E5%AD%B8%EF%BC%88windows%EF%BC%89
        http://forums.adobe.com/message/2869206
        http://www.luar.com.hk/flashbook/archives/001425.php
        http://www.digglife.cn/articles/pp-the-locale-codeset-is-not-one-that-perl-can-decode.html

        Gmail信件被截斷

        使用sendemail.exe時發現寄出的mail被截斷,研究了一陣子,使用別的email地址卻沒事,所以應該是gmail的問題,大於16KB就會被截斷。後來嘗試了blat(不支援SSL),然後最後使用mailsend這支小程式,成功解決問題。我想問題大概是sendemail這支程式被判定限制?或者是跟使用base64有關。

        參考資料
        http://productforums.google.com/forum/#!topic/gmail-zh-tw/ViedeYzoF8A
        http://productforums.google.com/forum/#!topic/gmail/3dZE6kjwYpA
        http://ihower.tw/blog/archives/3481
        gmail大量寄件者指南
        https://support.google.com/a/answer/81126?hl=zh-Hant
        https://support.google.com/mail/answer/180707
        https://support.google.com/mail/answer/2451690
        jave.replace
        http://repeat.tw/blog/post/25800355-javascript-.replace()-%E7%9A%84%E7%AD%86%E8%A8%98
        測試垃圾郵件程度
        http://www.brandonchecketts.com/emailtest.php
        mailsend
        https://code.google.com/p/mailsend/wiki/ChangeLog
        http://community.spiceworks.com/topic/265337-how-to-send-email-from-command-line-in-windows-7
        base64decode
        http://www.convertstring.com/zh_TW/EncodeDecode/Base64Decode
        phpmailer
        http://blog.jsdan.com/php-%E4%BD%BF%E7%94%A8phpmailer%E9%80%8F%E9%81%8Egmail%E5%AF%84%E4%BF%A1/

        2013年12月28日 星期六

        pfsense抓不到IP問題

        pfsense 有時候會抓不到IP,需要重開且碰運氣,以下是我找到並測試的解決方法

        先到Diagnostics > Command輸入
        ifconfig -m em0
        應該會看到類似像這樣
        : ifconfig -m em0
        em0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
               [...]
               supported media:
                       media autoselect (1000baseT <full-duplex>)
                       media 1000baseT
                       media 1000baseT mediaopt full-duplex
                       media 100baseTX mediaopt full-duplex
                       media 100baseTX
                       media 10baseT/UTP mediaopt full-duplex
                       media 10baseT/UTP
        然後將media設成可以連線時autoselect的那個(就是( )裡的),以上面為例輸入
        ifconfig em0 media 1000baseT mediaopt full-duplex
        就不會再抓不到IP了

        參考資料
        http://community.spiceworks.com/topic/257580-no-internet-connectivity-pfsense-needs-reboot-to-have-internet-connection
        https://doc.pfsense.org/index.php/Forcing_Interface_Speed_or_Duplex_Settings