WebLogic – Admin Server and Node Manager Utility Script

Wrote this as I wanted any starts of my managed servers under WebLogic to wait until the Admin Server is finished launching.

It also doubles as great syntax bookmark for what core components to start on the WebLogic server and what the filenames are.

function weblogicStart() {
  START_COMMAND="$1"
  LOG_OUTPUT="$2"
	
  "$START_COMMAND" > "$LOG_OUTPUT" 2>&1 &
}

function weblogicStart_ManagedServer() {
  START_COMMAND="$1"
  SERVER_NAME="$2"
  ADMIN_SERVER_URL="$3"
  LOG_OUTPUT="$4"
	
  "$START_COMMAND" "$SERVER_NAME" "$ADMIN_SERVER_URL" > "$LOG_OUTPUT" 2>&1 &
}

function weblogicWait() {
  SERVER_NAME="$1"
  LOG_FILE="$2"

  echo "Waiting until WebLogic $SERVER_NAME is finished starting up before continuing..."
  ( tail -f -n0 "$LOG_FILE" & ) | grep -q "<Server state changed to RUNNING.>"
  echo "WebLogic $SERVER_NAME is Up!"
}

#Log Root for All Logs
LOG_ROOT="/home/oracle"

#Hack to remove any straggling domain backups that prevent changes from persisting on reboot
rm -Rf /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/domain_bak/*

#Defining core WebLogic Logs and Commands
WEBLOGIC_DOMAIN_ROOT="/home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain"

WEBLOGIC_ADMINSERVER_LOG="$LOG_ROOT/weblogic.out"
WEBLOGIC_ADMINSERVER_URL="http://localhost:8080"
WEBLOGIC_ADMINSERVER_SERVERNAME="AdminServer"
WEBLOGIC_ADMINSERVER_START="$WEBLOGIC_DOMAIN_ROOT/startWebLogic.sh"

WEBLOGIC_MANAGEDSERVER_START="$WEBLOGIC_DOMAIN_ROOT/bin/startManagedWebLogic.sh"

WEBLOGIC_NODEMANAGER_LOG="$LOG_ROOT/nodemanager.out"
WEBLOGIC_NODEMANAGER_START="$WEBLOGIC_DOMAIN_ROOT/bin/startNodeManager.sh"

#Defining JVM1 WebLogic Server Names and Log Paths
WEBLOGIC_JVM1_LOG="$LOG_ROOT/JVM1.out"
WEBLOGIC_JVM1_SERVERNAME="JVM1"

#Begin Weblogic Commands
weblogicStart "$WEBLOGIC_NODEMANAGER_START" "$WEBLOGIC_NODEMANAGER_LOG"
weblogicStart "$WEBLOGIC_ADMINSERVER_START" "$WEBLOGIC_ADMINSERVER_LOG"

#Wait Until Admin Server is finished booting up
weblogicWait "$WEBLOGIC_ADMINSERVER_SERVERNAME" "$WEBLOGIC_ADMINSERVER_LOG"

#Start Admin Server
weblogicStart_ManagedServer "$WEBLOGIC_MANAGEDSERVER_START" "$WEBLOGIC_JVM1_SERVERNAME" "$WEBLOGIC_ADMINSERVER_URL" "$WEBLOGIC_JVM1_LOG"

SSH Tunnel Script with Commented Usage Example

Useful script for accessing an unexposed port when you only have access to ssh on a server for debug purposes.

########################################
### BEGIN USAGE EXAMPLE FOR COPYING  ###
########################################

##########################################################################
### Usage Example, Copy the below into a separate script and uncomment ###
##########################################################################

#REMOTE_ADDRESS=8.8.8.8
#REMOTE_SSH_PORT=2020
#REMOTE_USERNAME=root
#REMOTE_PASSWORD=password

#declare -A DESTINATION_LIST
#DESTINATION_LIST+=(["5902"]="localhost:5902")

#sh tunnelOpen.sh "$REMOTE_ADDRESS" "$REMOTE_SSH_PORT" "$REMOTE_USERNAME" "$REMOTE_PASSWORD" "$(declare -p DESTINATION_LIST)"

########################################
### END USAGE EXAMPLE FOR COPYING  #####
########################################

REMOTE_ADDRESS=$1; shift
REMOTE_SSH_PORT=$1; shift
REMOTE_USERNAME=$1; shift
REMOTE_PASSWORD=$1; shift
eval "declare -A DESTINATION_LIST="${1#*=}

function establishTunnels()
{
    clear

    for LOCAL_LISTEN_PORT in "${!DESTINATION_LIST[@]}"
    do
        #Pull Values from Array
        DESTINATION_ADDRESS_DESTINATION_PORT=${DESTINATION_LIST[$LOCAL_LISTEN_PORT]}

        echo "Establishing Tunnel to $DESTINATION_ADDRESS_DESTINATION_PORT using Local Port $LOCAL_LISTEN_PORT"

        #ssh-keygen -R [$REMOTE_ADDRESS]:$REMOTE_SSH_PORT
        #ssh -o StrictHostKeyChecking=no -fNL $LOCAL_LISTEN_PORT:$DESTINATION_ADDRESS_DESTINATION_PORT -l root $REMOTE_ADDRESS -p $REMOTE_SSH_PORT >/dev/null 2>&1

        nohup echo y | "/c/Program Files/PuTTY/plink.exe" -N -L $LOCAL_LISTEN_PORT:$DESTINATION_ADDRESS_DESTINATION_PORT -ssh $REMOTE_USERNAME@$REMOTE_ADDRESS -P $REMOTE_SSH_PORT -pw $REMOTE_PASSWORD >/dev/null 2>&1 &

        echo ""
        echo "##################################################################"
        echo "If there are no errors up to this point you can now connect"
        echo "to the endpoint using localhost:$LOCAL_LISTEN_PORT using ssh/rdp"
        echo "##################################################################"
        echo ""
    done
}

establishTunnels

Bluetooth Pairing Script using Expect in Bash

Needed a quick script to do automated pairing in Linux of already known bluetooth devices. I’m using my Switch and L4T Ubuntu to stream using Moonlight and hate having to repair my controllers every time.

Credit to RamonGilabert and the people in the below discussion for their work and iterations. My modification was removing expect of controller (since it wouldn’t work for me), raising the sleep from 5 to 10 on the scanning phase, and adding a connect at the end of the script to force connect the Joycons. Works pretty darn well if I do say so myself.

https://gist.github.com/RamonGilabert/046727b302b4d9fb0055
#!/usr/bin/expect -f

set prompt "#"
set address [lindex $argv 0]

spawn bluetoothctl
expect -re $prompt
send "remove $address\r"
sleep 1
expect -re $prompt
send "scan on\r"
send_user "\nSleeping\r"
sleep 10
send_user "\nDone sleeping\r"
send "scan off\r"
send "trust $address\r"
sleep 2
send "pair $address\r"
sleep 2
send "0000\r"
sleep 3
send_user "\nShould be paired now.\r"
send "connect $address\r"
send "quit\r"
expect eof

Also, as a bonus here is the below Launcher file to be placed on your desktop as “ControllerName.desktop” that does the invocation of the script. Basically just ripped off the Chromium Launcher file.

#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Name=GreyLeft
Exec=/home/cody/Documents/bluetoothConnect.sh 00:00:00:00:00:00
Terminal=true
X-MultipleArgs=false
Type=Application
Icon=preferences-desktop-remote-desktop
Categories=Network;
MimeType=text/html;text/xml;application/xhtml_xml;x-scheme-handler/http;x-scheme-handler/https;
StartupNotify=true
Name[en_US]=GreyLeft.desktop

SSH Port Forwarding Utility Script for Secured Endpoint Testing

Wrote this quick little diddy for testing endpoints with SOAP UI that are traditionally not reachable unless on a specific box that has an IP range or network that is whitelisted for connection.

Read comments below and change as needed for your respective endpoint.

Be sure to terminate the script afterwards so that the tunnel is closed.

#Change IP to AWS Server IP and Correct Username
AWS_SERVER_IP="192.168.1.1"
AWS_SERVER_PORT="22"
AWS_SERVER_USER="myCoolUser"

#External app address that can only be hit from the EC2 Instance or Server.
#This is useful for debugging of Web Service Endpoints and Connectivity Tests.

EXTERNAL_APP_IP="appHostnameThatCanOnlyBeHitFromAwsBox.com"
EXTERNAL_APP_PORT="443"

#Local Port that you will hit to test appHostnameThatCanOnlyBeHitFromAwsBox
#aka open Web Browser and go to https://localhost:1337 and this will forward 
#the connection to appHostnameThatCanOnlyBeHitFromAwsBox and the respective port

LOCAL_APP_PORT="1337"

#Set the proper unix style path using either Cygdrive or Git-Bash

PLINK_PATH="/c/path/PLINK.exe"
PPK_PATH="/c/path/pathToPpk.ppk"

#PLINK Syntax: 
#Password: -pw password 
#Private Key: -i "$PPK_PATH"
#Run in Background: & at the end

"$PLINK_PATH" -N -L $LOCAL_APP_PORT:$EXTERNAL_APP_IP:$EXTERNAL_APP_PORT -ssh $AWS_SERVER_USER@$AWS_SERVER_IP -P $AWS_SERVER_PORT -i "$PPK_PATH" 

Sendmail Example Usage for Bash

Useful snippet I wrote up after searching the internet for details on syntax for sendmail in Linux.

PRETTY_HOSTNAME=$(hostnamectl --pretty)
ADMIN_USERNAME='username'
ADMIN_PASSWORD='password'

sendmail -F "noreply@website.com" -it <<END_MESSAGE
Content-Type: text/html
MIME-Version: 1.0
To: person@email.com
Subject: [INFO] Connection Details
Hostname: $PRETTY_HOSTNAME <br />
If your program is down, do the following.
<br />
1. Open Putty <br />
2. Establish Connection to $PRETTY_HOSTNAME <br />
2a. Username: $ADMIN_USERNAME <br />
2b. Password: $ADMIN_PASSWORD <br />
3a. Check for the status of your program. <br />
If there are any questions or concerns please reach out to your server admin.
<br />
Thanks, <br />
Automated System 
END_MESSAGE

Bash Snippet to Dynamically Find Free Ports

Useful snippet I wrote for Docker container creation and dynamic port assignment.

ALLOCATED_PORT_LIST=()

function getFreePort()
{
	CHECK="do while"

	while [[ ! -z $CHECK ]]; do
		PORT=$(( ( RANDOM % 60000 )  + 1025 ))
		CHECK=$(sudo netstat -ap | grep $PORT)
	done

	echo $PORT
}

function getFreePort_Unallocated()
{
	PORT=$(getFreePort)
	
	MATCH_FOUND="false"

	for ALLOCATED_PORT in "${ALLOCATED_PORT_LIST[@]}"
	do
		if [ $PORT == $ALLOCATED_PORT ]; then
			MATCH_FOUND="true"
		fi
	done
	
	if [ $MATCH_FOUND == "true" ]; then
		getFreePort_Unallocated
	else
        ALLOCATED_PORT_LIST+=("$PORT")
		echo $PORT
	fi
}

PORT1=$(getFreePort_Unallocated)
PORT2=$(getFreePort_Unallocated)
PORT3=$(getFreePort_Unallocated)