以下爲個人撰寫的Shell Script代碼段,用於提高個人生產效率。格式分爲2種:在Shell Script中,在~/.bashrc中。

~/.bashrc中,需要對雙引號"和awk中的$做轉義。

Python Version Check

查看Python最新版本信息

Shell Script

1
2
3
curl -fsL https://www.python.org/downloads/ | sed -r -n '/download-os-source/,/<\/div>/s@.*href="(.*)">Download Python (.*)</a>@\2 \[email protected]' | while read -r version download_link;do
curl -fsL https://www.python.org/downloads/ | sed -r -n '/list-row-container/,/View older releases/{s@^[[:space:]]*@@g;s@</?(span|ol)[^>]*>@@g;p}' | awk '{if($0!~/<\/li>/){ORS="";print $0}else{printf "\n"}}' | sed -r -n '/'"${version}"'/s@.*<a href="(.*)">Python (.*)</a>(.*)<a href=".*"> Download</a><a href="(.*)">.*@\2 \3 '"${download_link}"' https://www.python.org\1 \[email protected]' | awk '{printf("Version \033[31;1m%s\033[0m\nRelease Date \033[31;1m%s\033[0m\nDownload Link \033[31;1m%s\033[0m\nDownload Page \033[31;1m%s\033[0m\nRelease Note \033[31;1m%s\033[0m\n\n",$1,$2,$3,$4,$5)}'
done

bashrc

1
alias pythonVersionCheck="curl -fsL https://www.python.org/downloads/ | sed -r -n '/download-os-source/,/<\/div>/s@.*href=\"(.*)\">Download Python (.*)</a>@\2 \[email protected]' | while read -r version download_link;do curl -fsL https://www.python.org/downloads/ | sed -r -n '/list-row-container/,/View older releases/{s@^[[:space:]]*@@g;s@</?(span|ol)[^>]*>@@g;p}' | awk '{if(\$0!~/<\/li>/){ORS=\"\";print \$0}else{printf \"\n\"}}' | sed -r -n '/'"\${version}"'/s@.*<a href=\"(.*)\">Python (.*)</a>(.*)<a href=\".*\"> Download</a><a href=\"(.*)\">.*@\2 \3 '"\${download_link}"' https://www.python.org\1 \[email protected]' | awk '{printf(\"Version \033[31;1m%s\033[0m\nRelease Date \033[31;1m%s\033[0m\nDownload Link \033[31;1m%s\033[0m\nDownload Page \033[31;1m%s\033[0m\nRelease Note \033[31;1m%s\033[0m\n\n\",\$1,\$2,\$3,\$4,\$5)}'; done"

執行結果如下

1
2
3
4
5
6
7
8
9
10
11
Version 3.6.1
Release Date 2017-03-21
Download Link https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
Download Page https://www.python.org/downloads/release/python-361/
Release Note https://docs.python.org/3.6/whatsnew/changelog.html#python-3-6-1
Version 2.7.13
Release Date 2016-12-17
Download Link https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
Download Page https://www.python.org/downloads/release/python-2713/
Release Note https://hg.python.org/cpython/raw-file/v2.7.13/Misc/NEWS

Hosts Update

更新系統/etc/hosts文件,內容來源racaljk

Shell Script

1
2
sudo sed -i '/Modified Hosts Start/,/Modified Hosts End/d' /etc/hosts
curl -fsL https://raw.githubusercontent.com/racaljk/hosts/master/hosts | sed -r -e '1i\# Modified Hosts Start' -e '/Copyright/,/Localhost.*End/d' | sudo tee -a /etc/hosts 1> /dev/null

bashrc

1
alias hostsupdate="sudo sed -i '/Modified Hosts Start/,/Modified Hosts End/d' /etc/hosts && curl -fsL https://raw.githubusercontent.com/racaljk/hosts/master/hosts | sed -r -e '1i\# Modified Hosts Start' -e '/Copyright/,/Localhost.*End/d' | sudo tee -a /etc/hosts 1> /dev/null"

Docker Image Update

Shell Script

1
2
3
4
5
6
7
8
9
sudo systemctl restart docker
docker images | awk 'match($1,/[^(hexo|REPOSITORY)]/)&&match($2,/[^<none>]/){print $1,$2}' | while read -r name tag;do
docker pull $name:$tag
done
docker images | awk 'match(\$2,/<none>/){print \$3}' | while read -r imageid;do
docker rmi -f $imageid
done

bashrc

1
alias dockerimageupdate="sudo systemctl restart docker && docker images | awk 'match(\$1,/[^(hexo|raxtonegit|REPOSITORY)]/)&&match(\$2,/[^<none>]/){print \$1,\$2}' | while read -r name tag;do docker pull \$name:\$tag; done && docker images | awk 'match(\$2,/<none>/){print \$3}' | while read -r imageid;do docker rmi -f \$imageid; done"

SamAir Proxy IP Extraction

[SamAir][samair]是一個代理IP網站,提供HTTP和SOCKS類型的IP。

Shell Script

socks5 list

1
2
3
4
5
page_no=\$(curl -fsL https://premproxy.com/socks-list/01.htm | sed -r -n '/next/{s@<[^>]*>@@gp}' | awk '{print \$(NF-1)}');
seq -f 0%g 1 \$page_no | parallel -k -j 0 -X curl -fsL https://premproxy.com/socks-list/{}.htm 2> /dev/null | sed -r -n '/^<tr><td>/{{s@<\/?(tr)[[:space:]]*[^>]*>@@g;s@<td>@@g;s@[[:space:]]*<\/td>@[email protected];s@>.*@@g;s@(<dfn title="|")@@g;p}}' | awk -F\| 'match($2,/5/){printf("%s|%s|%s|%s\n",$1,$4,$5,$6)}' | while IFS="|" read -r ip country city isp; do
[[ -n $(curl -fsSL --speed-time 1 --speed-limit 3 --max-time 2 -x socks5h://"$ip" ipinfo.io/country 2> /dev/null) ]] && echo "$ip|$country|$city|$isp"
done

high-anonymous list

1
2
3
seq -f 0%g 1 6 | parallel -k -j 0 -X curl -fsL https://premproxy.com/list/{}.htm 2> /dev/null | sed -r -n '/ptabletitle/,/pageinfo/{/tr class/{s@<\/?(tr)[[:space:]]*[^>]*>@@g;s@<td>@@g;s@[[:space:]]*<\/td>@[email protected];s@>.*@@g;s@(<dfn title="|")@@g;p}}' | sed '/^[[:space:]]*$/d' | awk -F\| 'match($2,/high/){printf("%s|%s|%s|%s\n",$1,$4,$5,$6)}' | while IFS="|" read -r ip country city isp; do
[[ -n \$(curl -fsSL --speed-time 1 --speed-limit 3 --max-time 2 -x "$ip" ipinfo.io/country 2> /dev/null) ]] && echo "$ip|$country|$city|$isp"
done

bashrc

1
2
3
alias proxy_socks5="page_no=\$(curl -fsL https://premproxy.com/socks-list/01.htm | sed -r -n '/next/{s@<[^>]*>@@gp}' | awk '{print \$(NF-1)}'); seq -f 0%g 1 \$page_no | parallel -k -j 0 -X curl -fsL https://premproxy.com/socks-list/{}.htm 2> /dev/null | sed -r -n '/^<tr><td>/{{s@<\/?(tr)[[:space:]]*[^>]*>@@g;s@<td>@@g;s@[[:space:]]*<\/td>@[email protected];s@>.*@@g;s@(<dfn title=\"|\")@@g;p}}' | awk -F\| 'match(\$2,/5/){printf(\"%s|%s|%s|%s\n\",\$1,\$4,\$5,\$6)}' | while IFS=\"|\" read -r ip country city isp;do [[ -n \$(curl -fsSL --speed-time 1 --speed-limit 3 --max-time 2 -x socks5h://\"\$ip\" ipinfo.io/country 2> /dev/null) ]] && echo \"\$ip|\$country|\$city|\$isp\"; done"
alias proxy_high_anonymous="seq -f 0%g 1 6 | parallel -k -j 0 -X curl -fsL https://premproxy.com/list/{}.htm 2> /dev/null | sed -r -n '/ptabletitle/,/pageinfo/{/tr class/{s@<\/?(tr)[[:space:]]*[^>]*>@@g;s@<td>@@g;s@[[:space:]]*<\/td>@[email protected];s@>.*@@g;s@(<dfn title=\"|\")@@g;p}}' | sed '/^[[:space:]]*$/d' | awk -F\| 'match(\$2,/high/){printf(\"%s|%s|%s|%s\n\",\$1,\$4,\$5,\$6)}' | while IFS=\"|\" read -r ip country city isp;do [[ -n \$(curl -fsSL --speed-time 1 --speed-limit 3 --max-time 2 -x \"\$ip\" ipinfo.io/country 2> /dev/null) ]] && echo \"\$ip|\$country|\$city|\$isp\"; done"

Change Logs

  • 2017.06.09 15:02 Fri Asia/Shanghai
    • 初稿完成
  • 2017.06.16 11:06 Fri Asia/Shanghai
    • 添加SamAir代理IP提取

[samair]: https://premproxy.com/ ‘SamAir’