Category Archives: scripts

Powershell random password generator – with complexity!

Recently we needed to automate password randomization for account creation purposes. In order to ensure the password policy was met we needed, 8 characters total, 1 uppercase letter, 1 number, and 1 special character. We also needed to control the character set of special characters due to certain finicky applications. This script grabs a base password from http://random.org, verifies that it has all but our special character, and finally randomly inserts one of our special characters into the password.

Function Get-Seed{
# Generate a seed for future randomization
$RandomBytes = New-Object -TypeName 'System.Byte[]' 4
$Random = New-Object -TypeName 'System.Security.Cryptography.RNGCryptoServiceProvider'
$Random.GetBytes($RandomBytes)
[BitConverter]::ToInt32($RandomBytes, 0)
}

do
{
$initialPW = $(Invoke-RestMethod https://www.random.org/passwords/?num=1&len=7&format=plain&rnd=new) -replace "`n"

if($initialPW -cmatch "[A-Z]") { $containsUpper = $True } else { $containsUpper = $False }
if($initialPW -cmatch "[a-z]") { $containsLower = $True } else { $containsLower = $False }
if($initialPW -match "[0-9]") { $containsNumbr = $True } else { $containsNumbr = $False }

$specCharArray = [Char[]]"!#$&*_-"
$specChar = $($specCharArray | Get-Random -Count 1)

if (($containsUpper -eq $True) -and ($containsLower -eq $True) -and $($containsNumbr -eq $True))
{
$seed = $(Get-Random -Maximum 7 -Minimum 1 -SetSeed $(Get-Seed))
$initialPW.Insert($seed,$specChar)
break
}
}
While ($True)

OpenStack API python script example

Recently I was given a number of API test cases for OpenStack to manually execute via curl. It was quite tedious figuring out the nuances of the OpenStack API using curl; manually constructing HTTP headers and substituting JSON values into a curl command got old real fast. I decided to run all the test cases through python, this was the first bit I had to finish, authentication!

OpenStack API call requesting a token. Tokens are valid for authentication for 24 hours:

#!/usr/bin/python

import argparse
import getopt
import json
import sys
import urllib2

def getToken(url, osuser, ostenant, ospassword):

    """
    Returns a token to the user given a tenant,
    user name, password, and OpenStack API URL.
    """

    url = url + '/tokens'
    tokenRequest = urllib2.Request(url)
    tokenRequest.add_header("Content-type", "application/json")
    jsonPayload = json.dumps({'auth' : {'tenantName' : ostenant, 'passwordCredentials' : {'username' : osuser, 'password' : ospassword}}})
   
    request = urllib2.urlopen(tokenRequest, jsonPayload)
    json_data = json.loads(request.read())
   
    request.close()
    return json.dumps(json_data)

# Build our required arguments list
parser = argparse.ArgumentParser()
mandatory = parser.add_argument_group("mandatory")
mandatory.add_argument("-l", "--login", help="The administrative user for your OpenStack installation", type=str)
mandatory.add_argument("-p", "--password", help="The administrative user's password", type=str)
mandatory.add_argument("-t", "--tenant", help="The administrative user's tenant / project", type=str)
mandatory.add_argument("-u", "--url", help="The Keystone API endpoint from running, 'nova endpoints'", type=str)
args = parser.parse_args()

# Validate arugments were given
if type(args.url) != type(str()):
    sys.stderr.write('Invalid URL: %s\n' % args.url)
    parser.print_help()
    sys.exit(2)
if type(args.tenant) != type(str()):
    sys.stderr.write('Invalid tenant: %s\n' % args.tenant)
    parser.print_help()
    sys.exit(2)
if type(args.password) != type(str()):
    sys.stderr.write('Invalid password: %s\n' % args.password)
    parser.print_help()
    sys.exit(2)
if type(args.login) != type(str()):
    sys.stderr.write('Invalid login: %s\n' % args.login)
    parser.print_help()
    sys.exit(2)
   
# Since we return a raw JSON payload from getToken,
# we need to load it into a readable object.
adminToken = json.loads(getToken(args.url, args.login, args.tenant, args.password))

# Access the token portion of the JSON payload and grab the token and tenant ID
adminTokenID = adminToken['access']['token']['id']
adminTokenTenantID = adminToken['access']['token']['tenant']['id']

for item in adminToken['access']['serviceCatalog']:
    """
    The "name" of each OpenStack service catalog item from
    the item list changed between versions.  Things like
    "glance" became "volume" and "keystone" became "identity".  
    You will need to update this based on your installation.
    """

    if item['name'] == "nova":
        adminNovaURL = item['endpoints'][0]['adminURL']
    if item['name'] == "glance":
        adminGlanceURL = item['endpoints'][0]['adminURL']
    if item['name'] == "nova-volume":
        adminVolumeURL = item['endpoints'][0]['adminURL']
    if item['name'] == "ec2":
        adminEc2URL = item['endpoints'][0]['adminURL']
    if item['name'] == "swift":
        adminSwiftURL = item['endpoints'][0]['adminURL']
    if item['name'] == "keystone":
        adminAuthURL = item['endpoints'][0]['adminURL']

print "------ Admin information ------"
print "Token ID = ", adminTokenID
print "Tenant ID = ", adminTokenTenantID
print "Nova URL = ", adminNovaURL
print "Glance URL = ", adminGlanceURL
print "Volume URL = ", adminVolumeURL
print "EC2 URL = ", adminEc2URL
print "Swift URL = ", adminSwiftURL
print "Keystone URL = ", adminAuthURL
print "------ End Admin info ---------"

When passed the appropriate parameters, this script will print your environment information in a nice readable format:

uesr@localhost:~$ ./token.py -l admin -t admin -p password -u http://localhost:35357/v2.0
------ Admin information ------
Token ID =  abc123def456ghi789jkl012mno345pq
Tenant ID =  qp543onm210lkj987ihg654fed321cba
Nova URL =  http://localhost:8774/v2/qp543onm210lkj987ihg654fed321cba
Glance URL =  http://localhost:9292/v1
Volume URL =  http://localhost:8776/v1/qp543onm210lkj987ihg654fed321cba
EC2 URL =  http://localhost:8773/services/Admin
Swift URL =  https://localhost:8080/v1/
Keystone URL =  http://localhost:35357/v2.0
------ End Admin info ---------

vSphere PowerCLI – slot metrics

We’re not talking about coin slots here people! vSphere 5 uses slot sizes to determine the capacity for high availability (HA) fail-over metrics. Here’s a quick and dirty way to see what your current slot size is:

Connect-VIServer <vCenterServerName>
$Cluster = Get-Cluster -Name <clusterName>
$SlotDetails = $Cluster.ExtensionData.RetrieveDasAdvancedRuntimeInfo()
Write-Host -ForegroundColor Green "`n Slot info for <clusterName> cluster `
`n Number of vCPUs per slot: $($SlotDetails.SlotInfo.NumvCpus) `
`n MHz per slot: $($SlotDetails.SlotInfo.CpuMHz) `
`n Memory (MB) per slot: $($SlotDetails.SlotInfo.MemoryMB) `
`n Total Slots = $($SlotDetails.TotalSlots) `
`n Used Slots = $($SlotDetails.UsedSlots) `
`n Available Slots = $($SlotDetails.TotalSlots - $SlotDetails.UsedSlots)"

Be sure and replace <vCenterServerName> with your vCenter server name and <clusterName> with the HA cluster name for which you need slot information. This can be useful when determining your current fail-over capacity, just be sure your slot size is adequate for your environment. You should see output similar to the following:

Name                           Port  User
----                           ----  ----
<vCenterServerName>            443   <user>

 Slot info for <clusterName> cluster
 Number of vCPUs per slot: 2
 MHz per slot: 500
 Memory (MB) per slot: 1024
 Total Slots = 250
 Used Slots = 32
 Available Slots = 218

PowerCLI C:\temp>

Thanks to Alan over at http://www.virtu-al.net/

Weblogic Linux service script

Seeing as how a ton of the work I do these days is with Weblogic, I figured it would come in handy to have an init script tucked away for a rainy day. Recently we had an Oracle consultant in to deploy Oracle Business Intelligence, Enterprise Edition and while I can say the consultant knew too little about systems, in the same breath I would say he knows too much about OBIEE. He put together an init script for OBIEE which is based on Weblogic so I borrowed and mangled what he started with to fit my needs and this was the outcome.

#!/bin/bash
# Weblogic start / stop script.
#
# chkconfig: 345 99 10
# description: Manage Weblogic services.
#


SRVC_OWNR=<user>                    # Local Unix user running Weblogic
WL_BIN=/path/to/Oracle/Middleware   # Deployment root directory
WL_USER=wl_admin_user               # WL administrator name
WL_PASSWD=<password>                # WL administrator password
WL_DOMAIN=<wl_domain>               # Domain name
Mgmt_SERVER=<admin_server>          # Admin server name
MS_SERVER=<ms1>                     # Managed server name
ADMIN_URL=<host>:<port>             # Admin server URL (hostname:port)
MS_URL=<host>:<port>                # Managed server URL (hostname:port)
WL_PATH=$WL_BIN/wlserver_10.3/server/bin
WL_DOMAIN_PATH=$WL_BIN/user_projects/domains/$WL_DOMAIN/bin
export WL_DOMAIN_PATH

START_LOG=/var/log/wl-start.log
STOP_LOG=/var/log/wl-stop.log
SUBSYS=wl_svcs

start() {

    echo "********************************************************************************"
    echo "Starting Node manager on $(date)"
    echo "********************************************************************************"
    su $SRVC_OWNR -c "$WL_PATH/startNodeManager.sh" &
    wait_for "Secure socket listener started on port 5556"

    echo "********************************************************************************"
    echo "Starting Admin Server on $(date)"
    echo "********************************************************************************"
    su $SRVC_OWNR -c "$WL_DOMAIN_PATH/startWebLogic.sh" &
    wait_for "Server started in RUNNING mode"

    echo "********************************************************************************"
    echo "Starting Managed Server $MS_SERVER on $(date)"
    echo "********************************************************************************"
    su $SRVC_OWNR -c "$WL_DOMAIN_PATH/startManagedWebLogic.sh $MS_SERVER http://$ADMIN_URL" &
    wait_for "Server started in RUNNING mode"

    echo "********************************************************************************"
    echo "Weblogic start sequence completed on $(date)"
    echo "********************************************************************************"
}
stop() {

    echo "********************************************************************************"
    echo "Stopping Managed Server $MS_SERVER on $(date)"
    echo "********************************************************************************"
    su $SRVC_OWNR -c "$WL_DOMAIN_PATH/stopManagedWebLogic.sh $MS_SERVER t3://$ADMIN_URL $WL_USER $WL_PASSWD"

    echo "********************************************************************************"
    echo "Stopping Admin Server $Mgmt_SERVER on $(date)"
    echo "********************************************************************************"
    su $SRVC_OWNR -c "$WL_DOMAIN_PATH/stopWebLogic.sh"

    echo "********************************************************************************"
    echo "Stopping Node manager on $(date)"
    echo "********************************************************************************"
    su $SRVC_OWNR -c "kill -9 $( ps -ef| grep weblogic.NodeManager | grep -v grep | awk '{print $2}' )"

    echo "********************************************************************************"
    echo "Weblogic stop sequence completed on $(date)"
    echo "********************************************************************************"
}

wait_for() {
    res=0
    while [[ ! $res -gt 0 ]]
    do
        res=$(tail -5 "$START_LOG" | fgrep -c "$1")
        sleep 5
    done
}

case "$1" in
    start)
        echo "********************************************************************************"
        echo "Starting Weblogic Components on $(date)"
        echo "Logs are sent to $START_LOG"
        echo "********************************************************************************"
        start &> $START_LOG &
        touch /var/lock/subsys/$SUBSYS
    ;;
    stop)
        echo "********************************************************************************"
        echo "Stopping Weblogic Components on $(date)"
        echo "Logs are sent to $STOP_LOG"
        echo "********************************************************************************"
        stop &> $STOP_LOG
        rm -f /var/lock/subsys/$SUBSYS
        ;;
    *)
        echo "Usage: $(basename $0) start|stop"
        exit 1
esac

exit 0

If you’ve ever worked with Weblogic the script should be fairly self explanatory. The script starts node manager, waits for completion, then moves on to an admin server and managed server; comment one or the other out based upon your needs.

how to: cookies with javascript

I love cookies – that’s all the personality you’re going to get for this post. Here’s the javascript / html form pair you need to create and manage browser cookies:

function SetCookie(){

if (document.cookie && document.cookie != "") {
// Cookie exists - let's append to it!

// Grab "Field 1" from the form and assign it to item1
    var item1=document.getElementById('ctest0');
// Grab "Field 2" from the form and assign it to item2
    var item2=document.getElementById('ctest1');
    var cookie=document.cookie;

/*
Get the actual text from the fields via the item# variables .value property
and assign it to a string that we can later split via the # sign in the cookie
itself!
*/

    var newCookie=cookie + "#field1=" + escape(item1.value);
    newCookie=newCookie + "#field2=" + escape(item2.value);
   
// Generate the cookie
    document.cookie=newCookie;
   
}
else
{
// Cookie does not exists - create a new one!

// Same as above except we also name our cookie
    document.cookie="name=stuff";

// Grab "Field 1" from the form and assign it to item1
    var item1=document.getElementById('ctest0');
// Grab "Field 2" from the form and assign it to item2
    var item2=document.getElementById('ctest1');
    var cookie=document.cookie;

/*
Get the actual text from the fields via the item# variables .value property
and assign it to a string that we can later split via the # sign in the cookie
itself!
*/

    var newCookie=cookie + "#field1=" + escape(item1.value);
    newCookie=newCookie + "#field2=" + escape(item2.value);
   
// Generate the cookie
    document.cookie=newCookie;
}  

}

function GetCookie(){

/*
Cookie fields are stored in item=value pairs.  We separate the fields
by a hash sign in the SetCookie() function therefore we'll split the
entire cookie into an array of item=value pairs via the cookieArray
variable assignment.  Then we have each field in a item=value pair as
data inside each array element.  Next you'll need to parse the array
as done in the for loop below.
*/


var cookieToText,cookieName,fieldData,cookieArray=document.cookie.split('#');
// Field counter - used for display purposes

// For loop to parse through each item=value pair in the cookie
for (i=0;i<cookieArray.length;i++)
    {
    if (i=="0")
        {
       
        /*
        The first field in a cookie is always the cookie name.  Let's add that
        to our "cookie to text" conversion variable for ease of use!  Also we
        know the name of the cookie since we set it previously.  The word "Stuff"
        is 5 characters long thus we strip out 5 characters from the second field
        (The data) in the item=value pair.
        */

        cookieName="Cookie Name: "
        // Split the item=value pair
        fieldData=cookieArray[i].split('=');
        // Grab the name of the cookie
        cookieToText=cookieName + fieldData[1].substr(0,5) + "\n";
                       
        }
        else
        {
        /*
        These are data items.  Let's put them on top of the "cookie to text"
        conversion variable for later display.  You have to "unescape" cookie
        data as it is encoded.
        */

       
        fieldData=cookieArray[i].split('=');
        cookieToText=cookieToText + "Field " + i + ": " + unescape(fieldData[1]) + "\n"
                       
        }

}
// Finally - display the "cookie to text" conversion variable:
alert(cookieToText);
}

Here’s the form you need to test your javascript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
<script type="text/javascript" src="cookie.js"></script>
<form name="srf" id="srf" method="post" action=""> 
Field 1: <input id="ctest0" name="ctest0" type="text" maxlength="255" value=""/><br/>  
Field 2: <input id="ctest1" name="ctest1" type="text" maxlength="255" value=""/><br/>
<input id="ctest2" type="button" name="setCookie" value="Set Cookie" onclick="SetCookie()"/><br/>  
<input id="ctest3" type="button" name="getCookie" value="Get Cookie" onclick="GetCookie()"/><br/>  
</form>
</html>