1. 2016-12-21 - Toggle bash history; Tags: Toggle bash history
    Loading...

    Toggle bash history

    The linux bash records your commands in the bash history. You can toggle it off and then back on again.

    Turn off

    set +o history
    

    Turn on

    set -o history
    
  2. 2016-08-06 - Check active hosts within ip range; Tags: Check active hosts within ip range
    Loading...

    Check active hosts within ip range

    I forgot the IP address of my Raspberry Pi, so I had to check which one is currently assigned to my Pi. nmap comes for that in handy.

    For the host discovery I use the simple option -sn to disable the port scan. The option P allows to specify the known DHCP IP range, that shorten the host discovery time. After that you may you see active hosts and its assigned IP.

    tan@omega:~$ nmap -snP 192.168.1.100-130
    Starting Nmap 7.01 ( https://nmap.org ) at 2016-08-05 21:51 CEST
    Nmap scan report for DNS-320.home (192.168.1.104)
    Host is up (0.00030s latency).
    Nmap scan report for android-36517fff78a8bc76.home (192.168.1.112)
    Host is up (0.061s latency).
    Nmap scan report for raspberry.home (192.168.1.121)
    Host is up (0.00052s latency).
    Nmap scan report for omega.home (192.168.1.123)
    Host is up (0.00024s latency).
    Nmap done: 31 IP addresses (4 hosts up) scanned in 1.61 seconds
    
  3. 2016-07-21 - Start bash in docker container; Tags: Start bash in docker container
    Loading...

    Start bash in docker container

    Replace id and there you go:

    sudo docker exec -i -t dd81eb497f43 /bin/bash
    

    Or use the image name

    docker run -ti ubuntu:latest /bin/bash
    
  4. 2016-07-14 - Check used files of process; Tags: Check used files of process
    Loading...

    Check used files of process

    I have to work recently with a Fraud Prevention and Detection solution and needed to check in the enormous configuration which resources are used.

    I wrote a custom script that tells me the used process id.

    riskshield@omega:~> rsservice status
    ============================================================
    [  OK  ] RiskShield Decision Server is  running
    ============================================================
    riskshield    11738     1  1 15:40 pts/0    00:00:14 /usr/bin/java -Drss.configuration.propertiesFile=../rss_config.properties -Djava.library.path=/opt/RiskShield/Resources/RS-Server/current/bin -server -Xmx32G -Xms32G -classpath /usr/share/java/*:/opt/RiskShield/DecisionServer/.:/opt/RiskShield/Resources/JDBC/ojdbc6.jar:/opt/RiskShield/Resources/RS-Server/current/lib/*:/opt/RiskShield/Resources/JmsQueue/* -XX:+UseG1GC -Xloggc:/var/log/RiskShield/cur/decision_server_gc-201606011540.log -verbose:GC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=25 -XX:GCLogFileSize=100K com.riskshield.server.Starter -idecision_server.ini start
    ============================================================
    [  OK  ] RiskShield Data Server is  running
    ============================================================
    riskshield    11743     1  0 15:40 pts/0    00:00:09 /usr/bin/java -Drss.configuration.propertiesFile=../rss_config.properties -Djava.library.path=/opt/RiskShield/Resources/RS-Server/current/bin -server -Xmx4G -Xms4G -classpath /usr/share/java/*:/opt/RiskShield/DataServer/.:/opt/RiskShield/Resources/JDBC/ojdbc6.jar:/opt/RiskShield/Resources/RS-Server/current/lib/* -XX:+UseG1GC -Xloggc:/var/log/RiskShield/cur/data_server_gc-201606011540.log -verbose:GC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=25 -XX:GCLogFileSize=100K com.riskshield.server.Starter -idata_server.ini start
    ============================================================
    [  OK  ] RiskShield Back Office Component is  running , process 11747
    ============================================================
    ============================================================
    [  OK  ] File Watchdog for RiskShield is  running
    ============================================================
    riskshield    11789     1  0 15:40 pts/0    00:00:00 bash /home/riskshield/bin/file-watchdog.sh start
    riskshield    11794 11789  0 15:40 pts/0    00:00:00 bash /home/riskshield/bin/file-watchdog.sh start
    ============================================================
    bash(11789)-+-bash(11794)
                `-inotifywait(11791)
    

    With the retrieved process id I can check the used libraries and files. Example output truncated.

    riskshield@omega:~> ls -l /proc/11738/fd
    total 0
    lr-x------ 1 riskshield riskshield 64 Jun  1 15:57 0 -> /dev/null
    l-wx------ 1 riskshield riskshield 64 Jun  1 15:57 1 -> /dev/null
    lr-x------ 1 riskshield riskshield 64 Jun  1 15:57 10 -> /opt/RiskShield/Resources/RS-Server/4.32a/lib/derby.jar
    lr-x------ 1 riskshield riskshield 64 Jun  1 15:57 100 -> /opt/RiskShield/Resources/RS-Server/4.32a/lib/servlet-api-3.1.jar
    lrwx------ 1 riskshield riskshield 64 Jun  1 15:57 101 -> socket:[91725601]
    lrwx------ 1 riskshield riskshield 64 Jun  1 15:57 102 -> socket:[91725602]
    ...
    lrwx------ 1 riskshield riskshield 64 Jun  1 15:57 128 -> anon_inode:[eventpoll]
    lr-x------ 1 riskshield riskshield 64 Jun  1 15:57 129 -> pipe:[91548343]
    lr-x------ 1 tpiss rshld 64 Jun  1 15:57 99 -> /opt/RiskShield/Resources/RS-Server/4.32a/lib/rss.api.jar
    
  5. 2016-06-01 - Show process tree of a dedicated process; Tags: Show process tree of a dedicated process
    Loading...

    Show process tree of a dedicated process

    I always wonder if I could display the a certain process with pstree and not the whole process tree itself. It is possible. See below the results after reading the man page and trying out the options.

    Example process with subprocess

    tan@omega:~> ps -Af | grep '[f]ile-watchdog.sh start'
    tan 11165 1 0 09:53 pts/1 00:00:00 bash bin/file-watchdog.sh start
    tan 11167 11165 0 09:53 pts/1 00:00:00 bash bin/file-watchdog.sh start
    

    Display process tree of one process

    tan@omega:~> pstree 11165
    bash─┬─bash
    └─inotifywait
    

    Display process tree in Unicode

    tan@omega:~> pstree -U 11165
    bash─┬─bash
    └─inotifywait
    

    Display process tree in Unicode, compact and with process id

    tan@omega:~> pstree -U -c -p 11165
    bash(11165)─┬─bash(11167)
    └─inotifywait(11166)
    

    Display process tree compact, process id in ASCII characters

    tan@omega:~> pstree -c -p -A 11165
    bash(11165)-+-bash(11167)
    `-inotifywait(11166)
    
  6. 2016-04-25 - Conditional check of installed rpm; Tags: Conditional check of installed rpm
    Loading...

    Conditional check of installed rpm

    I had to do some scripting. You can query the rpm database with a wildcard pattern. The output is stored in a variable FOUND.

    FOUND=$(rpm -qa *econn*)
    vinh@foha03:~> echo $FOUND
    fo-econn-dev-3.2.7-364.noarch
    

    To check is the search result is empty (-z) or not empty (-n):

    vinh@foha03:~> if [ -z "$FOUND" ]; then echo $FOUND; fi
    vinh@foha03:~> if [ -n "$FOUND" ]; then echo $FOUND; fi
    fo-econn-dev-3.2.7-364.noarch
    
  7. 2016-02-11 - Check for running Elasticsearch instance in a shell script; Tags: Check for running Elasticsearch instance in a shell script
    Loading...

    Check for running Elasticsearch instance in a shell script

    This small shell script checks if an Elasticsearch instance is running by querying the REST API.

    #!/usr/bin/env bash
    PORT=9200
    URL="http://localhost:$PORT"
    # Check that Elasticsearch is running
    curl -s $URL 2>&1 > /dev/null
    if [ $? != 0 ]; then
        echo "Unable to contact Elasticsearch on port $PORT."
        echo "Please ensure Elasticsearch is running and can be reached at $URL"
        exit -1
    fi
    
  8. 2015-11-14 - Use dirname and basename for dealing with paths; Tags: Use dirname and basename for dealing with paths
    Loading...

    Use dirname and basename for dealing with paths

    Dealing with paths or pathnames is a common Bash Shell scripting task. dirname and basename are unix programs that are very handy for dealing with pathnames.

    Getting current path with pwd (print working directory).

    root@cinhtau:/var/log/apache2# pwd
    /var/log/apache2
    

    Get last directory

    root@cinhtau:/var/log/apache2# basename $(pwd)
    apache2
    

    Get all directories except the last directory

    root@cinhtau:/var/log/apache2# dirname $(pwd)
    /var/log
    

    If you deal with symbolic links, you can use realpath to get the absolute pathname.

    pi@pelion /var/run $ realpath shm
    /dev/shm
    
  9. 2015-11-12 - Use whiptail in interactive shell scripts; Tags: Use whiptail in interactive shell scripts
    Loading...

    Use whiptail in interactive shell scripts

    Whiptail is a program that allows shell scripts to display dialog boxes to the user for informational purposes, or to get input from the user in a friendly way. It is included in several linux distributions like Debian or rhel.

    Following script demonstrates a shell script skeleton for a rpm cleanup script. Whiptail is only displayed in interactive mode (run the script with bash -i).

    #!/bin/bash
    do_rpm_cleanup(){
      RPM=`which rpm`
      if [ "$RPM"="" ]; then
        echo "no rpm installed"
      else
        echo "do cleanup"
        $RPM --rebuildb
      fi
    }
    if [[ "$-" == *i* ]]; then
      if whiptail --title "Cleanup RPM" --yesno "Are you sure?" 10 40 ;then
        do_rpm_cleanup
      else
        echo "Cleanup aborted"
      fi
    else
      echo "do something old school"
      do_rpm_cleanup
    fi