Hero Image
Parse Command Line Arguments in Bash

Parse Command Line Arguments in Bash getopts getopts optstring opt [arg ...] #!/bin/bash while getopts 'abc:h' opt; do case "$opt" in a) echo "Processing option 'a'" ;; b) echo "Processing option 'b'" ;; c) arg="$OPTARG" echo "Processing option 'c' with '${OPTARG}' argument" ;; ?|h) echo "Usage: $(basename $0) [-a] [-b] [-c arg]" exit 1 ;; esac done shift "$(($OPTIND -1))" optstring represents the supported options. The option expects an argument if there is a colon (:) after it. For instance, if option c expects an argument, then it would be represented as c: in the optstring When an option has an associated argument, then getopts stores the argument as a string in the OPTARG shell variable. For instance, the argument passed to option c would be stored in the OPTARG variable. opt contains the parsed option. #!/bin/bash while getopts ':abc:h' opt; do case "$opt" in a) echo "Processing option 'a'" ;; b) echo "Processing option 'b'" ;; c) arg="$OPTARG" echo "Processing option 'c' with '${OPTARG}' argument" ;; h) echo "Usage: $(basename $0) [-a] [-b] [-c arg]" exit 0 ;; :) echo -e "option requires an argument.\nUsage: $(basename $0) [-a] [-b] [-c arg]" exit 1 ;; ?) echo -e "Invalid command option.\nUsage: $(basename $0) [-a] [-b] [-c arg]" exit 1 ;; esac done shift "$(($OPTIND -1))" Note that we’ve updated optstring as well. Now it starts with the colon(:) character, which suppresses the default error message. The getopts function disables error reporting when the OPTERR variable is set to zero. Parsing Long Command-Line Options With getopt #!/bin/bash VALID_ARGS=$(getopt -o abg:d: --long alpha,beta,gamma:,delta: -- "$@") if [[ $? -ne 0 ]]; then exit 1; fi eval set -- "$VALID_ARGS" while [ : ]; do case "$1" in -a | --alpha) echo "Processing 'alpha' option" shift ;; -b | --beta) echo "Processing 'beta' option" shift ;; -g | --gamma) echo "Processing 'gamma' option. Input argument is '$2'" shift 2 ;; -d | --delta) echo "Processing 'delta' option. Input argument is '$2'" shift 2 ;; --) shift; break ;; esac done -o option represents the short command-line options --long option represents the long command-line options

Hero Image
Tcpdump Usage Summary

Tcpdump Usage Summary Command usage tcpdump uses the command line. The command format is: tcpdump [ -AdDeflLnNOpqRStuUvxX ] [ -c count ] [ -C file_size ] [ -F file ] [ -i interface ] [ -m module ] [ -M secret ] [ -r file ] [ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ] [ -E spi@ipaddr algo:secret, ... ] [ -y datalinktype ] [ -Z user ] [ expression ] Simple option notes for tcpdump -E spi@ipaddr algo:secret , ... can decrypt IPsec ESP packets using spi@ipaddr algo:secret. The secret is the ESP key, expressed as an ASCII string. If it starts with 0x, the key is read as hex. In addition to the syntax above (spi@ipaddr algo:secret), you can append a syntax input filename for tcpdump to use (replace … in spi@ipaddr algo:secret, ... with a syntax filename). This file is opened when the first ESP packet arrives, so it is best to drop some privileges at that time (to reduce risk if the file is malicious). -T type forces tcpdump to analyze packets according to the protocol structure specified by type. Known type values include: aodv (Ad-hoc On-demand Distance Vector protocol, used in Ad hoc peer-to-peer networks) cnfp (Cisco NetFlow protocol) rpc (Remote Procedure Call) rtp (Real-Time Applications protocol) rtcp (Real-Time Applications control protocol) snmp (Simple Network Management Protocol) tftp (Trivial File Transfer Protocol) vat (Visual Audio Tool, an application-layer protocol used for video conferencing on the internet) wb (distributed White Board, an application-layer protocol for online meetings) Practical command examples Capture communication between host 210.27.48.1 and host 210.27.48.2 or 210.27.48.3