Skip to content

Instantly share code, notes, and snippets.

@yougg
Created April 4, 2022 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yougg/55bfd0e1c464373733e3cd808d97bf41 to your computer and use it in GitHub Desktop.
Save yougg/55bfd0e1c464373733e3cd808d97bf41 to your computer and use it in GitHub Desktop.
Command Note

Command Note

@yougg
Copy link
Author

yougg commented Apr 4, 2022

Linux

查看进程环境变量

tr \\0 \\n < /proc/${PID}/environ

ps eww -p ${PID}

查看进程网络连接

lsof -i 4 -a -p ${PID}
lsof -i :${Port}
netstat -anp | grep ${PID}

生成随机复杂密码

# 16位密码:
< /dev/urandom tr -dc '`~!@#$%^&*()-_=+[]{}\|;:\,./<>?"1-9a-zA-Z' | head -c${1:-16}; echo

生成数字序列

seq -f "%03.f" 9 12

强制关机

echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

重设登陆失败重试次数

sudo pam_tally2 -u root --reset=0

设置普通用户定时任务

# 添加username编辑crontab权限
sudo echo username >> /etc/cron.allow

# 添加定时任务到username用户的任务列表
sudo crontab -u username -e
# 加入定时任务, 例如:
* * * * * /path/to/cmd

# 查看保存在username用户下的任务
sudo crontab -u username -l

# 重启定时任务服务加载新的任务运行
sudo service cron restart

# 查看定时任务日志
sudo vi /etc/cron.log

查看网线连接状态

ethtool eth0
mii-tool eth0

上传文件

scp -q -o StrictHostKeyChecking=no -i identtify_file -P ${Port} ${localfile} ${SSHUser}@${SSHServer}:${RemotePath}

sftp -q -o StrictHostKeyChecking=no -i identtify_file -P ${Port} ${SSHUser}@${SSHServer}:${RemotePath} <<< "put ${localfile}"

禁止ssh连接超时

客户端修改/etc/ssh/ssh_config或者~/.ssh/config

ServerAliveInterval 100

服务端修改/etc/ssh/sshd_config

ClientAliveInterval 30
TCPKeepAlive yes
ClientAliveCountMax 99999

重启ssh服务

sudo service ssh restart

重置只读变量

readonly HISTSIZE=0

cat << EOF| sudo gdb
attach $$
call unbind_variable("HISTSIZE")
detach
EOF

密钥格式pkcs8转换为pkcs1

openssl pkcs8 -in ca0.key -passin pass:xxxx -outform PEM -out ca1.key
openssl rsa -in ca1.key -out ca2.key -aes256 -passout pass:xxxx

压缩包加密

zip -rP ${pwd} filename.zip filename
unzip -P ${pwd} filename.zip

抓包网卡流量

tcpdump -i eth0 port <port> -w <file>

# 抓包DNS查询
tcpdump -i any -n -w <file> port domain

查看shell所有颜色

for i in {0..7}; do
	for j in {0..7}; do
		echo -en "\033[${f};3${i};4${j}m${f};3${i};4${j}\033[0m"
	done
	echo
done

shell显示循环滚动字符

# '-','\','|','/'
echo -n ' '; while :; do
	for i in {'','','','','','','','','',''}; do
		echo -e -n "\b${i}"
		sleep 0.1s
	done
done

@yougg
Copy link
Author

yougg commented Apr 4, 2022

Windows

查看进程环境变量PowerShell

(Get-Process -id %PID%).StartInfo.EnvironmentVariables

挂载/卸载分区

:: 查看分区信息
mountvol /?

:: 卸载分区
mountvol D: /D

:: 挂载分区
mountvol D: \\?\Volume{e316bd82-2441-11e5-9656-d6d12ffbbef0}

MingW配置/etc/profile

export PS1='\e[35;40m[\w]\e[0m\$ '

alias clear=clsb
alias reset=clsb
alias ls='/bin/ls --color=auto'
alias l='ls -AlF'
alias vi=vim

创建服务

sc create xxxxx type= "own" start= "auto" error= "normal" binPath= "D:\xxxxx.exe" tag= "no" depend= "RpcSs" DisplayName= "xxxxx" obj= "LocalSystem"

sc delete xxxxx

@yougg
Copy link
Author

yougg commented Apr 4, 2022

编程语言

Go

单元测试

盖率报告
go test -cover -coverprofile=cover.out -covermode=count
go tool cover -html cover.out -o cover.html
测试包
# 测试当前包
go test .
# go test会在GOROOT和GOPATH下查找这个包并执行包的测试
go test fmt
# 测试整个包
go test github.com/yougg/pool/...

静态链接

引用了net包的程序, 编译时默认会动态链接

CGO_ENABLED=0 go build -installsuffix cgo yourcode.go
# 首先需要静态链接标准库中的net包
go install -tags netgo -a -v std
# 然后再编译自己的程序
CGO_ENABLED=0 go build yourcode.go

使用gccgo静态编译引用了cgo代码的程序

# static cgo
go build --ldflags '-extldflags "-static"' yourcode.go
# gccgo
go build -compiler gccgo --gccgoflags "-static" yourcode.go

Python

域名解析

#!/usr/bin/env python
#coding=utf8

import socket
import traceback

def resolv(url):
    try:
        while True:
            socket.getaddrinfo(url, 80, 0, socket.SOCK_STREAM)
            s = socket.socket()
            s.connect((url, 80))
            s.close()
    except Exception as e:
        print "Exception: %s\n" % e
        traceback.print_exc()

resolv("domain.example.com")

@yougg
Copy link
Author

yougg commented Apr 4, 2022

Tool/Software/Middleware

MySQL

启用用户远程连接权限

grant all privileges on *.* to ${username}@${IP} identified by '${pwd}' with grant option;
flush privileges;

超级权限

grant super on *.* to 'my_user'@'localhost';
revoke super on *.* from 'my_user'@'localhost';

连接数据库

mysql -h ${IP} -P ${Port} -D ${database} -u ${user} -p${pwd}

本地MySQL调试

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pa55w0rd --name DB mysql
docker exec -it DB bash
mysql -u root -ppa55w0rd

Docker

从镜像复制文件到其他镜像

COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord

列出镜像ID、摘要

docker images -f=reference='registry.example.com/*/*' --format "table {{.Repository}}:{{.Tag}}\t{{.Digest}}\t{{.CreatedSince}}"

docker image ls --format "table {{.Repository}}:{{.Tag}}\t{{.Digest}}\t{{.CreatedSince}}" registry.example.com/group*/*
docker image ls --format "{{.Repository}}:{{.Tag}}" registry.example.com/group*/*

IDE/Editor

编译Webkit版本的LietIDE

sudo apt install qt4-qmake libqt4-dev libqtwebkit-dev
go get github.com/visualfc/gotools
cd ${SRC}/liteide/build/
chmod +x build_linux_qt4_webkit.sh
./build_linux_qt4_webkit.sh
cp -rf liteide ~/

Emacs Shortcut

image
image
image

Git

切换远程仓库路径

git remote set-url origin https://github.com/user/FORK.git

添加子模块

cd parent_project
git submodule add https://github.com/username/subproject.git external/src
git submodule update --init --recursive external/src
cd external/src
git checkout -b develop origin/develop
cd ..
git add .
git commit -m 'Add submodule'
git push

同步子模块

git submodule update --init --force --recursive

删除子模块

  1. Delete the relevant section from the .gitmodules file.
git config -f .gitmodules --remove-section submodule.src

Stage the .gitmodules changes git add .gitmodules

  1. Delete the relevant section from .git/config.
git config --remove-section submodule.src
  1. Delete submodule and folder
git rm --cached path_to_submodule
rm -rf .git/modules/path_to_submodule
git commit -m "Removed submodule <name>"
  1. Delete the now untracked submodule files
rm -rf path_to_submodule

git克隆保存用户/密码

git clone http://username:password@github.com/user/project.git

git指定提交时的作者和邮箱

GIT_AUTHOR_NAME="some one" GIT_AUTHOR_EMAIL="someone@email.com" GIT_COMMITTER_NAME="some one" GIT_COMMITTER_EMAIL="someone@email.com" git commit -m 'message'
git push -f --set-upstream origin master

配置git diff使用vimdiff

git config --global diff.tool vimdiff
git config --global difftool.prompt false
git config --global alias.d difftool

使用方式

git difftool
git d

如果执行报错

Can't locate Error.pm in @inc ......

则执行如下方式后再操作

cpan Error.pm

或者

sudo perl -MCPAN -e 'install Error'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment