2011/12/29

msys git single file history

In Git BASH
gitk -- path/to/file &

Or right click on file
Then click "Git History"

BTW...
Not only file, files in folders also do.

2011/12/26

net snmp 尋找 mib tree

先看 net snmp
把mib檔設定一下 然後載入mib檔

找名字含有ssl的snmp oid
snmptranslate -TB '.*ssl.*'

http://www.net-snmp.org/wiki/index.php/TUT:snmptranslate 看來的...

2011/12/13

html encode/decode using javascript

  1. String.prototype.encodeHTML = function() { var d=document.createElement("div");(d.textContent)?(d.textContent=this):d.innerText=this;return d.innerHTML };
  2. String.prototype.decodeHTML = function() { var d=document.createElement("div");d.innerHTML=this;return d.innerText||d.textContent };

2011/11/23

net snmp

net-snmp download

安裝至預設路徑C:\netsnmp


設定 C:\netsnmp\usr\etc\snmp\snmp.conf
document -> C:\netsnmp\usr\docs\Net-SNMP.chm

#放mib的地方
mibdirs C:/netsnmp/usr/share/snmp/mibs-XXXX
persistentDir C:/netsnmp/usr/snmp/persist
tempFilePattern C:/netsnmp/usr/temp/snmpdXXXXXX

#載入所有的mib
mibs all

#預設版本 snmpv2c
defVersion 2c

#預設Community
defCommunity private


然後就可以用cmd來下指令了...

P.S.
記得要加環境變數 ;C:\netsnmp\usr\bin 在PATH後面...


snmp走走...
snmpwalk 192.168.1.1 system

從頭走到尾...
snmpwalk 192.168.1.1 .1

snmp翻譯成樹狀...
snmptranslate -IR -Tp system

2011/08/23

Git get diff file list and diff content from some branch

  1. BRANCH_NAME='dev'
  2. SOURCE_DIR='/home/me/src'
  3. EXPORT_TO='/home/me/out'
  4.  
  5. cd $SOURCE_DIR
  6. git diff --name-only $BRANCH_NAME > $EXPORT_TO/changes.txt
  7. git diff $BRANCH_NAME > $EXPORT_TO/changes.diff
  8.  

2011/08/12

Unix like shell - find_keywords

  1. #!/bin/sh
  2. if [ "xx$2" != "xx" ] && [ -e $2 ]; then
  3.     echo finding $1 in $2
  4.     if [ "xx$WINDIR" != "xx" ]; then
  5.         grep -lr $1 $2 | sed 's~/~\\~g' | sed 's~^\\\(\w\)\\~\U\1:\\~g'
  6.     else
  7.         grep -lr $1 $2
  8.     fi
  9. else
  10.     echo first param must be search string
  11.     echo second param must be full path
  12. fi

2011/08/10

CodeBlocks 中文化

下載mo檔:
codeblocks translation page
www.badongo.com - Click Here to Download codeblocks zh_TW
www.u-file.net - Click Here to Download codeblocks zh_TW

然後建一個目錄 C:\Program Files\CodeBlocks\share\CodeBlocks\locale\zh_TW
其中 C:\Program Files\CodeBlocks 是安裝路徑
然後把mo檔放到裏面去

Settings -> Environment -> View -> Internationalization
勾起來 -> 選 Chinese
然後重開CodeBlocks

搞定...

BTW...中文化以後, syntex highlight好像消失了...
所以還是把勾勾拿掉好了....sigh.....

2011/07/15

Compare a Copy of a Project

Usage:
compare-projects /home/me/project/ /home/me/old/project/ | grep -v OK > /home/me/compare_result 2>&1
Source: compare-projects
  1. #!/usr/bin/sh
  2.  
  3. #
  4. # settings
  5. #
  6.  
  7. md5=~/bin/md5files
  8.  
  9. #
  10. # inputs
  11. # src: main project
  12. # dst: another project
  13. #
  14.  
  15. src=$1
  16. dst=$2
  17.  
  18. #
  19. # limit file size smaller than 1024k (pass to find)
  20. # md5 all files, and then replace the path to destination
  21. # finally compare the md5 "use md5sum -c"
  22. #
  23.  
  24. $md5 $src -size -1024k | sed "s!${src}!${dst}!g" | md5sum -c 2>&1

Get All Files Recursively and Get MD5

  1. #!/usr/bin/sh
  2.  
  3. #
  4. # find all files recursively, and enum them to get md5sum
  5. # args: pass all to find
  6. #
  7.  
  8. for f in $(find $@ -type f);
  9. do
  10.     md5sum $f
  11. done

2011/04/09

使用javascript產生guid

參考來源: http://note19.com/2007/05/27/javascript-guid-generator/
javascript
  1. function guid() {
  2.     function S4() {
  3.         return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  4.     }
  5.     return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  6. }

html javascript 小時鐘 digit clock


就像這樣:
html javascript
  1. <span id="__current_datetime__">
  2. <script type="text/javascript">
  3.     (function () {
  4.         var target = "__current_datetime__";
  5.         var padLeft = function(str) {
  6.             str = "00" + str;
  7.             return str.substring(str.length-2)
  8.         };
  9.         var getCurrentDateTime = function() {
  10.             var d = new Date();
  11.             return d.getFullYear() + "&#24180;"
  12.                 + padLeft(d.getMonth()+1) + "&#26376;"
  13.                 + padLeft(d.getDate()) + "&#26085; "
  14.                 + padLeft(d.getHours()) + ":"
  15.                 + padLeft(d.getMinutes()) + ":"
  16.                 + padLeft(d.getSeconds());
  17.         };
  18.         var updateCurrentDateTime = function(id) {
  19.             document.getElementById(id).innerHTML = getCurrentDateTime();
  20.         };
  21.         updateCurrentDateTime(target);
  22.         setInterval(function() {
  23.             updateCurrentDateTime(target);
  24.         }, 99);
  25.     }) ()

製造出純html的原始碼線上工具(Online Syntex Highlighter)

quickhighlighter

VB.net 動態變更使用者 switch user dynamically

文章參考: 如何在 ASP.NET 應用程式中實作模擬

VB.net
  1. Public Class Personate
  2.     Private Shared LOGON32_LOGON_INTERACTIVE As Integer = 2
  3.     Private Shared LOGON32_PROVIDER_DEFAULT As Integer = 0
  4.     Private Shared ImpersonationContext As WindowsImpersonationContext
  5.  
  6.     Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
  7.                             ByVal lpszDomain As String, _
  8.                             ByVal lpszPassword As String, _
  9.                             ByVal dwLogonType As Integer, _
  10.                             ByVal dwLogonProvider As Integer, _
  11.                             ByRef phToken As IntPtr) As Integer
  12.  
  13.     Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
  14.                             ByVal ExistingTokenHandle As IntPtr, _
  15.                             ByVal ImpersonationLevel As Integer, _
  16.                             ByRef DuplicateTokenHandle As IntPtr) As Integer
  17.  
  18.     Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
  19.     Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
  20.  
  21.     Public Shared Function ImpersonateValidUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean
  22.  
  23.         Dim tempWindowsIdentity As WindowsIdentity
  24.         Dim token As IntPtr = IntPtr.Zero
  25.         Dim tokenDuplicate As IntPtr = IntPtr.Zero
  26.         ImpersonateValidUser = False
  27.  
  28.         If RevertToSelf() Then
  29.             If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
  30.                 If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
  31.                     tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
  32.                     ImpersonationContext = tempWindowsIdentity.Impersonate()
  33.                     If Not ImpersonationContext Is Nothing Then
  34.                         ImpersonateValidUser = True
  35.                     End If
  36.                 End If
  37.             End If
  38.         End If
  39.         If Not tokenDuplicate.Equals(IntPtr.Zero) Then
  40.             CloseHandle(tokenDuplicate)
  41.         End If
  42.         If Not token.Equals(IntPtr.Zero) Then
  43.             CloseHandle(token)
  44.         End If
  45.     End Function
  46.  
  47.     Public Shared Sub UndoImpersonation()
  48.         ImpersonationContext.Undo()
  49.     End Sub
  50. End Class

VB.net Convert to JSON

VB.net
  1. Public Class JSONConvert
  2.     Public Shared Function DataTable2NameValueCollection(ByVal dt As DataTable) As NameValueCollection
  3.         Dim columnlist As New List(Of String)()
  4.         For Each column As DataColumn In dt.Columns
  5.             columnlist.Add(column.ColumnName)
  6.         Next
  7.  
  8.         Dim rowlist As New List(Of String)()
  9.         For Each row As DataRow In dt.Rows
  10.             Dim itemlist As New List(Of String)()
  11.             For Each column As DataColumn In dt.Columns
  12.                 If column.DataType.Equals(GetType(Int32)) Or column.DataType.Equals(GetType(Double)) Then
  13.                     itemlist.Add(row(column.ColumnName))
  14.                 Else
  15.                     itemlist.Add(String.Format("""{0}""", row(column.ColumnName)))
  16.                 End If
  17.             Next
  18.             rowlist.Add(String.Format("[{0}]", String.Join(", ", itemlist.ToArray())))
  19.         Next
  20.  
  21.         Dim nv As New NameValueCollection()
  22.         nv.Add("columns", Array2JSONArray(columnlist.ToArray()))
  23.         nv.Add("rows", Array2JSONArray(rowlist.ToArray()))
  24.         Return nv
  25.     End Function
  26.  
  27.     Public Shared Function DataTable2JSONObject(ByVal dt As DataTable) As String
  28.         Dim nv As NameValueCollection = DataTable2NameValueCollection(dt)
  29.         Return NameValueCollection2JSONObjectArray(nv)
  30.     End Function
  31.  
  32.     Public Shared Function NameValueCollection2JSONObjectArray(ByVal nv As System.Collections.Specialized.NameValueCollection) As String
  33.         Dim list As New List(Of String)()
  34.         For Each name As String In nv.AllKeys
  35.             If Regex.IsMatch(nv(name), "^(.*(\[|\]|{|})+.*|\d+)$") Then
  36.                 list.Add(String.Format("""{0}"": {1}", name, nv(name)))
  37.             Else
  38.                 list.Add(String.Format("""{0}"": ""{1}""", name, nv(name)))
  39.             End If
  40.         Next
  41.         Return String.Format("{{{0}}}", String.Join(", ", list.ToArray()))
  42.     End Function
  43.  
  44.     Public Shared Function Array2JSONArray(ByVal arr As Array) As String
  45.         If Regex.IsMatch(String.Join(", ", CType(arr, String())), "^(.*(\[|\]|{|})+.*|\d+)$") Then
  46.             Return ObjectArray2JSONArray(arr)
  47.         Else
  48.             Return StringArray2JSONArray(arr)
  49.         End If
  50.     End Function
  51.  
  52.     Public Shared Function StringArray2JSONArray(ByVal arr() As String) As String
  53.         If arr.Length = 0 Then
  54.             Return ("[]")
  55.         Else
  56.             Return String.Format("[""{0}""]", String.Join(""", """, arr))
  57.         End If
  58.     End Function
  59.  
  60.     Public Shared Function ObjectArray2JSONArray(ByVal arr As Array) As String
  61.         Return String.Format("[{0}]", String.Join(", ", CType(arr, String())))
  62.     End Function
  63. End Class