Automatic Bash Script for Unpackage/Repackage of Java WAR/JAR files

Had some instruction today from management that I would need to modify the contents of a WAR file and replace/update a specific XML file.

So you guessed it. I wrote a quick script for it.

Below are some usage screenshots:

Unpackaging a WAR:

Repackaging a WAR:


Here is the code:

unpackWar() {
    echo "Type Full Java JAR/WAR Path: "
	read FILE_PATH_FULL

    #echo "FILE_PATH_FULL:" 
    #echo $FILE_PATH_FULL

    FILE_EXTENSION=$(echo ${FILE_PATH_FULL##*.})

    #echo "FILE_EXTENSION:"
    #echo $FILE_EXTENSION

    FILE_NAME_WITHOUT_EXTENSION=$(basename "$FILE_PATH_FULL" ".$FILE_EXTENSION")

    #echo "FILE_NAME_WITHOUT_EXTENSION:"
    #echo $FILE_NAME_WITHOUT_EXTENSION

    FILE_NAME_WITH_EXTENSION="$FILE_NAME_WITHOUT_EXTENSION"
    FILE_NAME_WITH_EXTENSION+="."
    FILE_NAME_WITH_EXTENSION+="$FILE_EXTENSION"
    
    #echo "FILE_NAME_WITH_EXTENSION:"
    #echo $FILE_NAME_WITH_EXTENSION

    FILE_PATH_DIRECTORY=$(printf '%s\n' "${FILE_PATH_FULL//$FILE_NAME_WITH_EXTENSION/}")
    echo $FILE_PATH_DIRECTORY

    #echo "FILE_PATH_DIRECTORY:"
    #echo $FILE_PATH_DIRECTORY

    OUTPUT_DIRECTORY="$FILE_PATH_DIRECTORY/$FILE_NAME_WITHOUT_EXTENSION"

    #echo "OUTPUT_DIRECTORY:"
    #echo $OUTPUT_DIRECTORY

    OUTPUT_DIRECTORY_TEMP_FILE=$OUTPUT_DIRECTORY
    OUTPUT_DIRECTORY_TEMP_FILE+="/"
    OUTPUT_DIRECTORY_TEMP_FILE+=$FILE_NAME_WITH_EXTENSION

    #echo "OUTPUT_DIRECTORY_TEMP_FILE:"
    #echo $OUTPUT_DIRECTORY_TEMP_FILE

    mkdir "$OUTPUT_DIRECTORY"
    cp "$FILE_PATH_FULL" "$OUTPUT_DIRECTORY_TEMP_FILE"
    
    cd "$OUTPUT_DIRECTORY"
    jar -xf "$OUTPUT_DIRECTORY_TEMP_FILE" 
    rm "$OUTPUT_DIRECTORY_TEMP_FILE"
}

repackWar() {
    echo "Type Path To Package: "
	read OUTPUT_DIRECTORY

    echo "Type The Final File Extension (Without Period): "
	read FILE_EXTENSION

    FILE_NAME_WITH_EXTENSION=$(basename "$OUTPUT_DIRECTORY")
    FILE_NAME_WITH_EXTENSION+="."
    FILE_NAME_WITH_EXTENSION+=$FILE_EXTENSION
    
    cd "$OUTPUT_DIRECTORY"
    jar -cf "$FILE_NAME_WITH_EXTENSION" .
}

# Bash Menu Script Example
choiceFunction() {
	SUCCESS="false"

	while [ $SUCCESS == "false" ]
	do
		clear
		echo "Please enter your choice: "
		echo "1 - Unpackage War" 
		echo "2 - Repackage War" 
		echo "3 - Quit"
		read CHOICE

		case $CHOICE in
		   "1") SUCCESS="true";echo "Unpacking War ..."; unpackWar;;
		   "2") SUCCESS="true";echo "Packing War ..."; repackWar;;
		   "3") SUCCESS="true";echo "Exiting ..."; exit;;
		   *) read -p "Invalid choice, Press any key to continue..." fakeVariableIgnoreMe;;
		esac
	done
}

choiceFunction

Continuous Download Script Utilizing Bash

I had been having an issue getting a file from a central server halfway across the world and it kept failing every 5 minutes.

To remedy this I put a curl command inside a while loop and made it keep attempting to download the file until it successfully downloaded.

As a result, I came up with the below script, and it worked very nicely:

URL_TO_DOWNLOAD_FROM="http://URL.COM/download.zip"
FINAL_FILE_NAME="MyFile.zip"

EXIT_CODE=1

while [ $EXIT_CODE -ne 0 ]
do
	echo "Downloading File - $FINAL_FILE_NAME from $URL_TO_DOWNLOAD_FROM ..."
	rm -Rf $FINAL_FILE_NAME
	curl $URL_TO_DOWNLOAD_FROM --output $FINAL_FILE_NAME
	
	EXIT_CODE=$?
	
	if [ $EXIT_CODE -ne 0 ]; then
		echo "Download Failed! Retrying..."
	fi
done

if [ $EXIT_CODE -eq 0 ]; then
	echo "Download Finished!"
fi 

Automatic Connection Script for Cisco Any Connect and Windows RDP Box

Hello and welcome back!

Today I come to you with a quick hack I wrote to allow myself to quickly connect to Cisco Any Connect VPN Endpoints. I wrote this with Linux in mind as I use a local VirtualBox VM locally that runs literally nothing but a VPN and an RDP client.

The reason? I don’t want my main computer touching anything with this specific VPN I am referring to and wanted a flexible way to RDP into a required server.

As a result, I came up with the below script based off the current version of Cisco Any Connect in combination with xFreeRDP:

VPN_COMMAND="/opt/cisco/anyconnect/bin/vpn"
VPN_ADDRESS="https://corporate_vpn.com"
VPN_CONNECT="$VPN_COMMAND -s connect $VPN_ADDRESS"
VPN_DISCONNECT="$VPN_COMMAND disconnect"
USERNAME="username"
DOMAIN="domain"
PASSWORD="password"
VDI_ADDRESS="VDI-IP"
SUDO_PASSWORD="sudo_password"

$VPN_DISCONNECT

#Remove Existing Configurations
rm -f ~/.anyconnect
printf "$SUDO_PASSWORD" | sudo -S "find rm -f /opt/cisco/anyconnect/profile/ -name '*.xml' -delete"

echo "Type Token:"
read token

printf "y\ny\n" | $VPN_CONNECT
#$VPN_CONNECT

#If Certificate Not Trusted
printf "y\ny\n2\n$USERNAME\n$PASSWORD\n$token\n" | $VPN_CONNECT

#If Certificate Trusted
#printf "2\n$USERNAME\n$PASSWORD\n$token\n" | $VPN_CONNECT

xfreerdp /u:$DOMAIN\\$USERNAME /p:$PASSWORD /v:$VDI_ADDRESS /f -wallpaper +clipboard

Automatic Meeting Generator Using PowerShell (Generic and On-Boarding)

Today I come to you with a PowerShell Script written to generate meeting invites in mass quantity.

Here was the Problem Statement:

  • Problem:
    • As a helpful employee, I need to be able to generate (in-mass) meeting invites to a bunch of different people with various subjects for my boss when he asks
      • Side note: These were on-boarding meetings so this definitely will be done for on-boarding in the future for other employees
    • These meetings with the same people will 100% be needed to done again in the future so it needs to be automated
  • Solution:
    • Using the Outlook command-line extensions I can create a automated PowerShell script that can generate meetings in bulk

Here is the Code (Generic Version – Non On-Boarding):

$OUTLOOK = "C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"

$MEETING_HASHMAP = @{ }

function generateParticipants {
    $PARTICIPANT_LIST = $args[0]

    $PARTICIPANT_STRING_TO_RETURN = ""

    foreach ($PARTICIPANT in $PARTICIPANT_LIST) {
        $PARTICIPANT_STRING_TO_RETURN = $PARTICIPANT_STRING_TO_RETURN + ";" +  $PARTICIPANT
    }

    return $PARTICIPANT_STRING_TO_RETURN
}

function createMeeting {
    
    $SUBJECT = $args[0]
        
    $BODY = $args[1]

    $PARTICIPANT_LIST = $args[2]

    $TO = generateParticipants $PARTICIPANT_LIST

    $MEETING_CONTENTS = "$TO&subject=$SUBJECT&body=$BODY"

    Start-Process -WindowStyle Maximized -FilePath "$OUTLOOK" -ArgumentList "/c ipm.appointment /m `"$MEETING_CONTENTS`""
}

function addMeeting {
    $SUBJECT_OF_MEETING = $args[0]
    $PARTICIPANT_LIST = $args[1]
    $MEETING_HASHMAP.Add($SUBJECT_OF_MEETING, $PARTICIPANT_LIST)
}

function generateMeetingInvites {
    foreach ($SUBJECT_OF_MEETING in $INTRODUCTION_HASHMAP.Keys) {
        $PARTICIPANT_LIST = $MEETING_HASHMAP[$SUBJECT_OF_MEETING]
        createMeeting $SUBJECT_OF_MEETING "Meeting Body" $PARTICIPANT_LIST
        Start-Sleep -Seconds 2
    }
}

$SUBJECT_OF_MEETING = "Quality Assurance"
$PARTICIPANT_LIST = New-Object Collections.Generic.List[String]
$PARTICIPANT_LIST.Add("bob.dole@company.com")
$PARTICIPANT_LIST.Add("jed.i.knighte@company.com")
addMeeting $SUBJECT_OF_MEETING $PARTICIPANT_LIST

$SUBJECT_OF_MEETING = "Software Development"
$PARTICIPANT_LIST = New-Object Collections.Generic.List[String]
$PARTICIPANT_LIST.Add("bob.dole@company.com")
$PARTICIPANT_LIST.Add("jed.i.knighte@company.com")
addMeeting $SUBJECT_OF_MEETING $PARTICIPANT_LIST

generateMeetingInvites

Here is the Other Code (On-Boarding Version):

$PERSON_NAME="Randy Pausch"
$PERSON_JOB_TITLE="Senior Imagineer"
$PERSON_EMAIL="randy.pausch@carnigie.edu"

$OUTLOOK = "C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"

$INTRODUCTION_HASHMAP = @{ }

function generateSubject {
    $PERSON_JOB_TITLE = $args[0]
    $PERSON_NAME = $args[1]
    $DEPARTMENT_NAME = $args[2]

    return "Intro of New $PERSON_JOB_TITLE $PERSON_NAME to $DEPARTMENT_NAME"
}

function generateParticipants {
    $PARTICIPANT_LIST = $args[0]

    $PARTICIPANT_STRING_TO_RETURN = ""

    foreach ($PARTICIPANT in $PARTICIPANT_LIST) {
        $PARTICIPANT_STRING_TO_RETURN = $PARTICIPANT_STRING_TO_RETURN + ";" +  $PARTICIPANT
    }

    $PARTICIPANT_STRING_TO_RETURN = $PARTICIPANT_STRING_TO_RETURN + ";" + $PERSON_EMAIL

    return $PARTICIPANT_STRING_TO_RETURN
}

function createMeeting {
    $PERSON_JOB_TITLE = $args[0]
    
    $PERSON_NAME = $args[1]
    
    $DEPARTMENT_NAME = $args[2]
    
    $BODY = $args[3]
    
    $TO_ARRAY = $args[4]

    $SUBJECT = generateSubject $PERSON_JOB_TITLE $PERSON_NAME $DEPARTMENT_NAME

    $TO = generateParticipants $TO_ARRAY

    $MEETING_CONTENTS = "$TO&subject=$SUBJECT&body=$BODY"

    Start-Process -WindowStyle Maximized -FilePath "$OUTLOOK" -ArgumentList "/c ipm.appointment /m `"$MEETING_CONTENTS`""
}

function addDepartment {
    $DEPARTMENT_NAME = $args[0]
    $PARTICIPANT_LIST = $args[1]
    $INTRODUCTION_HASHMAP.Add($DEPARTMENT_NAME, $PARTICIPANT_LIST)
}

function generateMeetingInvites {
    foreach ($DEPARTMENT_NAME in $INTRODUCTION_HASHMAP.Keys) {
        createMeeting $PERSON_JOB_TITLE $PERSON_NAME $DEPARTMENT_NAME "Overview of Department and Practices" $INTRODUCTION_HASHMAP[$DEPARTMENT_NAME]
        Start-Sleep -Seconds 2
    }
}


$DEPARTMENT_NAME = "Quality Assurance"
$PARTICIPANT_LIST = New-Object Collections.Generic.List[String]
$PARTICIPANT_LIST.Add("bob.dole@company.com")
$PARTICIPANT_LIST.Add("jed.i.knighte@company.com")
addDepartment $DEPARTMENT_NAME $PARTICIPANT_LIST

$DEPARTMENT_NAME = "Software Development"
$PARTICIPANT_LIST = New-Object Collections.Generic.List[String]
$PARTICIPANT_LIST.Add("bob.dole@company.com")
$PARTICIPANT_LIST.Add("jed.i.knighte@company.com")
addDepartment $DEPARTMENT_NAME $PARTICIPANT_LIST

generateMeetingInvites

Annoying Reminder Ball – Utilizing Visual Basic and C#

So I set out to do something yesterday. It was one of those fun code things that literally takes you 20-30 minutes if you have had the prior experience doing it. I wanted an annoying executable that fires and bounces something around the screen each time Outlook popped up saying you have an upcoming meeting.

So I did some quick Googling because I definitely wasn’t going to waste time personally writing something like this because I was sure someone has created something prior. And low and behold someone did.

As a result, I took this code and upgraded it to the latest version of .NET and to my lucky surprise, it worked fine!

So here’s what I did after I got that code:

  • Made the ball bigger
  • Studied the movement tick function behind it and separated some code out for… more annoying purposes
  • Created an async task that runs every 2-3 seconds that bounces the ball ridiculously around the screen
  • Change the way the ball works with it displaying a color and instead put an image on the inside
  • Added logic to the beginning of the Main(); method to have it check if there are any other balls bouncing around the screen, kill them, and have it take their place

The result? Just a simply amazing annoying reminder.

When coupled with VBA Functionality with Outlook I was able to create a great reminder that will jump around and be easily closed.

If you’re interested in checking out the code visit my Github repo for the project below:

Setting Required Proxy Variables Using Bash Shell (URL Encoding Included)

Today I come to you with a script I’ve created that helps to set the http_proxy/https_proxy/ftp_proxy/no_proxy variables and configruations for Yum, Git, and other basic commands that may make sure of these configurations.

I created this one due to the need to easily jump on any Linux Box and punch through a proxy then remove my credentials after.

This script uses strictly native Bash functionality to create a completely native Linux approach towards setting required configurations when working behind a Corporate Proxy.

The process would be as follows:

  1. Open SSH Terminal (Or Git-Bash/Cygwin)
  2. Execute this Bash Script using the “source” command
  3. Perform your Required Commands (pip/curl/yum/wget/etc)

Here is the script:

#!/bin/bash

##################################
#### Defining URL Encode     ####
#### Function for Passwords  ####
#### with Special Characters ####
#################################

rawurlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"
}

PROXY_ADDRESS=yourCorporateProxy.com
PROXY_PORT=80
PROXY_BYPASS=localhost,127.0.0.1
ENVIRONMENT_VARIABLE_LIST="http_proxy https_proxy ftp_proxy"
GIT_ENVIRONMENT_VARIABLE_LIST="http.proxy https.proxy"

setProxy() {

	###################################
	#### Defining Proxy Parameters ####
	###################################

	echo "Enter Username: "
	read USERNAME

	echo "Enter Password: "
	read -s PASSWORD

	PASSWORD_ENCODED=$(rawurlencode $PASSWORD)

	PROXY_URL="http://$USERNAME:$PASSWORD_ENCODED@$PROXY_ADDRESS:$PROXY_PORT/"


	for ENVIRONMENT_VARIABLE in $ENVIRONMENT_VARIABLE_LIST
	do
		TEMP=$ENVIRONMENT_VARIABLE="$PROXY_URL"
		export $TEMP
	done

	export no_proxy=$PROXY_BYPASS

	###################################
	#### Setting GIT Configuration ####
	###################################

	#for ENVIRONMENT_VARIABLE in $GIT_ENVIRONMENT_VARIABLE_LIST
	#do
		#Apparently Not Needed if Above Environment Variables are set
		#git config --global $ENVIRONMENT_VARIABLE $PROXY_URL
	#done

	###################################
	#### Setting YUM Configuration ####
	###################################

	echo proxy=http://$PROXY_ADDRESS:$PROXY_PORT >> /etc/yum.conf
	echo proxy_username=$USERNAME >> /etc/yum.conf
	echo proxy_password=$PASSWORD >> /etc/yum.conf
}

unsetProxy() {
	if [[ $suffix == *"User"* ]]; then
		JAVA_ARGS+=" $prefix.$suffix=$USERNAME"
	fi
	sed -i '/proxy=/d' /etc/yum.conf
	sed -i '/proxy_username=/d' /etc/yum.conf
	sed -i '/proxy_password=/d' /etc/yum.conf

	for ENVIRONMENT_VARIABLE in $ENVIRONMENT_VARIABLE_LIST
	do
		TEMP=$ENVIRONMENT_VARIABLE=""
		export $TEMP
	done

	export no_proxy=""
	
	for ENVIRONMENT_VARIABLE in $GIT_ENVIRONMENT_VARIABLE_LIST
	do
		git config --global --unset $ENVIRONMENT_VARIABLE 
	done
}


launchJavaJar() {
	######################################################
	#### Setting JAVA Launch Parameters Configuration ####
	######################################################

	echo "Type Java JAR Path: "
	read JAVA_JAR_PATH

	JAVA_ARGS=""

	PREFIX_LIST="-Dhttp -Dhttps"
	SUFFIX_LIST="proxyHost proxyPort proxyUser proxyPassword"

	for prefix in $PREFIX_LIST
	do
		 for suffix in $SUFFIX_LIST
		 do
			if [[ $suffix == *"Host"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=$PROXY_ADDRESS"
			fi

			if [[ $suffix == *"Port"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=$PROXY_PORT"
			fi	
			
			if [[ $suffix == *"User"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=$USERNAME"
			fi

			if [[ $suffix == *"Password"* ]]; then
				JAVA_ARGS+=" $prefix.$suffix=\"$PASSWORD\""
			fi	  
		 done
	done
	
	JAVA_ARGS+=" -Djava.net.useSystemProxies=true"
	JAVA_ARGS+=" -Djdk.http.auth.tunneling.disabledSchemes=\"\""
	JAVA_ARGS+=" -Djdk.http.auth.proxying.disabledSchemes=\"\""

	java $JAVA_ARGS -jar "$JAVA_JAR_PATH"
}

# Bash Menu Script Example
choiceFunction() {
	SUCCESS="false"

	while [ $SUCCESS == "false" ]
	do
		clear
		echo 'Please enter your choice: '
		echo "1 - Set Proxy Settings (Yum, Environment, GIT)" 
		echo "2 - Disable/Remote Proxy Settings (Yum, Environment, GIT)" 
		echo "3 - Launch Java JAR with Proxy Settings" 
		echo "4 - Quit"
		read CHOICE

		case $CHOICE in
		   "1") SUCCESS="true";echo "Setting Proxy..."; unsetProxy; setProxy;;
		   "2") SUCCESS="true";echo "Disabling Proxy..."; unsetProxy;;
		   "3") SUCCESS="true";echo "Launching JAR with Proxy Settings...";unsetProxy; setProxy;launchJavaJar;;
		   "4") SUCCESS="true";echo "Exitting..."; exit;;
		   *) read -p "Invalid choice, Press any key to continue..." fakeVariableIgnoreMe;;
		esac
	done
}

choiceFunction

Setting Required Proxy Variables Using Command Prompt (URL Encoding Included)

Today I come to you with a script I’ve created that helps to set the HTTP_PROXY/HTTPS_PROXY variables when using certain command prompt based programs.

I created this out of curiosity and necessity as I had a co-worker who preferred to work strictly in Cmd vs using something like Git-Bash or Cygwin.

This script uses a combination of PowerShell and Batch functionality to create a completely native Windows approach towards setting required configurations when working behind a Corporate Proxy.

The process would be as follows:

  1. Open Command Prompt
  2. Execute this Batch Script
  3. Perform your Required Commands (pip/curl/etc)

Here is the script:

ECHO OFF
CLS

:MENU
	ECHO.
	ECHO ...............................................
	ECHO PRESS 1, 2 to select your task, or 3 to EXIT.
	ECHO ...............................................
	ECHO.
	ECHO 1 - Set Proxy Variables
	ECHO 2 - Clear Proxy Variables
	ECHO 3 - Exit
	ECHO.
	SET /P M=Type 1, 2, or 3 then press ENTER:
	IF %M%==1 GOTO SET_PROXY
	IF %M%==2 GOTO CLEAR_PROXY
	IF %M%==3 GOTO END

:SET_PROXY
	SET PROXY_ADDRESS=yourCorporateProxy.com
	SET PROXY_PORT=80
	set /p USERNAME="Enter Username: "
	set /p PASSWORD="Enter Password: "

	SET COMMAND=powershell.exe -command "Add-Type -AssemblyName System.Web;[System.Web.HttpUtility]::UrlEncode(\"%PASSWORD%\")"


	FOR /F "tokens=* USEBACKQ" %%F IN (`%COMMAND%`) DO (
	SET PASSWORD=%%F
	)

	SET FULL_PROXY_URL=http://%USERNAME%:%PASSWORD%@%PROXY_ADDRESS%:%PROXY_PORT%/

	SET HTTP_PROXY=%FULL_PROXY_URL%
	SET HTTPS_PROXY=%FULL_PROXY_URL%

	GOTO END
:CLEAR_PROXY
	SET HTTP_PROXY=""
	SET HTTPS_PROXY=""

	GOTO END
:END
	CLS