2015年2月3日 星期二

linux架站

當http://localhost/phpmyadmin出現404 not found時
#cd /var/www
#ln -s /usr/share/phpmyadmin

mysql new account

  • select
  • insert
  • update
  • create
  • alter
  • index
  • drop
  • reference
  • lock tables
drupal theme 教學
https://www.youtube.com/watch?v=6ekJOn2ocRE
https://drupal.org/node/83501

30個好用的themes
http://drupalchina.cn/node/1597

更改資料夾擁有者
sudo chown -R www-data:www-data /var/www/drupal
https://drupal.org/node/1036494

安裝smtp module讓mail功能正常
https://drupal.org/project/smtp
https://drupal.org/node/1108514


參考資料
https://drupal.org/project/drupal
https://drupal.org/documentation/install/create-database
http://altinukshini.wordpress.com/2011/01/09/how-to-install-drupal-7-on-linux/
http://shaurong.blogspot.tw/2014/01/drupal-726-centos-65-x64.html
https://www.youtube.com/watch?v=WBFqlgt7kuI
http://linadonis.pixnet.net/blog/post/27585552-ubuntu-server-%E5%AE%89%E8%A3%9D-phpmyadmin

Robot Framework

Regular Expression
在使用 Read Until Regexp 時,卡在$及 . 很久,研究的結果是,由於使用或 ( | ) 所以要轉義兩次,第一次轉義後由於仍在 ( | ) 內所以仍被當成regular expression看,故要再轉義一次。

所以最後結果是 (\\$|\\.txt)

在使用SSHLibrary抓取terminal內字串時遇到換行符號,本來以為是\n,結果試了不是,又試了轉義以及\r都不是,最後靈機一動,居然是\r\n,記錄在這邊,希望能夠幫到人。

SSHLibrary Open Connection
與Telnet不同,ssh open connection 必須同時使用open connection及login兩個keyword,原因應該是因為ssh連接時是使用username@ip。robot因考慮Telnet使用同一組Keyword所以造成此限制。另外AttributeError: 'NoneType'錯誤則是因為沒有輸入帳號密碼,也就是上面所說沒有配合使用login造成。

unbalanced parenthesis則是括號沒有配合到,我遇到的狀況是空了兩格造成robot將右括號視作另一個argument了

SSHLibrary.Read until regexp 我有遇到狀況是會卡住,即使timeout時間到也不會FAIL。後來使用Read交叉測試的結果是因為output內有不同的編碼所以decode時卡住。Read結果的錯誤訊息為
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 318: invalid start byte
解決方法是close connection等到該段output被跳過之後再重新建立連線,換言之就是跳過該段output。另外也有一可能是整個編碼錯誤,如開始英文後轉為中文。此時可於open connection指定編碼encoding=

參考資料
自己測試結果
Read until regexp卡住
http://stackoverflow.com/questions/6180521/unicodedecodeerror-utf8-codec-cant-decode-bytes-in-position-3-6-invalid-dat
http://www.robotframework.net/question/68

phpmailer

參考資料
http://belleaya.pixnet.net/blog/post/27410978-%5B%E6%95%99%E5%AD%B8%5D-php-%E5%88%A9%E7%94%A8-phpmailer-%E9%80%8F%E9%81%8E-gmail-%E5%AF%84%E4%BF%A1
http://blog.jsdan.com/php-%E4%BD%BF%E7%94%A8phpmailer%E9%80%8F%E9%81%8Egmail%E5%AF%84%E4%BF%A1/
http://who-know.com/use-phpmailer-to-send-emails-via-gmail-smtp/
while try catch自動分行讀取
http://kris-14.blogspot.tw/2009/12/php-txt.html
http://phpwrite.blogspot.tw/2010/05/php5try-catch.html
fopen 檔案路徑寫法
http://www.php.net/manual/en/function.fopen.php
輸入及呼叫
http://www.dami.tw/2013/11/11phpcode.html
http://www.php5.idv.tw/html.php?mod=article&do=show&shid=54
http://www.php5.idv.tw/html.php?mod=article&do=show&shid=68
http://stackoverflow.com/questions/15890627/phpmailer-body-with-variables
php重複定義
http://witmax.cn/php-cannot-redeclare-class.html
html註解
http://ant4html.blogspot.tw/2008/11/blog-post.html
php運行時間限制以及後台運行
ignore_user_abort(true);
set_time_limit(0);
http://www.ptt.cc/man/PHP/DEAA/M.1359052474.A.47F.html
http://www.blueshop.com.tw/board/FUM20041006152627A9N/BRD2012020312083365R.html
http://www.allenj.net/archives/1910
http://blog.wu-boy.com/2007/06/php-%E5%96%84%E7%94%A8-ignore_user_abort-%E5%87%BD%E5%BC%8F/
http://www.isdee.com/notes/ecshop_phpmailer.html
http://hi.baidu.com/yxl7808/item/00b9ca9c85628ae62916471d
http://sourceforge.net/p/phpmailer/discussion/81620/thread/6819cd16/
清除收件者欄位
由於預設是用addaddress()所以當跑迴圈時會重複寄送
因此需使用clearaddresses()並且不能用unset(), unset只是暫時重設,下次再呼叫時會讀取unset前之數值
http://www.wibibi.com/info.php?tid=236
http://chan15.blogspot.tw/2011/11/phpmailer.html
textarea
使用input只有一行,因此一般的留言板都應該使用textarea
另外input可以設定長度跟高度,雖然還是只能輸入一行
http://stackoverflow.com/questions/4391344/how-can-i-set-the-size-of-the-box-of-a-form
http://crazy.molerat.net/learner/cpuroom/programmer/reading.php?filename=1010407112825.dov
http://www.php-example.com/2011/05/php-html-form-textarea-example.html
斷行處理
http://chensh.loxa.edu.tw/php/X_8.php
http://www.jollen.org/php/jollen_php_book_79_textarea.html
http://itgroup.blueshop.com.tw/ann71727/ann71727?n=convew&i=6032

java心得

java的變數不能使用數字開頭,否則會判斷為數字。

有關筆電透過無線網路及VPN觀看遠端區域網路影片斷線LAG及雜音問題

這個問題困擾我好久,並且每次在我播放影片時就會出現。經過測試發現斷線問題是因為無線網路切換問題,固定在802.11g就不會斷線,一開始還以為是VPN問題。另外LAG及雜音則應該是因為硬碟空間不足,所以在讀取時需要一邊清空間一邊播放造成LAG及有雜音,想說i5應該不會不夠力才對。剛剛測試把硬碟空間清出來就沒有再遇到相同問題,明天再測試一次就知道究竟是不是solution了。

更新:
換了一台筆電之後就沒問題,看來是Hardware issue

參考資料
11樓
http://www.mobile01.com/topicdetail.php?f=300&t=1347232&p=2

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