Skip to content

Instantly share code, notes, and snippets.

View ipan's full-sized avatar

Ivan Pan ipan

  • Thermo Fisher Scientific, Inc.
  • San Francisco, CA
View GitHub Profile
@ipan
ipan / query-cpu.sh
Created January 16, 2018 06:42
query the number of CPU cores #nproc #lscpu
$ nproc
32
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
@ipan
ipan / port_binding.md
Created January 16, 2018 06:31
find out what process is binding the port #lsof #netstat #unix

find out what process is binding the port 4000 with lsof

lsof -i tcp:4000

and use netstat

netstat -lnt -p tcp
@ipan
ipan / ssh_config.md
Created January 16, 2018 06:21
insecure ssh configuration #ssh #dsa
@ipan
ipan / jq.sh
Last active January 16, 2018 06:33
formatting json files with jq #json #jq
# jq can not write back to the same file
for i in `find . -name '*.json'`
do
jq '.' $i > $i.tmp
mv $i.tmp $i
done
# oneliner version
# for i in `find . -name '*.json'`; do jq '.' $i > $i.tmp && mv $i.tmp $i; done
@ipan
ipan / add_newlines.sh
Last active January 16, 2018 06:34
add new lines to json files when there is no newline char #git #ls-files #read
# only add when the json file does not have new line
git ls-files | grep json | while read f; do tail -n1 $f | read -r _ || echo >> $f; done
# https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file
# https://unix.stackexchange.com/questions/192786/what-is-the-meaning-of-read-r
@ipan
ipan / sed_append.md
Created January 16, 2018 06:00
using sed to append #sed #unix

Using sed to append something after search pattern.

sed -i '/PATTERN/a\APPENDED' <file>

For example, this will add "sample": "sample x", after "flows": .*, to test_inactive.json

sed -i '/"flows": .*,/a\ "sample": "sample x",' test.json

@ipan
ipan / mkdir.py
Last active January 16, 2018 05:42
python: mkdir -p #unix
"""
For Python ≥ 3.2, os.makedirs has an optional third argument exist_ok that, when true, enables the mkdir -p functionality -- unless mode is provided and the existing directory has different permissions than the intended ones; in that case, OSError is raised as previously.
"""
import errno
import os
def mkdir_p(path):
try:
os.makedirs(path)
@ipan
ipan / ssh_config.md
Created January 16, 2018 05:26
ssh config: ProxyJump #ssh #mac

Example config for mac only

Host bastion
  Hostname ssh-bastion.example.com
  IdentityFile ~/.ssh/bastion_ssh.key
  User bastion-user

# internal server on private network
Host 172.18.* 192.168.*

ProxyJump bastion

@ipan
ipan / xcode.md
Created January 16, 2018 05:10
Xcode command line tools #xcode

Accepting licenses

sudo xcodebuild -license accept
@ipan
ipan / rsync.md
Created January 16, 2018 05:06
rsync copy only *.sh in subdirectory #rsync