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 / 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 / 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 / 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 / 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 / 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 / ssh_config.md
Created January 16, 2018 06:21
insecure ssh configuration #ssh #dsa
@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 / 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 / swap.md
Created January 18, 2018 00:42
creating swap partition #unix #swap

creating swap

This is how to format swap partition

mkswap /dev/data/swap

shell output:

mkswap: /dev/data/swap: warning: don't erase bootbits sectors

on whole disk. Use -f to force.

@ipan
ipan / screen.md
Created January 18, 2018 00:48
screen or byobu scroll up and down #screen #byobu #scroll

scroll up and down

Ctrl-A [

This will activate copy mode in GNU/screen. Now, you can scroll up/down and look at your data. Use the following keys:

  1. Ctrl-u and Ctrl-d scroll the display up/down by the specified amount of lines while preserving the cursor position. (Default: half screen-full).
  2. Ctrl-b and Ctrl-f scroll the display up/down a full screen.