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)

WebLogic 12c – Changes to Domain Configuration Lost on Server Reboot

Had this weird issue where every time I’d made elaborate deployments and configuration changes in WebLogic Console, they’d miraculously disappear on server reboot.

Naturally, this is extremely infuriating considering the time it takes to deploy/configure just about ANYTHING in WebLogic.

So basically, I did some snooping around in the file structure after just about going insane and found this little diddy in the domain structure of the root.

/home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/domain_bak/config_prev/config.xml

I had read on this guys blog that he had a similar issue essentially:

Somehow, every time I applied changes even after deleting this pesky file the folder and backup file kept getting created and reverting ALL the changes on reboot and successful activation.

I got fed up.

As a provisional/permanent fix, I added a small line before invoking the startWebLogic.sh which is below.

rm -Rf /home/oracle/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/domain_bak/*

Fixed my issue and worked like a charm.

Script for Debugging Windows Updates

Here’s a quick script I wrote to debug issues with Windows Update if you ever need to pass information along to IT.

To be run in git-bash and in it’s own folder.

###########################################
#### Declare Output Directory Settings ####
###########################################

OUTPUT_PATH="."
FOLDER_NAME=$(date +"%Y-%m-%d_%H-%M-%S")
FOLDER_PATH="$OUTPUT_PATH/$FOLDER_NAME"

mkdir -p "$FOLDER_PATH"

######################
####   Do Work    ####
######################

cp /c/Windows/Logs/WindowsUpdate/* "$FOLDER_PATH"
cat /c/Windows/Logs/CBS/CBS.log > "$FOLDER_PATH/CBS.log"
cp /c/ProgramData/USOShared/Logs/* "$FOLDER_PATH"

PowerShell.exe -command "Get-WindowsUpdateLog -LogPath \"$FOLDER_PATH/WindowsUpdate.log\""
cat "$FOLDER_PATH/WindowsUpdate.log"