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

Example for Sending Commands to a Currently Active Screen Session

If you are one of the people that use my Docker VPN Tunneling Tool this can be used to automate login by appending it to your portion of the script.

^M represents pressing enter if unfamiliar. I’ll probably forget, that’s why I’m creating this post.

#sh dockerVpnTool.sh $OPENCONNECT_VPN_ENDPOINT $LOCAL_DOCKER_ADDRESS $LOCAL_DOCKER_SSH_PORT $CONTAINER_NAME $IMAGE_NAME "$(declare -p DESTINATION_LIST)"

#Begin Customization
echo -e "\n\n1\n" | ./dockerVpnTool.sh $OPENCONNECT_VPN_ENDPOINT $LOCAL_DOCKER_ADDRESS $LOCAL_DOCKER_SSH_PORT $CONTAINER_NAME $IMAGE_NAME "$(declare -p DESTINATION_LIST)"

function connectToEndpoint()
{
    #Begin Endpoint Customization 
    COMMAND_TO_RUN="winpty docker exec $CONTAINER_NAME bash -c"
    COMMAND_TO_RUN_INTERACTIVE="winpty docker exec -it $CONTAINER_NAME bash -c"

    SESSION_NAME="session_vpnTool"
    USERNAME='username'
    PASSWORD='password'
    CONNECTION_PROFILE='connectionProfile'

    $COMMAND_TO_RUN "screen -d -m -S $SESSION_NAME openconnect $OPENCONNECT_VPN_ENDPOINT"
    $COMMAND_TO_RUN "screen -S $SESSION_NAME -p 0 -X stuff \"$CONNECTION_PROFILE^M\""
    $COMMAND_TO_RUN "screen -S $SESSION_NAME -p 0 -X stuff \"$USERNAME^M\""
    $COMMAND_TO_RUN "screen -S $SESSION_NAME -p 0 -X stuff \"$PASSWORD^M\""

    #If Debug is Needed
    #$COMMAND_TO_RUN_INTERACTIVE "screen -r"
}

connectToEndpoint

Understanding Question Mark and Colon Operators in Java Programming

Today I was having issues understanding syntactically how ? and : operators work in Java on some legacy code I was reading. The issue was that all definitions I found through google practically sucked until I found this one by “cricket_007” on StackOverflow.

Either way, I got it figured out and am documenting this and raising awareness for other developers in the future.

Thanks so much “cricket_007” and here is the link if anyone is interested:

Custom SendTo Execution Shortcuts – Windows

Start out with doing “Windows Key + R” and type “shell:sendto”

Next, this folder will pop up:

Create a new Batch File in this directory – I’ve called mine “Launch with Bash.bat” – below is some example code:

@ECHO OFF
SETLOCAL

SET filedrive=%~d1
SET filepath=%~p1
SET filedrive_and_filepath=%filedrive%%filepath%

SET filename=%~n1
SET fileextension=%~x1
SET filename_and_fileextension=%filename%%fileextension%

cd /d %filedrive_and_filepath%
"C:\Program Files\Git\bin\bash.exe" -c "sh ""%filename_and_fileextension%"" "

Note a few things – Batch files can take a starting argument like methods in programming so basically when you right click and do “SendTo” on Windows, it sends the filepath of what you are “Sending” to whatever the destination is.

In a batch file, these parameters are accessed through %1 %2 %3 etc for each positional parameter that is being passed to the “method”

This prewritten file will allow you to create a custom process that easily allows you to launch something with a programmatic process. AKA custom builds, processes, launch parameters, etc.

Quick Docker Import/Export Utilizing gzip for Compression

IMAGE_NAME="repository:image_tag"
OUTPUT_FILE="/path/to/imageOutput.tar.gz"
#docker load < $OUTPUT_FILE
#docker save $IMAGE_NAME | gzip -c &gt; $OUTPUT_FILE

The above snippet keeps replacing the docker save line with the escaped version as &gt instead of >

AKA ->

IMAGE_NAME=”repository:image_tag”
OUTPUT_FILE=”/path/to/imageOutput.tar.gz”
#docker load < $OUTPUT_FILE
#docker save $IMAGE_NAME | gzip -c > $OUTPUT_FILE

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"