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"

Oracle SQL Utility Script for Unlock of All Accounts and Removal of Password Expiration

I’ve gotten tired of dealing with this on various development databases so I wrote a script to automagically remove password expiration and unlock all schema accounts.

Below is the script – verified working as of Oracle 12c

declare
sql_statement varchar2(4000);

BEGIN
  FOR profileObject IN (select DISTINCT profile from DBA_USERS)
  LOOP
    BEGIN
        sql_statement := ' alter profile ' || profileObject.PROFILE || ' limit password_life_time UNLIMITED';
        execute immediate sql_statement;
        dbms_output.put_line('SQL Statement Succeeded - ' || sql_statement);
    COMMIT;
    EXCEPTION
    when others then 
        dbms_output.put_line('SQL Statement Failed - ' || sql_statement);
    END;
  END LOOP;
  
  FOR sysUserObject IN (select * from sys.user$)
  LOOP
    BEGIN
        sql_statement := 'ALTER USER ' || sysUserObject.name ||' IDENTIFIED BY VALUES '''|| sysUserObject.spare4 ||';'|| sysUserObject.password || '''';
        execute immediate sql_statement;
        dbms_output.put_line('SQL Statement Succeeded - ' || sql_statement);
    COMMIT;
    EXCEPTION
    when others then 
        dbms_output.put_line('SQL Statement Failed - ' || sql_statement);
    END;
  END LOOP;
  
  FOR userObject IN (select DISTINCT USERNAME from DBA_USERS)
  LOOP
    BEGIN
        sql_statement := 'alter user ' || userObject.USERNAME || ' account unlock';
        execute immediate sql_statement;
        COMMIT;
        dbms_output.put_line('SQL Statement Succeeded - ' || sql_statement);
        EXCEPTION
        WHEN OTHERS THEN dbms_output.put_line('SQL Statement Failed - ' || sql_statement);
    END;
  END LOOP;
  COMMIT;
END;

Git Push Fails – fatal: The remote end hung up unexpectedly

Had this issue today and found the below solution.

Credit to Atlassian for documenting – I am reposting and archiving the fix for my own records.

https://confluence.atlassian.com/staskb/git-push-fails-fatal-the-remote-end-hung-up-unexpectedly-282988530.html

Symptoms

When users try to run “git push” the following error message is shown:

$ git push
Counting objects: 2332669, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (360818/360818), done.
error: RPC failed; result=22, HTTP code = 411
fatal: The remote end hung up unexpectedly
Writing objects: 100% (2332669/2332669), 483.30 MiB | 114.26 MiB/s, done.
Total 2332669 (delta 1949888), reused 2330461 (delta 1949349)
fatal: The remote end hung up unexpectedly

Cause

The “Smart HTTP” protocol in Git uses “Transfer-Encoding: chunked” in POST requests when it contains packed objects greater than 1MB in size.

Some proxy servers, like Nginx, do not support this transfer encoding by default, and the requests will be rejected before they get to Stash. Because of this, the Stash logs will not show any extra information.

Workaround

  • When pushing a large amount of data (initial push of a big repository, change with very big file(s)) may require a higher http.postBuffer setting on your git client (not the server). From https://www.kernel.org/pub/software/scm/git/docs/git-config.htmlhttp.postBufferMaximum size in bytes of the buffer used by smart HTTP transports when POSTing data to the remote system. For requests larger than this buffer size, HTTP/1.1 and Transfer-Encoding: chunked is used to avoid creating a massive pack file locally. Default is 1 MiB, which is sufficient for most requests.
  • Configuration on your reverse proxy. Usually ngnix the parameter client_max_body_size is a blocker. The reverse proxy may also have a connection timeout that’s closing the connection (e.g. TimeOut or ProxyTimeout in apache, proxy_read_timeout in ngnix). Try bypassing the proxy by pushing directly to Stash IP:port. If this works, it’s highly likely that the proxy server is causing the early disconnect and needs to be tuned.
  • User is using an outbound proxy on his machine that is causing the issue.

Resolution

Finding the Process that has a Lock on a File when you don’t have Administrator

Most of the time Microsoft Windows experts will immediately point you to Sysinternals Process Explorer. Unfortunately for a vast majority of us, this requires Administrator privileges on your machine and in today’s security conscious society, System Admin give this out less and less.

Enter this solution I found from Stack Overflow user Svish.

The following is a direct copy paste of the post that you can find at this link for citation purposes.

You can use the Resource Monitor for this which comes built-in with Windows 7, 8, and 10.

  1. Open Resource Monitor, which can be found
    • By searching for Resource Monitor or resmon.exe in the start menu, or
    • As a button on the Performance tab in your Task Manager
  2. Go to the CPU tab
  3. Use the search field in the Associated Handles section
    • See blue arrow in screen shot below

When you’ve found the handle, you can identify the process by looking at the Image and/or PID column.

You can then try to close the application as you normally would, or, if that’s not possible, just right-click the handle and kill the process directly from there. Easy peasy!

Resource Monitor screenshot