Bash Script for Automatic Creation and Generation of Public Private Key for Linux Server Login

Wrote this quick blurb so I never have to do this process again.

The general idea is to take the output that is created on the screen, copy and paste it to a file somewhere on your computer, and either use it as is with your environment ssh configuration or convert it to a *.ppk for Putty.

What the script does is the following:

  1. Create the .ssh directory as the user you are currently logged in as (if it doesn’t already exist)
  2. Set necessary permissions on the directory (if it isn’t already set)
  3. Create the “authorized_keys” file (if it doesn’t already exist)
  4. Generate the Public/Private Pair
  5. Append the thumbprint to the “authorized_keys” file
  6. Output the private key for copying to Standard Out
  7. Delete the generated files for the Public/Private pair

After copying the key from the terminal screen you’ll paste it into a text file and either use it as is or convert it to a *.ppk for usage in Putty.

KEY_OWNER="Cody Garrett"
KEY_NAME="automatic.key"
KEY_NAME_PUB="$KEY_NAME.pub"
mkdir ~/.ssh
chmod 0700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 0644 ~/.ssh/authorized_keys
ssh-keygen -t rsa -b 4096 -C "$KEY_OWNER" -f "$KEY_NAME" -P ""
cat "$KEY_NAME.pub" >> ~/.ssh/authorized_keys
echo "Very Important!"
echo "The next screen that will come up will display the key needed for login."
echo "You must copy this in it's entirety in order to login using what was setup in the previous commands."
echo ""
echo "Press enter to continue..."
read
clear
echo ""
echo "##################"
echo "## Private Key: ##"
echo "##################"
echo ""
cat $KEY_NAME
echo ""
echo "###########################"
echo "## Authorized Key Entry: ##"
echo "###########################"
echo ""
cat $KEY_NAME_PUB
echo ""
rm -f "$KEY_NAME" "$KEY_NAME_PUB"

Automated Email Script in Bash

Rewrote my “Free Ice Cream” script in bash because I needed to be able to flexibly send emails without going through 20 UAC prompts due to restrictions and how many people are scared of PowerShell.

Below is my new implementation, instead of relying on Outlook (like before), this version is platform independent and will work on any OS with any Mail Client like Thunderbird or Gmail.

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}"
}

function generateParticipants() {
	PARTICIPANT_LIST=("$@")
	
	PARTICIPANT_STRING_TO_RETURN=""

	SEPARATOR=";"

	for PARTICIPANT in "${PARTICIPANT_LIST[@]}";
	do
		PARTICIPANT_STRING_TO_RETURN+="$PARTICIPANT"
		PARTICIPANT_STRING_TO_RETURN+="$SEPARATOR"
	done

	echo "$PARTICIPANT_STRING_TO_RETURN"
}
 
function createEmail() {
	SUBJECT="$1" # Save first argument in a variable
	shift
	BODY="$1" # Save second argument in a variable
	shift # Shift all arguments to the left (original $1 gets lost)
	TO_ARRAY=("$@")

	TO=$(generateParticipants "${TO_ARRAY[@]}")

	SUBJECT_ENCODED=$(rawurlencode "$SUBJECT")
	BODY_ENCODED=$(rawurlencode "$BODY")

	MESSAGE_OBJECT="mailto:$TO&subject=$SUBJECT_ENCODED&body=$BODY_ENCODED"
	
	echo "########################"
	echo "#### EMAIL PREVIEW: ####"
	echo "########################"
	echo ""
	echo "To: $TO"
	echo "Subject: $SUBJECT"
	echo "Body: $BODY"
	echo ""
	echo "Sending to Email Client..."
	echo ""
	echo start "" "$MESSAGE_OBJECT"
	start "" "$MESSAGE_OBJECT"
}
 
EMAIL_SUBJECT="Free Ice Cream!"
EMAIL_BODY="I'm Buying."

PARTICIPANT_LIST+=("person1@company.com")
PARTICIPANT_LIST+=("person2@company.com")
PARTICIPANT_LIST+=("person3@company.com")

createEmail "$EMAIL_SUBJECT" "$EMAIL_BODY" "${PARTICIPANT_LIST[@]}"

Simple Selenium – For Faster Test Development

Wanted to share a pretty fun “Plain Old Java App” I wrote today. It’s an extremely helpful implementation of Selenium for those who are just starting out and want a base to build on and expand their knowledge.

I originally wrote this implementation in Python but encountered that more people expect the usage of a traditional programming language.

Below is the README.md from my Github:

# Simple Selenium
An implementation of Selenium that extrapolates some of the more complex features of Selenium Development for development ease.

---

**Problem:**

- Selenium can be overly complicated sometimes
- Selenium does not build in functions like scrolling the screen or simple xpath searches 

**Solution:**
- By extrapolating some of the more complicated functionality up a layer we accomplish:
   - Faster Test Case Development Time
   - More readable code

---

# Instructions

**Step 1:**
Clone

**Step 2:**
Ensure your Chrome driver version matches the version of chrome you currently have installed, if not acquire from official Chrome Selenium Site

**Step 3:**
Run "build.sh" file in Git-Bash or Terminal

**Step 4:**
Run "run.sh" file in Git-Bash or Terminal

**Step 5:**
Dive into the code in either Eclipse or your favorite IDE for further learning and customization to your specific needs

---

## Authors

* **Cody Garrett** - [cody@garrett.ms](mailto:cody@garrett.ms)

Bash Script for Automation of Full Oracle Database Export

(Insert blurb here, too lazy to type a description today. Read the variables and replace values as needed. It’s pretty straightforward.)

#!/bin/bash

#Linux - Put your Dump File in this Directory 
VARIABLE_DATABASE_DIR="/DatabaseDir"

#Windows - Put your Dump File in this Directory 
#VARIABLE_DATABASE_DIR="C:/DatabaseDir"

VARIABLE_DATABASE_DUMP="myDump.dmp"
VARIABLE_PDB="MyPDB"
VARIABLE_SYS_ACCOUNT_PASSWORD="password"
VARIABLE_SYSTEM_ACCOUNT_PASSWORD="password"
VARIABLE_SCRIPT_TEMP_PATH=$(mktemp)
VARIABLE_SCRIPT_TEMP_PATH+='.sql'

echo "Generating SQL Script under $VARIABLE_SCRIPT_TEMP_PATH"

echo "/* Begin SQL Script */

alter session set container=$VARIABLE_PDB

CREATE OR REPLACE DIRECTORY DatabaseDir AS '$VARIABLE_DATABASE_DIR';

alter session set \"_ORACLE_SCRIPT\"=true;

alter pluggable database $VARIABLE_PDB close immediate;

alter pluggable database $VARIABLE_PDB open upgrade;

alter session set container=$VARIABLE_PDB;

alter system set MAX_STRING_SIZE=extended;

@?/rdbms/admin/utl32k.sql

alter pluggable database $VARIABLE_PDB close immediate;

alter pluggable database $VARIABLE_PDB open;

@?/rdbms/admin/utlrp.sql

alter session set \"_ORACLE_SCRIPT\"=true;

alter session set container=$VARIABLE_PDB;

CREATE OR REPLACE DIRECTORY DatabaseDir AS '$VARIABLE_DATABASE_DIR';

commit;

/* End SQL Script */" >> $VARIABLE_SCRIPT_TEMP_PATH

echo "EXIT;" >> $VARIABLE_SCRIPT_TEMP_PATH

VARIABLE_TIMESTAMP=$(date "+%Y-%m-%d_%H_%M_%S")
VARIABLE_LOG_FILE="$VARIABLE_DATABASE_DUMP_"
VARIABLE_LOG_FILE+=$VARIABLE_TIMESTAMP

echo "##############################################" 
echo "#### BEGIN EXECUTION OF SQL PLUS COMMANDS ####"
echo "##############################################" 
echo ""

VARIABLE_LOG_FILE_SQLPLUS=$VARIABLE_LOG_FILE.sqlplus.log

sqlplus "sys/$VARIABLE_SYS_ACCOUNT_PASSWORD@$VARIABLE_PDB as sysdba" @$VARIABLE_SCRIPT_TEMP_PATH >> $VARIABLE_LOG_FILE_SQLPLUS

echo ""
echo "#########################################" 
echo "#### BEGIN EXECUTION OF DUMP COMMAND ####" 
echo "#########################################" 
echo ""

VARIABLE_LOG_FILE_EXPDP=$VARIABLE_LOG_FILE.expdp.log

expdp "system/$VARIABLE_SYSTEM_ACCOUNT_PASSWORD@$VARIABLE_PDB" full=y directory=DatabaseDir dumpfile=$VARIABLE_DATABASE_DUMP logfile="$VARIABLE_LOG_FILE_EXPDP"