Skip to content

Instantly share code, notes, and snippets.

@MarkZhangTW
MarkZhangTW / .bashrc
Last active June 14, 2020 01:51
My bashrc
# BUG: cursor will disappear after open tmux
function cursor_green { printf '%b' '\e]12;green\a'; }
# cursor_green
# print escape character when login will cause SCP malfunction
# SSH auto-reconnect
# https://backreference.org/2013/04/26/ssh-auto-reconnect/
function ssh {
while true; do
command ssh "$@";
@MarkZhangTW
MarkZhangTW / fail2block.ip.txt
Last active August 9, 2020 10:12
A list of IPs that have tried to brute force login into my SSH server.
1.214.215.236
2.57.122.186
2.135.239.123
5.39.29.252
5.101.151.83
5.135.181.53
5.135.224.152
5.189.167.107
13.65.190.193
13.70.1.39
#sudo lastb -ai | head -n-2 | awk '{print$NF}' | awk '/[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/{print}' | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n | uniq --count | awk '$1>9{print$2}' > failed.ip.9
sudo lastb -ai | head -n-2 | awk '{print$NF}' | awk '/[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/{print}' | sort | uniq --count | awk '$1>9{print$2}' > failed.ip.9
for ip in $(cat failed.ip.9); do
sudo firewall-cmd --permanent --zone=public --add-rich-rule="rule family='ipv4' source address='$ip' reject"
done
sudo firewall-cmd --reload
@MarkZhangTW
MarkZhangTW / runPython.java
Created April 17, 2020 10:44
Run python script in Java in Windows
public class App {
public static void main(String[] args) throws IOException {
final String result = runPython("script.py", "arg1", "arg2");
System.out.println(result);
}
private static String runPython(String script, String... args) throws IOException {
List<String> newArgs = new ArrayList<>();
newArgs.add(script);
@MarkZhangTW
MarkZhangTW / JavaSerializationDemo.java
Created April 13, 2020 06:25
Demo of Java Serialization
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Base64;
class JavaSerializationDemo {
@MarkZhangTW
MarkZhangTW / cow.find.js
Created April 12, 2020 07:28
Find the Invisible Cow Cheater
var cow = document.querySelector('div.game')
for (let y = 0; y < cow.clientHeight; y += 25)
for (let x = 0; x < cow.clientWidth; x += 25)
cow.dispatchEvent(new MouseEvent('click', {clientX: x, clientY: y}));
@MarkZhangTW
MarkZhangTW / curl.time.sh
Last active April 10, 2020 06:03
Curl format for displaying time infomation.
#!/usr/bin/env bash
# Reference: https://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl
curl_format="\
%{time_namelookup}:\
%{time_connect}:%{time_appconnect}:\
%{time_pretransfer}:%{time_redirect}:\
%{time_starttransfer}:%{time_total}"
curl_result=$(echo "$curl_format" | curl -s -o /dev/null -w @- $@)
IFS=':' read -r -a array <<< "$curl_result"
@MarkZhangTW
MarkZhangTW / cf.update.py
Created April 3, 2020 14:26
Update CloudFlare DDNS by Python
#!/usr/bin/env python3
import requests
import json
from datetime import datetime
ip = requests.get('https://icanhazip.com').text.strip()
cf_endpoint = 'https://api.cloudflare.com/client/v4/'
cf_apitoken = 'Bearer <Your API Token>'
# You can get <zone id> on CloudFlare dashboard
@MarkZhangTW
MarkZhangTW / .vimrc
Last active August 6, 2021 23:17
My vimrc
set background=dark
set number
highlight LineNr ctermfg=blue
syntax on
set tabstop=4
set shiftwidth=4
set expandtab
set smarttab
set autoindent
@MarkZhangTW
MarkZhangTW / Update-CF-DNS.ps1
Last active February 1, 2021 13:45
Update CloudFlare DNS records with PowerShell
$CF = @{}
$CF.Endpoint = "https://api.cloudflare.com/client/v4/"
# Replace <API Token> to your CloudFlare API Token
# You can create your API Token from https://dash.cloudflare.com/profile/api-tokens
$CF.APIToken = "Bearer <API Token>"
# Replace <zone id> and <record id>
$CF.UpdateDNSRecord = @{Method = "PUT"; object = "zones/<zone id>/dns_records/<record id>"}
$PublicIP = (Get-NetIPAddress -InterfaceAlias "<Interface Alias>" -AddressFamily IPv4).IPAddress