2015年2月3日 星期二

selenium download file, php

Download File [Arguments] ${COOKIE} ${URL} ${FILENAME} ${COOKIE_VALUE} = Call Selenium API get_cookie_by_name ${COOKIE} Run and Return RC wget --cookies=on --header "Cookie: ${COOKIE}=${COOKIE_VALUE}" -O ${OUTPUT_DIR}${/}${FILENAME} ${URL}

php POST變數.$_POST["item"]後方如果要接字必須以 "," or "." 隔開,否則會出錯

fopen file時在linux下會有權限問題,要注意需給予使用者read權限否則會無法讀取。

使用fgets取得txt資料時,會帶出行尾的特殊符號,以windows為例為\r\n。因此在做比對時,不論是使用==或strcmp()函數都需要先將這些特殊符號消去(或是在輸入端增加),才能成功比對。

我是使用ereg_replace("\r\n","",變數)來做。供參考。順帶一提,如果將\0消去則整個字串會消失。

想要驗證則可以使用strlen()函數來觀察字串的長度,就可以知道其實有特殊符號的存在。

最後apache的預設使用者是www-data(可去/etc/apache2/envvar修改),如果要使用fopen讀取檔案記得將www-data加到owner群組內,virtualbox shared folder共用資料夾則是要將www-data加到vboxsf群組內才可正常讀取。

#sudo gpasswd -a www-data vboxsf

#sudo usermod -G vboxsf www-data 有一樣效果

以下code可以查看group內成員
$ sudo grep ^群組名稱: /etc/group

過程中由於出錯使得apache內的log資料量暴增,一開始不知道只覺得硬碟空間怎麼消失這麼快,後來使用指令列出資料夾大小才找出來。max-depth是指只顯示一層,-B M是以MB表示,sort -g是照大小排列。

du -B M --max-depth=1 | sort -g

參考資料
自己經驗
https://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-impossible/
http://php.net/manual/en/function.shell-exec.php
http://stackoverflow.com/questions/2404794/strcmp-on-a-line-read-with-fgets
將ubuntu內資料夾所佔空間列出
http://banco.pixnet.net/blog/post/21967119-%5Bubuntu%5D-%E5%88%A9%E7%94%A8du%E6%90%AD%E9%85%8Dsort%E4%BE%86%E6%9F%A5%E7%9C%8B%E7%A3%81%E7%A2%9F%E4%BD%BF%E7%94%A8%E7%A9%BA%E9%96%93
fget
http://qaautomationworld.blogspot.in/2014/02/file-downlaoding-using-selenium.html
http://stackoverflow.com/questions/7618155/how-to-duplicate-a-request-using-wget-or-curl-with-raw-headers
https://addons.mozilla.org/zh-TW/firefox/addon/export-cookies/
http://stackoverflow.com/questions/4272770/wget-with-authentication

google apps script

Google apps sript 使用for loop處理簡單的寫入時會很慢,不像excel運算的速度。因此需要簡化程式,以參考資料為例即為將每格寫入轉換成一次寫入,速度會提升很多。

另外矩陣變數則是要用"[["開頭,"]]"結尾,另外如果需要用迴圈加入單元,可以用.push(),記得變數要用var 變數 = [];。google apps script的for是小寫,我試過用大寫會無法辨識,需要注意。

在尋找字串裡的"("時,由於"("是特殊字元,必須escape,而search本身也是regular express所以要再escape一次,故應寫成search("\\(")。此時的錯誤訊息會是SyntaxError: unterminated parenthetical。

同理在取代字串的時候也會遇到regular express的問題,因為是regexp所以只會取代找到的第一個,解決方法是直接使用regexp寫法,如replace(/,/g)可取代字串中所有","。

function的部分就是直接呼叫,如HelloWorld();

replace在spreadsheet內使用的話要寫.toString().replace()才能用。還有length在計算array內字元我遇過會出錯,記得最後再加上[]。

Invalid assignment left-hand side. 我遇到的狀況是comparison的問題,就是code裡該使用"=="的地方(判斷式)用成"="(變數assign)就會出現這個error。而且這個error的debug會抓錯地方一直顯示第一行錯,所以第一次遇到會不知道發生甚麼事,連存檔都無法。

另外還有遇到一個狀況是在使用importrange公式,跑完script後會無法正常update數值,查了一下好像也有人遇到同樣情形,在google修好之前只好先用script頂著先。現象是都不會更新,將該格公式更動,或刪除復原就可以正常更新。

參考資料
基礎
https://developers.google.com/apps-script/guides/bound#creating_a_bound_script
簡化程式
http://stackoverflow.com/questions/19120211/google-script-performance-slow-down
加入公式
http://stackoverflow.com/questions/12036726/how-do-i-add-formulas-to-google-spreadsheet-using-google-apps-script
Matrix
http://stackoverflow.com/questions/15783169/convert-string-to-matrix-array
search "("
http://stackoverflow.com/questions/3977351/javascript-search-fails-to-find
replace all
http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
javaScript運算子
http://taiwantc.com/js/js_tut_a4.htm
javaScript字串運算
http://miisoo.blogspot.tw/2008/01/javascript-string-operations.html
Invalid assignment left-hand side.
http://stackoverflow.com/questions/11888923/what-could-cause-invalid-assignment-left-hand-side-error-message-that-doesnt
importrange issue
http://stackoverflow.com/questions/25970185/importrange-function-not-refreshing-new-entries-in-linked-google-sheet
http://stackoverflow.com/questions/22587418/new-google-spreadsheet-formula-results-different-than-what-i-see-importrange-fu?rq=1

pfsense remote syslog and email notification

使用pfsense時,如果不想一直開著瀏覽器盯著,卻又想監控事件時該怎麼辦呢?

我們可以利用兩種方法,第一種是設定pfsense email alert,在system-advanced-notifications,設定smtp mail,設定完後按test smtp看是否設定成功,這個會在gateway down的時候email通知。

第二種則是使用status-system log-settings,設定remote syslog server,將log傳到另一台server上,就可以把log以文字檔形式儲存,再搭配之前提過的工作排程器或crontab,就可以將log發送到手機來監控囉。

以ubuntu為例,init.d下的rsyslog即為syslog server,pfsense傳過來的log則會存在/var/log/syslog下。記得要service rsyslog restart或重開機才會生效。

grep -Fxvf a b 可以秀出b有a沒有的字串

使用if -n -z可以判斷是否為空字串
範例
VAR="hello"
if [ -n "$VAR" ]; then
    echo "VAR is not empty"
fi

VAR=""
if [ -z "$VAR" ]; then
    echo "VAR is empty"
fi

要將結果傳至變數則要加上$()
範例
var="$(grep -Fxvf a b)"

bash regular expression的寫法範例
re="^([^-]+)-(.*)$"
[[ "ABCDE-123456" =~ $re ]] && var1="${BASH_REMATCH[1]}" && var2="${BASH_REMATCH[2]}"
echo $var1
echo $var2
這邊的${BASH_REMATCH[0]為全部[1]為第一個sub patteren以此類推
另外下次再做regxp舊的就會被洗掉,所以如果要留記得存變數
=~ 為比較regxp的operator
!~則是反義

ubuntu下寫bash時出現[[: not found的error message
找了老半天原來是ubuntu的問題

In Ubuntu, /bin/sh points to /bin/dash, not /bin/bash as is in Debian.


To change this, either update your script

Code:

#!/bin/bash
另外執行時前面加bash也有同樣效果ex. bash a.sh



參考資料
http://ubuntuforums.org/showthread.php?t=2200196
https://doc.pfsense.org/index.php/Advanced_Setup_(2.0)
https://www.safaribooksonline.com/library/view/pfsense-2-cookbook/9781849514866/apas04.html
比較檔案
http://stackoverflow.com/questions/14500787/comparing-two-files-in-linux-terminal
timmurphy.org/2010/05/19/checking-for-empty-string-in-bash/
http://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command
sendEmail
http://ubuntuforums.org/showthread.php?t=1127478
Bash regular expression
http://stackoverflow.com/questions/18709962/bash-regex-if-statement
http://stackoverflow.com/questions/10520623/how-to-split-one-string-into-multiple-variables-in-bash-shell
http://stackoverflow.com/questions/10486575/bash-how-to-take-a-number-from-string-regular-expression-maybe
http://stackoverflow.com/questions/19737675/shell-script-how-to-extract-string-using-regular-expressions
關於escape很清楚的一篇解釋
http://stackoverflow.com/questions/18709962/bash-regex-if-statement
ubuntu sh to bash
http://ubuntuforums.org/showthread.php?t=1159137
時間變數變成英文
http://askubuntu.com/questions/265753/how-to-change-date-time-format-to-english
時間變數
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

virtualbox vdi verr_not_supported

前幾天VM突然打不開,現象是媒體管理員顯示黃色驚嘆號,重新掛載也無法,錯誤訊息verr_not_supported,進階訊息是VBOX_E_IPRT_ERROR。本來查到是vdi內容版本與virtualbox不符,可是前一天還可用怎麼會突然就不行,後來找到vboxmanage command "VBoxManage internalcommands repairhd --format VHD --filename <image>"試著修復vdi結果居然顯示invalid header,只好再繼續找解法。既然是header出錯就想到應該是整個vdi檔損毀,但使用chkdsk修復硬碟卻也顯示硬碟沒問題,本來覺得大概無解了,沒想到找到一篇說把0000到01D0用正常的vdi檔頭覆蓋就可以,死馬當活馬醫之下還真的解決了!寫在這邊供參考。

使用的軟體是 HxD Hex Editor

參考資料
https://forums.virtualbox.org/viewtopic.php?p=29267#p29267
https://forums.virtualbox.org/viewtopic.php?f=6&t=46711
https://forums.virtualbox.org/viewtopic.php?f=6&t=44654#p201104
vboxmanage command
https://www.virtualbox.org/ticket/10785

virtualbox 增加硬碟空間

因為原來的ubuntu硬碟空間設定太小,導致現在空間滿了,需要增加硬碟空間。search了一下發現我設的固定硬碟大小無法增加空間,不過後來發現那是中文網頁的資訊,事實上是可以轉換的。

先在virtualbox安裝目錄下使用
vboxmanage.exe clonehd \要調整的硬碟路徑.vdi \複製到的路徑.vdi

複製完後新硬碟vdi就會從固定大小轉換為動態大小,就可以使用指令改變大小

vboxmanage.exe modifyhd \要調整的硬碟路徑.vdi --resize 51200

這邊的51200指的是MB

我試了參考資料2&3。發現磁區無法延伸,後來發現是要將sda2刪除(sda5是在sda2下面,也就是sda1與未配置區中間不可有別的磁區阻隔),刪除之後就可以延伸。供參考。

參考資料
http://superuser.com/questions/716649/how-to-change-fixed-size-vdi-with-modifyhd-command-in-windows
http://www.arthurtoday.com/2013/04/virtualbox-resize-partitions-of-ubuntu-guest-part1.html
http://www.arthurtoday.com/2013/04/virtualbox-resize-partitions-of-ubuntu-guest-part2.html

foldersync reupload issue

android上使用foldersync同步到FTP時發現會有重複同步的問題,看了一下是由於兩邊檔案時間不同,但是明明是同一個檔案。後來研究了一下似乎是foldersync的問題,沒有支援MFMT時間會錯亂。解決方法可以將容許時間調大,或者是乾脆就不要覆蓋舊的,只能等更新了。

參考資料
https://groups.google.com/forum/#!topic/tacitdynamics/GhfLILUGo2g

2014年12月17日 星期三

有關ATI Hydravision消失問題

最近重灌電腦,遇到ATI Hydravision消失問題,在三螢幕變得很麻煩,因為原本好用的Hydragrid不見了,找了一下找到資料說可以安裝這個然後只勾選Hydravision選項即可讓Hydravision選項在Catalyst裡面重新出現。給遇到一樣問題的大家參考。

參考資料
http://forums.amd.com/game/messageview.cfm?catid=454&threadid=173369