Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chap0x04 #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions chap0x04/code/task-1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env bash

# 帮助文档
function help {
echo "doc"
echo "-q Q 对jpeg格式图片进行图片质量因子为Q的压缩"
echo "-r R 对jpeg/png/svg格式图片在保持原始宽高比的前提下压缩成R分辨率"
echo "-w font_size text 对图片批量添加自定义文本水印"
echo "-p text 统一添加文件名前缀,不影响原始文件扩展名"
echo "-s text 统一添加文件名后缀,不影响原始文件扩展名"
echo "-t 将png/svg图片统一转换为jpg格式图片"
echo "-h 帮助文档"
}
# 对jpeg格式图片进行图片质量压缩

function compressQuality {
Q=$1 # 质量因子
for i in *;do
type=${i##*.} # 删除最后一个.及左边全部字符
if [[ ${type} != "jpeg" ]]; then continue; fi;
convert "${i}" -quality "${Q}" "${i}"
echo "${i} is compressed."
done
}

# 对jpeg/png/svg格式图片在保持原始宽高比的前提下压缩分辨率

function compressResolution {
R=$1
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
convert "${i}" -resize "${R}" "${i}"
echo "${i} is resized."
done
}

# 对图片批量添加自定义文本水印

function watermark {
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
convert "${i}" -pointsize "$1" -fill black -gravity center -draw "text 10,10 '$2'" "${i}"
echo "${i} is watermarked with $2."
done
}

# 批量重命名(统一添加文件名前缀或后缀,不影响原始文件扩展名)

function prefix {
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
mv "${i}" "$1""${i}"
echo "${i} is renamed to $1${i}"
done
}
function suffix {
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
filename=${i%.*}$1"."${type}
mv "${i}" "${filename}"
echo "${i} is renamed to ${filename}"
done
}

# 将png/svg图片统一转换为jpg格式图片

function transform2Jpg {
for i in *;do
type=${i##*.}
if [[ ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
filename=${i%.*}".jpg"
convert "${i}" "${filename}"
echo "${i} is transformed to ${filename}"
done
}

while [ "$1" != "" ];do
case "$1" in
"-q")
compressQuality "$2"
exit 0
;;
"-r")
compressResolution "$2"
exit 0
;;
"-w")
watermark "$2" "$3"
exit 0
;;
"-p")
prefix "$2"
exit 0
;;
"-s")
suffix "$2"
exit 0
;;
"-t")
transform2Jpg
exit 0
;;
"-h")
help
exit 0
;;
esac
done
# 调试实现代码
113 changes: 113 additions & 0 deletions chap0x04/code/task-2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env bash

# 帮助文档
function help {
echo "doc"
echo "-q Q 对jpeg格式图片进行图片质量因子为Q的压缩"
echo "-r R 对jpeg/png/svg格式图片在保持原始宽高比的前提下压缩成R分辨率"
echo "-w font_size text 对图片批量添加自定义文本水印"
echo "-p text 统一添加文件名前缀,不影响原始文件扩展名"
echo "-s text 统一添加文件名后缀,不影响原始文件扩展名"
echo "-t 将png/svg图片统一转换为jpg格式图片"
echo "-h 帮助文档"
}
# 对jpeg格式图片进行图片质量压缩

function compressQuality {
Q=$1 # 质量因子
for i in *;do
type=${i##*.} # 删除最后一个.及左边全部字符
if [[ ${type} != "jpeg" ]]; then continue; fi;
convert "${i}" -quality "${Q}" "${i}"
echo "${i} is compressed."
done
}

# 对jpeg/png/svg格式图片在保持原始宽高比的前提下压缩分辨率

function compressResolution {
R=$1
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
convert "${i}" -resize "${R}" "${i}"
echo "${i} is resized."
done
}

# 对图片批量添加自定义文本水印

function watermark {
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
convert "${i}" -pointsize "$1" -fill black -gravity center -draw "text 10,10 '$2'" "${i}"
echo "${i} is watermarked with $2."
done
}

# 批量重命名(统一添加文件名前缀或后缀,不影响原始文件扩展名)

function prefix {
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
mv "${i}" "$1""${i}"
echo "${i} is renamed to $1${i}"
done
}
function suffix {
for i in *;do
type=${i##*.}
if [[ ${type} != "jpeg" && ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
filename=${i%.*}$1"."${type}
mv "${i}" "${filename}"
echo "${i} is renamed to ${filename}"
done
}

# 将png/svg图片统一转换为jpg格式图片

function transform2Jpg {
for i in *;do
type=${i##*.}
if [[ ${type} != "png" && ${type} != "svg" ]]; then continue; fi;
filename=${i%.*}".jpg"
convert "${i}" "${filename}"
echo "${i} is transformed to ${filename}"
done
}

while [ "$1" != "" ];do
case "$1" in
"-q")
compressQuality "$2"
exit 0
;;
"-r")
compressResolution "$2"
exit 0
;;
"-w")
watermark "$2" "$3"
exit 0
;;
"-p")
prefix "$2"
exit 0
;;
"-s")
suffix "$2"
exit 0
;;
"-t")
transform2Jpg
exit 0
;;
"-h")
help
exit 0
;;
esac
done
# 调试实现代码
99 changes: 99 additions & 0 deletions chap0x04/code/task-3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env bash

function help {
echo "doc"
echo "-o 统计访问来源主机TOP 100和分别对应出现的总次数"
echo "-i 统计访问来源主机TOP 100 IP和分别对应出现的总次数"
echo "-u 统计最频繁被访问的URL TOP 100"
echo "-c 统计不同响应状态码的出现次数和对应百分比"
echo "-f 分别统计不同4XX状态码对应的TOP 10 URL和对应出现的总次数"
echo "-s URL 给定URL输出TOP 100访问来源主机"
echo "-h 帮助文档"
}

# 统计访问来源主机TOP 100和分别对应出现的总次数
function top100_host {
printf "%40s\t%s\n" "TOP100_host" "count"
awk -F "\t" '
NR>1 {host[$1]++;}
END { for(i in host) {printf("%40s\t%d\n",i,host[i]);} }
' web_log.tsv | sort -g -k 2 -r | head -100
}
# 统计访问来源主机TOP 100 IP和分别对应出现的总次数
function top100_IP {
printf "%20s\t%s\n" "TOP100_IP" "count"
awk -F "\t" '
NR>1 {if(match($1, /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/)) ip[$1]++;}
END { for(i in ip) {printf("%20s\t%d\n",i,ip[i]);} }
' web_log.tsv | sort -g -k 2 -r | head -100
}
# 统计最频繁被访问的URL TOP 100
function top100_URL {
printf "%55s\t%s\n" "TOP100_URL" "count"
awk -F "\t" '
NR>1 {url[$5]++;}
END { for(i in url) {printf("%55s\t%d\n",i,url[i]);} }
' web_log.tsv | sort -g -k 2 -r | head -100
}
# 统计不同响应状态码的出现次数和对应百分比
function stateCode {
awk -F "\t" '
BEGIN {printf("code\tcount\tpercentage\n");}
NR>1 {code[$6]++;}
END { for(i in code) {printf("%d\t%d\t%f%%\n",i,code[i],100.0*code[i]/(NR-1));} }
' web_log.tsv
}
# 分别统计不同4XX状态码对应的TOP 10 URL和对应出现的总次数
function stateCode4 {
printf "%55s\t%s\n" "code=403 URL" "count"
awk -F "\t" '
NR>1 { if($6=="403") code[$5]++;}
END { for(i in code) {printf("%55s\t%d\n",i,code[i]);} }
' web_log.tsv | sort -g -k 2 -r | head -10

printf "%55s\t%s\n" "code=404 URL" "count"
awk -F "\t" '
NR>1 { if($6=="404") code[$5]++;}
END { for(i in code) {printf("%55s\t%d\n",i,code[i]);;} }
' web_log.tsv | sort -g -k 2 -r | head -10
}
# 给定URL输出TOP 100访问来源主机
function specificURL {
printf "%40s\t%s\n" "TOP100_host" "count"
awk -F "\t" '
NR>1 {if("'"$1"'"==$5) {host[$1]++;} }
END { for(i in host) {printf("%40s\t%d\n",i,host[i]);} }
' web_log.tsv | sort -g -k 2 -r | head -100
}
while [ "$1" != "" ];do
case "$1" in
"-o")
top100_host
exit 0
;;
"-i")
top100_IP
exit 0
;;
"-u")
top100_URL
exit 0
;;
"-c")
stateCode
exit 0
;;
"-f")
stateCode4
exit 0
;;
"-s")
specificURL "$2"
exit 0
;;
"-h")
help
exit 0
;;
esac
done
Binary file added chap0x04/img/extention_install.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chap0x04/img/task1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chap0x04/img/task1-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chap0x04/img/task1-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chap0x04/img/task1-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chap0x04/img/task1-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chap0x04/img/task1-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading