Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active March 13, 2017 14:35
Show Gist options
  • Save tps2015gh/718f6aefbbc2a3ec824cba58f7559f7d to your computer and use it in GitHub Desktop.
Save tps2015gh/718f6aefbbc2a3ec824cba58f7559f7d to your computer and use it in GitHub Desktop.
PowerShell Script get Last 50 Line Apache Log / Summary and By Time
#================================================================
# @Author: Thitipong.s
# @Since : 2017-03-13
# @For : (Demo ) Display tail log in xampp log
#================================================================
# copy and past this script to power shell script
#===========================================
function loadfile($max_tail = 50 ){
$row = Get-Content c:\xampp\apache\logs\access.log -Tail $max_tail
return $row
}
#===========================================
function filter_row($array , $search_text){
return $array.GetEnumerator() | Where { $_ -match $search_text }
}
#===========================================
function rep_sum($row){
$aurl = @{}
$aurl2 = @()
foreach ($r in $row ){
$col = $r.split(" ")
$url = $col[6]
if( -Not ($aurl.Contains($url ))){
$tmp = $aurl.Add( $url , 1)
}else{
$aurl[$url] = $aurl[$url] + 1
}
$aurl2 += $url
}
# report sum
$rep = $aurl.GetEnumerator() | Select Value, Name | Sort-Object Value -descending
#$aurl
#$aurl2
return @{'r_bycount'= $rep ; 'rawlog_url' = $aurl2 }
}
function main($max_tail = 50 ){
$row = loadfile -max_tail $max_tail
$result = rep_sum -row $row
$r_bycount = $result['r_bycount']
$rawlog_url = $result['rawlog_url']
"====== Summary Data ========= "
#full data
$r_bycount
# filter
"====== Filter Text on Summary Data ========= "
filter_row -array $r_bycount -search_text "admin"
"====== Filter Text on Summary raw log ========= "
filter_row -array $rawlog_url -search_text "admin"
"===== raw log , by time ====== "
$rawlog_url
}
return main -max_tail 50
#=====================
# run main -max_tail 10 ,again , again and again
#=====================
#===========================================
$row = Get-Content c:\xampp\apache\logs\access.log -Tail 50
#===========================================
$aurl = @{}
$aurl2 = @()
foreach ($r in $row ){
$col = $r.split(" ")
$url = $col[6]
if( -Not ($aurl.Contains($url ))){
$tmp = $aurl.Add( $url , 1)
}else{
$aurl[$url] = $aurl[$url] + 1
}
$aurl2 += $url
}
$aurl.GetEnumerator() | Select Value, Name | Sort-Object Value -descending
$aurl
$aurl2
#NOTE :
# หาต่อ ว่า url พวกนี้ มีอันไหนบ้าง มีคำว่า root ใน url
$aurl2.GetEnumerator() | where { $_ -Match "root" }
# หาว่า แถวไหน เป็น 404 บ้าง
$row.GetEnumerator() | where { $_ -Match "1.1"" 404" }
# หาว่าแถวไหน มีคำว่า admin บ้าง
$row.GetEnumerator() | where { $_ -Match "admin" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment