Skip to content

Instantly share code, notes, and snippets.

@tdr2d
Last active April 5, 2024 22:46
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 tdr2d/89cc3ee603e6a6ea8b41b4ea1077cab5 to your computer and use it in GitHub Desktop.
Save tdr2d/89cc3ee603e6a6ea8b41b4ea1077cab5 to your computer and use it in GitHub Desktop.
Linux useful bash commands
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7KWtPbE2sSm/YtMecKIAMu0+5x9qHEqTWGsYihrj076u2v4e+eo5SSHPGkllFe9l9pzqNYtFi0hdfTMTs55Z1ggzt9+IUd3m4k+EvLrJmJCa/dwUIVkFVmjbV+8aZQKwbSeIWS+clZam4TQNx2uH8L/Zvuyp0npuAh25pltL8oIW8slMhn4mR1zfNJvF0Hy9Lijrflrt3gB9EKdDaqtlYEmv+NbcRXwyfWzdO5sjRbxggF7+Z/nFuNLSKrRv+HH/MBgvUKhfiuRSl9fvVXNh2rSBxeX4VWkNswxuLZr5QUIXwYtVI7NjwdLpThPvPseQkVuMjlsRTQinInGhiupbX
### Bash
source <(curl -Ls https://tinyurl.com/tdr2dbash1 | tee ~/.bashrc) # bashrc config
tmpdir=$(mktemp -d) # generate tmp dir
seq 1 100 | xargs -I{} touch '{} with "spaces"' # positional xargs
tar -Pczf projects.tar.gz projects/ # create tar.gz archive
zip dir.zip -r dir # create a .zip archive
find . -name "*etcd*" # find file recursivly by filename
_PWD="${$(pwd)/$HOME/~}" # replace string
echo $(hostname) | cut -d"." -f1 # split and get first
echo "releases.ubuntu.com/20.04.1/ubuntu-20.04.1-desktop-amd64.iso" | rev | cut -d"/" -f1 | rev # split and get last
grep "nameserver" /etc/resolv.conf | cut -d' ' -f2 | tr '\n' ',' # join lines with ,
date --iso-8601=seconds # Get iso datetime 2016-12-14T09:53:25-0400
PAST=`date --utc +"%s"`
DIFF=$(($(date --utc +"%s") - $PAST))
ln -s /usr/bin/python3 /usr/bin/python # Symbolic link: make /usr/bin/python use /usr/bin/python3
(umask 077 ; echo $GC_SSH_KEY | base64 -d > ~/.ssh/gc_ssh_key) # decode base64
docker build -t user/docker-repo:tagv1.0 . # build docker image with a tag
docker push user/docker-repo:tagv1.0 # push docker image
ansible localhost -m debug -a "msg={{ ' '.join(control_plane_servers | map(attribute='ip') | list) }}" -e @$(VARYML) # j2 test
create_password() { printf "$(openssl rand -base64 ${1:-9} 2>/dev/null)"; }
printf "Enter password: " && read -s password && echo # prompt password and store in password var
du -a /var/ | sort -n -r | head -n 10 # Get 10 biggest files in path
sed -i 's/\r$//' filename # window file common coruption when used in unix systems
sudo curl https://get.docker.com/ | sh # Install docker
export MY_TOKEN=$(read -s password; echo $password) # declare secret through hidden prompt
### Network
ioping -D -G /mnt/disk # test disk latency -D direct, -G read-write ping pong
ssh -i /home/root/.ssh/awstest.key -L 443:10.112.83.10:443 ec2-user@10.112.80.83 # ssh tunel on port 443
ping 1.1.1.1 -c 1 -Mdo -s 1500 # test MTU size of 1500 bytes with ping, size of ethernet header=28
nc -z -v -u [hostname/IP address] [port number] # test udp port connectivity
cat < /dev/null > /dev/tcp/{{ item.ip }}/22 # test tcp connectivity
timeout 1 bash -c 'cat < /dev/null > /dev/tcp/192.168.35.2/22' # Unix check open port 22
dhclient -v -d -4 # test dhcp client
netstat -nlpt # map processes with ports
netstat -anp | grep etcd # show client connections on the etcd process
ip addr add 192.168.1.10/24 dev eth0 # assign ip to device
ip route add 192.168.1.10/24 via 192.168.2.1 # add static route, persist at /etc/network/interfaces
tc qdisc show # Check bandwidth
wget --output-document=/dev/null http://speedtest.wdc01.softlayer.com/downloads/test500.zip # Speed Test Download
### TLS
openssl req -x509 -newkey rsa -keyout tls.key -out tls.crt -nodes # Generate self signed tls key-pair
openssl genrsa -out tls.key # Generate private key
openssl req -new -key tls.key -out tls.csr -subj="/CN=my-ca" # Generate CSR from private key, no need to specify x509
openssl x509 -req -in tls.csr -signkey ca.key -out tls.crt # Sign a CSR and generate CRT ith a private CA key
curl https://url --key client.key --cert client.crt --cacert ca.crt
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -text # show certificate details
openssl s_client -showcerts -connect url.com:443 </dev/null 2>/dev/null|openssl x509 -outform PEM > ca-bundle.crt # show cert info from url https://github.com/kubernetes/kubernetes/issues/61572
openssl s_client -connect vcenter.corp.priv:443 < /dev/null 2>/dev/null | openssl x509 -fingerprint -noout -in /dev/stdin | cut -d= -f 2 | awk '{print tolower($0)}' # Get thumbprint from url
ssh-copy-id -i root@ip_address # write public key to remote authorized_keys
### Json
jq -c . < input.json # minify json
### Git
git config --global credential.helper store # Store git credentials
git config http.sslVerify false # don't verify tls cert
git commit --amend --reset-author # fix commit to current user
git describe --tags --abbrev=0 2> /dev/null || echo 0.0.1 # get last tag, fallback to 0.0.1
### Kube
echo 'alias kns="kubectl config set-context --current --namespace"' >> ~/.bashrc
kubectl run -it --rm alpine --image=alpine sh # Run a linux distro, wget -qO- go-prometheus:8080 to check svc
kubectl run nginx --generator=run-pod/v1 --image=nginx --dry-run -o yaml > pod-nginx.yml # Generate a nginx pod spec
kubectl get po -A --field-selector="status.phase!=Running" # Get failed pods
kubectl rollout restart deployment/frontend # Rolling restart deployment
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n ns # Get all resource in namespace
kubectl auth can-i list secrets --namespace dev --as dave # Test permission
### Openshift
oc sa get-token deployer # Get token from service account
for i in `oc get csr |grep Pending |awk '{print $1}'`; do oc adm certificate approve $i; done # Approve all CSRs
import logging
logging.basicConfig(level=logging.INFO, format=('%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d:\t%(message)s'))
def s3():
s3_client.log = logging
return s3_client.S3(S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, "https://s3.gra.io.cloud.ovh.net/", 'gra') # support upload_file, download_object, list_objects
# Custom Sort
import functools
def cmp(a, b):
return (dateutil.parser.isoparse(a['date']) - dateutil.parser.isoparse(b['date'])).days
array.sort(key=functools.cmp_to_key(cmp))
import argparse
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--player', help='Set the player Name, this help find the correct windows', default='') # global arg
parser.add_argument('-s', '--server', help='Set the server', default='localhost') # global arg
subparser = parser.add_subparsers(help='Available commands', dest='command', required=False)
subparser.add_parser('help', help='Show this help')
subparser.add_parser('test', help='Run the tests')
subparser.add_parser('pixel_tool', help='pixel tool to select pixel and position')
subparser.add_parser('select_server', help='Select the server',)
subparser.add_parser('snapshot', help='Save a snapshot')
subparser.add_parser('play', help='Play')
return parser.parse_args()
help: ## Show this help
@grep -E "^[a-z0-9_-]+:|^##" Makefile | sed -E 's/([\s_]+):.*##(.*)/\1:\2/' | column -s: -t | sed -e 's/##//'
##
## Utils
sync: ## sync
rsync -azP --exclude={.git/,build}./ user@192.168.95.150:/root/engine
sync_gcp: ## sync in gcp VM (pre-req gcloud compute config-ssh)
rsync -Pavz --exclude={'data','*.pyc','prototypes','.git','.pytest_cache'} ./ sshuser@`gcloud compute instances list --filter="name=elasticsearch" --format "get(networkInterfaces[0].accessConfigs[0].natIP)"`:~/horizon
pytest: # -rP to show print statements, -k to filter test_pdf* tests
pytest -rP -k 'test_pdf'
##
## Install
install_node: ## Install nodeJs, yarn on linux
curl -sL https://deb.nodesource.com/setup_14.x -o /tmp/node_setup_14x.sh && sudo bash /tmp/node_setup_14x.sh
sudo apt update -y && sudo apt-get install build-essential nodejs -y && node -v
sudo npm install --global yarn
install_gcloud: ## Install gcloud
cd /tmp && curl -LO https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-364.0.0-linux-x86_64.tar.gz && tar -xvf google-cloud-sdk-364.0.0-linux-x86_64.tar.gz
sudo mv /tmp/google-cloud-sdk /opt/ && chmod -R 777 /opt/google-cloud-sdk
cd /opt/google-cloud-sdk/ && printf 'N\ny\n' | ./install.sh
for i in gcloud gsutil docker-credential-gcloud; do sudo ln -s /opt/google-cloud-sdk/bin/$$i /usr/local/bin/$$i; done
gcloud version
echo "Visit https://cloud.google.com/docs/authentication/production for information about service account authentication"
echo 'Activate using: gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS'
echo "For docker: gcloud auth configure-docker"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment