Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Last active July 28, 2022 22:23
Show Gist options
  • Save ryuheechul/72aa19933d52b5d1085519dafa4ecb20 to your computer and use it in GitHub Desktop.
Save ryuheechul/72aa19933d52b5d1085519dafa4ecb20 to your computer and use it in GitHub Desktop.
Useful commands
#!/usr/bin/env bash
url='https://github.com' # or whatever url you want to check
sec=3
while true; do
date -u +%H:%M:%S # or date +%H:%M:%S
echo "${url}"
# https://unix.stackexchange.com/a/84820/396504
curl -Is "${url}" | head -n 1 && sleep "${sec}"
done
# there is also https://github.com/megaease/easeprobe and https://k6.io/docs/getting-started/running-k6/
# find a process using port 3000 via https://unix.stackexchange.com/a/106562
sudo lsof -n -i :3000 | grep LISTEN
# export env vars from a file via https://github.com/sunghyunzz
export $(cat .env | xargs)
# check the reachability of the website every 1 seconds
while true; do curl -I https://example.com/; sleep 1; done
# pipe stdout as one arg to another command
echo 1 2 3 | xargs -0 -I {} ./my-script.sh {} # == ./my-script.sh "$(echo 1 2 3)"
# get the first column seperated by space
docker images | cut -d' ' -f1
# scriptize stdin
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
# trim front and back
echo " please trim " | xargs echo -n
# run command over seperated by space
echo "arg1 arg2" | tr ' ' '\0' | xargs -0 cat
# run command over seperated by newline
echo "arg1\narg2" | tr '\n' '\0' | xargs -0 cat
## an example for run command over text chunks
kubectl get pod | grep OutOfcpu | cut -d' ' -f5 | xargs echo -n | tr ' ' '\0' | xargs -0 kubectl delete pod
## pipe stdin to json
echo bar | jq -R {"foo":.}
# example: https://github.com/ryuheechul/atlaminitlab/blob/479b26ddf453b39de20aa0d733a3efa1cb64a9b8/terraform/gitlab.tf#L8
## getting names of k8s namespaces
kubectl get ns -o jsonpath='{.items[*].metadata.name}'
## watch cue file change and export for convenient debugging
ls a.cue | entr -c cue export --out=yaml /_
## watch changes as a output file to avoid flickering by clear
ls a.cue | entr cue export /_ > a.json
less +F a.json # in a saperate shell instance - https://unix.stackexchange.com/a/41281/396504
## list open ports - https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/
sudo lsof -i -P -n | grep LISTEN
## list open ports on mac - https://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x
sudo lsof -iTCP -sTCP:LISTEN -n -P
## quickly run a container to debug networ connectivity - https://github.com/nicolaka/netshoot
kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bash
## verify remote ip and port is reachable
nc -v [ip] [port]
## get ip address of the host inside the host - https://opensource.com/article/18/5/how-find-ip-address-linux
hostname -i
## delete comments - https://unix.stackexchange.com/questions/229275/sed-remove-all-comments-in-a-file
sed -e '/^[ \t]*#/d'
## filter empty lines - https://www.folkstalk.com/2012/02/delete-empty-lines-using-sed-grep.html
grep -v '^$' file.txt
## early exit in case required dependency like `jq` is not found
if test -z "$(command -v jq)"; then
echo 'you will need `jq` to continue'
exit 1
fi
## get file path of all running process
ps aux | awk '{print $2}' | tail -n +2 | xargs pwdx # optionally add `sudo` in front of xargs
## nohup the "right" way - https://stackoverflow.com/a/10408906/1570165
nohup command </dev/null >/dev/null 2>&1 & # completely detached from terminal
#!/usr/bin/env bash
BASEDIR=$(dirname "$0")
export $(cat $BASEDIR/.env | xargs)
exec $@
## read a file and export as env and then run command
## usage: `$ with-env.sh echo "ENV_VAR_1"`
## example .env: `ENV_VAR_1=VAR_CONTENT_1`
@ryuheechul
Copy link
Author

export TARGET_IP=x.x.x.x

ncat --sh-exec "ncat ${TARGET_IP} 22" -l 3322 --keep-open

@ryuheechul
Copy link
Author

ryuheechul commented May 28, 2019

@ryuheechul
Copy link
Author

ryuheechul commented Jan 18, 2020

@ryuheechul
Copy link
Author

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