User Tools

Site Tools


scripting:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
scripting:bash [2018/09/24 20:44] jotasandokuscripting:bash [2023/11/02 14:38] (current) – external edit 127.0.0.1
Line 1: Line 1:
-**BASH**+BASH CONDITIONS:
  
-This is an example of a very simple script: +  # run ssh agent in /etc/bashrc 
-https://docs.google.com/document/d/1uNUGXkHmb9jvHA4Y12nTXEDSOjkLzzVoAcHkSVwxNmw/edit?usp=sharing+  if [ ! -S ~/.ssh/ssh_auth_sock ]; then # if file does('nt) exist and is socket 
 +    eval `ssh-agent` 
 +    ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock 
 +  fi 
 +  ssh-add -l > /dev/null || ssh-add 
 +  #run ssh agent 
 +  ssh-add ~/.ssh/* >/dev/null 2>&1
  
 +__EXAMPLE OF ssh/config FILE__:\\
 +  $ cat .ssh/config 
 +  Host *
 +    AddKeysToAgent yes
 +    #UseKeychain yes
 +    ServerAliveCountMax 6
 +    ServerAliveInterval 10
 +    TCPKeepAlive yes
 +    ForwardAgent yes
 +    Compression yes
 +    GatewayPorts yes
 +    StrictHostKeyChecking no
 +    User jaime.santos.amandi
 +  
 +  ## This is saying for anything for those IP ranges or for domains  myorgdatacloud.com OR myorgdatacloud.com EXCEPT anything starting with bastion-62
 +  ## use socks proxy over that host: bastion-lhr.myorgdatacloud.com
 +  Host 10.32.* 10.33.* *.myorgdatacloud.com *.bigshot.co.uk !bastion-62-*
 +    ProxyCommand ssh bastion-lhr.myorgdatacloud.com -W %h:%p
  
-http://www.freeos.com/guides/lsst/while 
  
-Flow control: http://linuxcommand.org/wss0090.php 
  
-http://www.ieor.berkeley.edu/~faridani/bash.htm <-- notes 
  
-$# represents the number of arguments: +----
-\\ +
-\\ +
-**OR & AND OPERATORS FOR PROCESS FLOW "&& and || "** \\ +
-The right side of && will only be evaluated if the exit status of the left side is zero. || is the opposite: it will evaluate the right side only if the left side exit status is nonzero. You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise. +
-  (ip route add 10.0.0.0/8 via 10.20.3.5 dev eth0 ; echo 'successful'; sleep 30 && ip route del 10.0.0.0/8 via 10.20.3.5 dev eth0) +
-  ping -c 1 $router_ip &>/dev/null || exit 0+
  
-\\ +BASH CONDITIONS:\\
-IF +
-Comparisons: The following flags (preceded by minus), represent  +
-  * eq equal to +
-  * ne not equal to +
-  * lt less than +
-  * le less than or equal to +
-  * gt greater than +
-  * ge greater than or equal to+
  
-File Operations: The following flags (preceded by minus), represent :\\ +  
-  * s file exists and is not empty-f file exists and is not a directory +
-  * d directory exists +
-  * x file is executable +
-  * w file is writable +
-  * r file is readable +
-\\+
 The tests below are test conditions provided by the shell: The tests below are test conditions provided by the shell:
  
Line 58: Line 59:
   * G file = True if the file exists and is owned by the effective group id.   * G file = True if the file exists and is owned by the effective group id.
 \\ \\
-  * +
   * file1 –nt file2 = True if file1 is newer, by modification date, than file2.   * file1 –nt file2 = True if file1 is newer, by modification date, than file2.
   * file1 ot file2 = True if file1 is older than file2.   * file1 ot file2 = True if file1 is older than file2.
Line 69: Line 70:
   * expr1 –a expr2 = True if both expr1 and expr2 are true.   * expr1 –a expr2 = True if both expr1 and expr2 are true.
   * expr1 –o expr2 = True is either expr1 or expr2 is true.    * expr1 –o expr2 = True is either expr1 or expr2 is true. 
 +
 +
 +----
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +This is an example of a very simple script:
 +https://docs.google.com/document/d/1uNUGXkHmb9jvHA4Y12nTXEDSOjkLzzVoAcHkSVwxNmw/edit?usp=sharing
 +
 +
 +http://www.freeos.com/guides/lsst/while
 +
 +Flow control: http://linuxcommand.org/wss0090.php
 +
 +http://www.ieor.berkeley.edu/~faridani/bash.htm <-- notes
 +
 +$# represents the number of arguments:
 +\\
 +\\
 +**OR & AND OPERATORS FOR PROCESS FLOW "&& and || "** \\
 +The right side of && will only be evaluated if the exit status of the left side is zero. || is the opposite: it will evaluate the right side only if the left side exit status is nonzero. You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.
 +  (ip route add 10.0.0.0/8 via 10.20.3.5 dev eth0 ; echo 'successful'; sleep 30 && ip route del 10.0.0.0/8 via 10.20.3.5 dev eth0)
 +  ping -c 1 $router_ip &>/dev/null || exit 0
 +
 +\\
 +IF
 +Comparisons: The following flags (preceded by minus), represent 
 +  * eq equal to
 +  * ne not equal to
 +  * lt less than
 +  * le less than or equal to
 +  * gt greater than
 +  * ge greater than or equal to
 +
 +File Operations: The following flags (preceded by minus), represent :\\
 +  * s file exists and is not empty-f file exists and is not a directory
 +  * d directory exists
 +  * x file is executable
 +  * w file is writable
 +  * r file is readable
 +
  
 ---- ----
Line 82: Line 136:
  
 **IN CLI (SINGLE LINE)  - One-liner** **IN CLI (SINGLE LINE)  - One-liner**
 +
 +  for s in ALL_SERVERS ; do ssh $s '/usr/whatever' ; done
 +
 +Apply to multiple hosts with no need for a script:
 +  for i in admin-worker00.dc.mycompany1.co.uk \
 +  centosGUI.dc.mycompany1.co.uk \
 +  dbtest.dc.mycompany1.co.uk \
 +  redis-admin.dc.mycompany1.co.uk \
 +  security-scanner01.dc.mycompany1.co.uk \
 +  test.dc.mycompany1.co.uk \
 +  test01.dc.mycompany1.co.uk ; \
 +  do ssh root@"$i" "ping -c 1 10.33.1.4"  ; done 
 +  
 +
  
   conns=`ps aux|grep java|egrep '(IM|IDS)'|awk '{print $2}'`;for i in $conns;do netstat -tulpan|grep $i|grep "ESTABLISHED"|egrep '(10.23.54.5|10.23.47.196)';done   conns=`ps aux|grep java|egrep '(IM|IDS)'|awk '{print $2}'`;for i in $conns;do netstat -tulpan|grep $i|grep "ESTABLISHED"|egrep '(10.23.54.5|10.23.47.196)';done
Line 92: Line 160:
 **EXECUTE SSH COMMAND IN A SET OF DEVICES FOR LOOP**:\\ **EXECUTE SSH COMMAND IN A SET OF DEVICES FOR LOOP**:\\
   for host in $(seq --format='cc%02.0f' 01 17); do ssh $host "/sbin/ip link show|egrep '(em|eth|bond)[0-9]:' | cut -d: -f 2"| xargs -n 1 ssh $host ip link set mtu 9000 dev ; done   for host in $(seq --format='cc%02.0f' 01 17); do ssh $host "/sbin/ip link show|egrep '(em|eth|bond)[0-9]:' | cut -d: -f 2"| xargs -n 1 ssh $host ip link set mtu 9000 dev ; done
 +
 +
 ---- ----
 +
  
 **FOR**\\ **FOR**\\
Line 104: Line 175:
 To automatically **generate configurations** (configuration generator) from a single line in the CLI. See this example: To automatically **generate configurations** (configuration generator) from a single line in the CLI. See this example:
   for i in {1..6}; do echo -e "conf t \n int fa0/0 \n ipv6 address 2001:150:1:$i::$i/128 \n no shut \n exit \n exit \n wr mem"; done   for i in {1..6}; do echo -e "conf t \n int fa0/0 \n ipv6 address 2001:150:1:$i::$i/128 \n no shut \n exit \n exit \n wr mem"; done
 +  for f in *.HEIC; do echo "converting file $f"; heif-convert $f $f.jpg; done  # multiple commads (convert HEIC)
  
   #!/bin/bash   #!/bin/bash
Line 110: Line 182:
   echo -e "   echo -e "
   Host $val1   Host $val1
-       HostName $val1.dc.grapeshot.co.uk+       HostName $val1.dc.mycompany1.co.uk
        User jaime_santos        User jaime_santos
        Port 22";        Port 22";
Line 158: Line 230:
  
 ---- ----
 +**BASH IDIOMS:**\\
 +Redirection ( stdout , stderr ). All files have an id (descriptor) but also! stdout1 , stderr2 habe ids!.
 +  cat foo.txt 1> output.txt  same as cat foo.txt 1> output.txt
 +  cat foo.txt 2> output.txt   # only the error messages 
 +  ls foo > /dev/null 2>&    # 2>&1 you are basically saying “Redirect the stderr to the same place we are redirecting the stdout”.   & is like the ind
 +  foo.txt > output.txt 2>&  # out and err both to file and and  screen
 +   > /dev/null 2>&          # for the cron jobs. redirecting standard output into /dev/null, which is a place you can dump anything you don�t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).
 +   > /dev/null 2>&         # This sends BOTH stdout and stderr to dev null
 +   
 +
 +  cat << EOF > file.txt
 +  [text]   
 +  EOF
 +
 +This is an interesting way of generating a json file with a bash script using EOF (inside an ansible facts file):
 +  #!/bin/bash
 +  user=$(who | cut -f 1 -d " " | tail -1 )
 +  ipuser=$(who -u | cut -f 2 -d "(" | cut -f 1 -d ")" | head -1) 
 +  
 +  cat << EOF 
 +  {
 +    "user" : "$user",
 +    "ipuser" : "$ipuser"
 +  } 
 +  
  
-Output redirection 
-  $ /dev/null 
 \\ \\
 Increment variable Increment variable
Line 168: Line 263:
   num=`expr $num1 + $num2`   num=`expr $num1 + $num2`
 \\ \\
 +
 +----
  
 **REGULAR EXPRESSIONS** **REGULAR EXPRESSIONS**
Line 283: Line 380:
  
 **TO EVALUATE COMMANDS REMOTELY (SSH)** **TO EVALUATE COMMANDS REMOTELY (SSH)**
-  ssh root@jaguar27.dc.grapeshot.co.uk "/usr/bin/sed -i '/^IPADDR=/d' /etc/sysconfig/network-scripts/ifcfg-bond0"+  ssh root@jaguar27.dc.mycompany1.co.uk "/usr/bin/sed -i '/^IPADDR=/d' /etc/sysconfig/network-scripts/ifcfg-bond0"
      
  
scripting/bash.1537821895.txt.gz · Last modified: (external edit)