Skip to content

Instantly share code, notes, and snippets.

@Cenness
Last active June 24, 2024 09:37
Show Gist options
  • Save Cenness/cc8a962d279908037e7d to your computer and use it in GitHub Desktop.
Save Cenness/cc8a962d279908037e7d to your computer and use it in GitHub Desktop.
bash oneliners
systat -ifstat 1
netstat -tulpn | grep :80
chcon -R -t httpd_sys_content_t /path/to/file
semanage port -a -t http_port_t -p tcp %port%
nmap %uri% -p- -PN
## scan subnet
nmap -sP 192.168.1.0/24
## check port with bash
host=google.com; port=443; timeout 1 bash -c "(</dev/tcp/$host/$port) >/dev/null 2>&1 && echo 'Up'" || echo 'Down'
mysqldbcopy --source=root:pass@localhost --destination=root:pass@localhost source_db:target_db
## send mail with heirloom-mailx via external smtp server
mail -s "something happened at $(hostname -f)" -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtps://smtp.yandex.ru:465 -S from="user@domain.tld" -S smtp-auth-user=user@domain.tld -S smtp-auth-password=PASSWORD -a /opt/whatever/something.txt "another@user.tld" <<EOF
See attached.
EOF
## send mail with heirloom-mailx in monospace
mail -s "$(echo -e 'SUBJECT\nContent-Type: text/html')" "another@user.tld" <<EOF
<pre style="font-size: 1.3em;">
$(cat /opt/whatever/messsage.body)
</pre>
EOF
## exclude dir from find
find $path/ -not \( -path $path/ignore1 -prune \) -not \( -path $path/ignore2 -prune \) -name '*.jpg' -type f -exec chmod 644 {} \;
## recursively find from /
find / -name "file_name"
## recursively md5
find . -type f -print0 | xargs -0 md5sum | md5sum
## recursively md5 and sort by path
find . -type f -print0 | xargs -0 md5sum | sort -k 2
## add all files in dir to gitignore
find . -type f -follow -print | grep -Ev "\.\/\.git" | cut -c 2- > /whatever/.git/info/exclude
## recursively find file containing string
cd /path/to/dir
grep -r "foo" .
## or
grep -r -l "foo" /path/to/dir/*.c
grep -A[fter] N -B[efore] M "foo"
## append to line
sed -i '/^127.0.0.1/ s/$/ what.ever/' file
sed -i '1 s/$/ what.ever/' file
## append after a line
awk '1;/line/{print "added line"}' file > file_a
## substring by lines for file
sed -n '/startpattern/,/endpattern/p' inputfile > outputfile
## remove all between the lines
sed -n '1,/start/p;/end/,$p' test
## including the lines
sed '/start/,/end/d' test
## space to carriage return
tr ' ' '\012'
## pretty print array
arr=("first element"
"second"
"and finally, third"
)
join () { local IFS; IFS="$1"; shift; echo "$*";}
pp() { local ref_a; declare -n ref_a="$@"; join \| "${ref_a[@]}" | sed 's/|/; /g';}
pp arr>first element; second; and finally, third
### same, but input is multiline text
echo -e "\nfirst element\n\nsecond\nand finally, third\n" | jq -nrR '[inputs | select(length>0)] | join("; ")'
>first element; second; and finally, third
## get subfolders from a huge repo
git clone -n --depth 1 --filter=tree:0 https://github.com/huge/repo.git
cd ./repo || exit 1
git sparse-checkout set --no-cone dir1/subdir1 dir2/subdir2
git checkout
## post json with vars, without jo
params='{ "ref": "%s", "variables": [{"key": "%s", "value": "%s"}]}'
curl -s -X POST -H "TOKEN: ${TOKEN}" -H "Content-Type: application/json" \
-d "$(printf "$params" "$TAG" "$VAR_NAME" "$VAR_VALUE")" \
"https://endpoint.tld/api/"
## url encode string
jq -rn --arg x "$STRING" '$x|@uri'
## import env file
eval $(grep -v '^#' envfile | sed 's.[ ]*=[ ]*.=.g' | xargs -d'\n' -n1 | sed 's.^.export .');
## convert seconds to HH:MM:SS, limits: 0-86399
date -d@$time_in_sec -u +%H:%M:%S
## use float math
python -c "print(format(max(min((1.*$foo)/$bar,86399),0),'.0f'))"
## remove from A anything that is in B
comm -23 <(sort A) <(sort B) > S
## without sorting
grep -Fvxf B A
## recursively find latest modified file from current directory
find . -type f -printf '%T+ %p\n' | sort -n | tail -1
## copy dir structure from current dir into /new/folder
find . -type d -exec mkdir -p -- /new/folder/{} \;
$(cat) is a shorthand for $(cat /dev/stdin)
for n in {8..1};do echo $n;done
## check pipe segment status
func1 | ( func2 && func3 )
return_codes=( "${PIPESTATUS[@]}" )
if (( return_codes[0] != 0 || return_codes[1] != 0 )); then
echo "error message" >&2
fi
## require function for script dependencies
require(){ hash "$@" 2>/dev/null || { echo "$@ is missing"; exit 127;};}
require jq
## pull all present images
docker images --format "{{.Repository}}:{{.Tag}}" | grep -v '<none>' | xargs -L1 docker pull
## average memory consumption of httpd processes
ps -ylC apache2 --sort:rss | awk '{sum+=$8; ++n} END {print "Tot="sum"("n")";print "Avg="sum"/"n"="sum/n/1024"MB"}'
lynx -dump http://localhost:82/server-status | grep -B 1 $vhost | awk '{print $2;}' | grep -o '[0-9]\+'
## save tmux buffer
Ctrl+b [
Ctrl+Space
Ctrl+w
Ctrl+b :
save[tab] filename
## restart specific container in a pod
oc exec -it $POD -c $CONTAINER -- kill -INT 1
## get pods running with default SA
oc get pods --all-namespaces -o=custom-columns="name:.metadata.name,namespace:.metadata.namespace,SA:.spec.serviceAccountName" | grep -E 'default|<none>'
## grep k8s objects by content
### configmaps
kubectl get cm --all-namespaces -o jsonpath='{range .items[*]}{"{"}"name": "{.metadata.name}", "ns": "{.metadata.namespace}", "data": {.data}{"}"}{"\n"}{end}' | sed '/"data": }/d' > c.json;
cat c.json | jq -r '[.name, .ns, (.data[]?)] | @tsv' | grep 'something'
### secrets
kubectl get secrets --all-namespaces -o jsonpath='{range .items[?(@.type=="Opaque")]}{"{"}"name": "{.metadata.name}", "ns": "{.metadata.namespace}", "data": {.data}{"}"}{"\n"}{end}' | sed '/"data": }/d' > s.json;
cat s.json | jq -r '[.name, .ns, (.data[]? | @base64d)] | @tsv' | grep 'something'
## save and import images
docker save $(docker images | sed '1d' | awk '{print $1 ":" $2}' | grep -vE 'but|not|these') -o allinone.tar
docker load -i allinone.tar
## nexus -> nexus
export target="nexus.target.com"; export source="nexus.source.com"
export tag="$(echo image=tag | tr '=' ':')"; time (docker pull $source/$tag && docker tag $source/$tag $target/$tag && docker push $target/$tag)
docker login -p $(oc whoami --show-token) -u unused docker-registry-default.CLUSTER_URI
## export table from a confluence article to a csv
curl https://$CONFLUENCE/rest/api/content/$(curl $ARTICLE_URI 2>/dev/null | grep 'page-id' | head -n1 | cut -d'"' -f4)?expand=body.storage | jq .body.storage.value | sed 's/""/"/g' | sed 's|"\\|"|g' | sed 's|\\"|"|g' | sed 's|>"|>|g' | sed 's|"<|<|g' > hmm.htlm
xidel hmm.htlm -se '//tr / join(td, ",")' | cut -d',' -f2,5
## stop and remove running containers older than 24h
docker ps --format='{{.ID}}' | xargs -n 1 -r docker inspect -f '{{.ID}} {{.State.Running}} {{.State.StartedAt}} {{.Name}}' | grep -v k8s_ | awk '$2 == "true" && $3 <= "'$(date -d "@$(($(date +%s) - 86400))" -Ins --utc | sed 's/+0000/Z/')'" { print $1 }' | xargs -r docker stop | xargs -r docker rm
## return random item from list
RANDOM=$(date +%s)
states=(pending running running running running running running running success success success failed)
echo ${states[$RANDOM % ${#states[@]}]}
## prometheus time intervals
### only workdays
scalar(clamp(day_of_week() > 0 and day_of_week() < 6, 1, 1)) * query
### only active hours, e.g. 07:00 -> 22:59 UTC
scalar(clamp(hour() > 7 and hour() < 23, 1, 1)) * query
## create swap file
rand="_$(cat /dev/urandom | tr -dc 'a-z' | fold -w 4 | head -n 1)"
### fallocate is faster, but may not work with some fs - etx4 is fine
## dd if=/dev/zero of=/swapfile${rand} bs=1M count=2048
fallocate -l 2G /swapfile${rand}
chmod 600 /swapfile${rand}
mkswap /swapfile${rand}
swapon /swapfile${rand}
echo "/swapfile${rand} none swap sw 0 0" >> /etc/fstab
## free page cache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
## Check what user can access
## FreeBSD
## Check readable
su -m httpd -c /usr/local/bin/bash
find / -type d \( -name dev \) -prune -o -exec test -r {} \; -exec echo {} is readable \; 2>/dev/null
## Check writable
su -m httpd -c /usr/local/bin/bash
find / -type d \( -name dev \) -prune -o -exec test -w {} \; -exec echo {} is writable \; 2>/dev/null
## Linux
## Check readable
su -l www-data
find / -type d \( -wholename '/dev' -o -wholename '/proc' -o -wholename '/sys' \) -prune -o -writable -print
## Check writable
su -l www-data
find / -type d \( -wholename '/dev' -o -wholename '/proc' -o -wholename '/sys' \) -prune -o -readable -print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment