VXP

Docker port checker

VXP
Sep 28th, 2020
1,039
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Usage: test_docker.sh <port> <sleepfor>
  4.  
  5. # Get parameters from the arguments passed
  6. port=$1
  7. sleepfor=$2
  8.  
  9. # Check if no argments has been passed to script
  10. if [ "$#" -le "0" ]; then
  11.     echo Not enough parameters passed.
  12.     echo
  13.     echo Usage:
  14.     echo "test_docker.sh <port> <sleepfor>"
  15.     exit
  16. fi
  17.  
  18. # If the second argument hasn't been passed - set the sleep value to a default value
  19. if [ "$sleepfor" = "" ]; then
  20.     echo "Setting the default value for sleepfor to 1 second"
  21.     sleepfor=1
  22. fi
  23.  
  24. # CentOS/RedHat: yum install nc
  25. # Fedora 22+: dmf install nc
  26. # Ubuntu: sudo apt-get install netcat
  27. # nc -zv localhost $port
  28.  
  29. # Begin the loop
  30. while [ true ]
  31. do
  32.     # Running the port test command
  33.     nc -zv localhost $port &> /dev/null
  34.  
  35.     # Check for errorlevel. 0 - command returned good value, >0 - not good (probably Docker is not present on this port)
  36.     if [ "$?" -eq "1" ]; then
  37.         echo Docker is not running on this port. Retrying...
  38.         sleep $sleepfor
  39.  
  40.         # Do another loop, do not proceed to a restart containers routine
  41.         continue
  42.     fi
  43.  
  44.     echo Something is running on this port.
  45.     sleep $sleepfor
  46.  
  47.     # TODO: Restart containers here:
  48.     #docker restart ...
  49.  
  50.     # Get out of the loop
  51.     break
  52. done
  53.  
  54. echo Docker containers has been restarted
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
RAW Paste Data