Dec 29

BackupRouters-TelnetToLocal.pl

#!/usr/bin/env perl
#=========================================================================
# BackupRouters-TelnetToLocal.pl
# VERSION: 1.0
# AUTHOR: Brian Steinmeyer
# EMAIL: sigkill@sigkillit.com
# WEB: http://sigkillit.com
# DATE: 12/29/2012
# COMMENTS: Uses the Telnet::Cisco module to copy a Cisco Router's running
# configuration to the local machine. This module uses telnet so passwords
# will be in clear text. Pass the Cisco Router's DNS name or IP, logfile name,
# username, and password to the Sub. The script will back up the running
# config to the scripts location.
#=========================================================================
use Net::Telnet::Cisco;
use File::Basename;
use DateTime;

#Backup Routers
BackupRouterRunningConfig('192.168.1.1', 'router-config', 'USERNAME', 'PASSWORD');

sub BackupRouterRunningConfig {

    #Set Variables
    $router = $_[0];
    $logfile = $_[1] . DateTime->now()->strftime('%m-%d-%y_%H%M%S') . '.txt';
    $username = $_[2];
    $password = $_[3];

    #Login to Router
    print "\n\n" . $router . "\n*************************\n";
    my $session = Net::Telnet::Cisco->new(Host => $router, Errmode => "return");
    if(! $session->login($username, $password)) {
        print "ERROR Logging Into $router\n";
    } else {
        print "SUCCESS Logging Into $router\n";

        #Ensure Router is in Enabled Mode
        if($session->is_enabled == 1) {
            print "SUCCESS Router in Enabled Mode\n";

            #Avoid Autopaging
            $session->cmd('terminal length 0'); # Avoid Autopaging         

            #Grab Router Running-Config
            my @arrOutput = $session->cmd("show running-config");
            my $arrSize = @arrOutput;
            if($arrSize > 0) {
                print "SUCCESS backing up configuration\n";
                open FILE, ">", $logfile or die $!;
                print FILE @arrOutput;
                close FILE;
            } else {
                print "ERROR backing up configuration\n";
            }          
            $session->close;
        } else {
            print "ERROR Router Not in Enabled Mode\n";
        }
    }  
}

 

Dec 29

Perl Show Current Script Name

If you ever needed to show the currently running Perl script name, here are the examples you need.  I personally like using the following to name log files based on the script name by replacing the extension:

use File::Basename;
#Display Current Script Name
my $scriptName = basename($0);
print $scriptName
#Display Current Script Name with Different Extension - Usefull for Naming Log Files
print basename($scriptName, ".pl") . ".txt";

 

Dec 28

Recover Private Key for SSL Certificates in IIS

Problem:

Your SSL Certificate is installed but missing the private key. At the time of this writing, this applies to:

  • IIS5
  • IIS6
  • IIS7

Causes:

There can be multiple causes from deleting the original SSL certificate to your server crashing and not having a backup of the private key.

Resolution:

  1.  Log on to the computer that issued the certificate request by using an account that has administrative permissions.
  2. Click Start, click Run, type mmc, and then click OK.
  3. On the File menu, click Add/Remove Snap-in.
  4. In the Add/Remove Snap-in dialog box, click Add.
  5. Click Certificates, and then click Add.
  6. In the Certificates snap-in dialog box, click Computer account, and then click Next.
  7. In the Select Computer dialog box, click Local computer: (the computer this console is running on), and then click Finish.
  8. Click Close, and then click OK.
  9. In the Certificates snap-in, expand Certificates, right-click the Personal folder, point to All Tasks, and then click Import.
  10. On the Welcome to the Certificate Import Wizard page, click Next.
  11. On the File to Import page, click Browse.
  12. In the Open dialog box, click the new certificate, click Open, and then click Next.
  13. On the Certificate Store page, click Place all certificates in the following store, and then click Browse.
  14. In the Select Certificate Store dialog box, click Personal, click OK, click Next, and then click Finish.
  15. In the Certificates snap-in, double-click the imported certificate that is in the Personal folder.
  16. In the Certificate dialog box, click the Details tab.
  17. Click Serial Number in the Field column of the Details tab, highlight the serial number, and then write down the serial number.
  18. Click Start, click Run, type cmd, and then click OK.
  19. At the command prompt, type the following:
    1. certutil -repairstore my “SerialNumber”
    2. NOTE: SerialNumber is the serial number that you wrote down in step 17.
  20. In the Certificates snap-in, right-click Certificates, and then click Refresh.

 

The certificate now has an associated private key, and you can assign to a website in IIS.  When completed, you should see the private key on the certificate as below:

SSL Certificate with a Private Key

Dec 21

ListRunningScripts.vbs

'=========================================================================
' ListRunningScripts.vbs
' VERSION: 1.1 - Null value is returned when a script is launched by another user on the local computer and the script is not elevated for UAC. I added a check to look for this and alert the user.
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 12/21/2012
' COMPATIBLE: Windows XP, Server 2003, and Above
' COMMENTS: If you ever had a wscript.exe or cscript.exe process running but
' need to know the exact script it is running you can find the answer using
' the CommandLine property in the WIN32_Process Class. You can view this
' property in Task Manager by clicking View->Choose Columns. Alternatively,
' you can script it as I have below. The script works on the local computer
' as well as remote computers. Set the computer to run the script against
' and optionally set it to show the Scripting Host that launched the script.
' EXAMPLE: List Running Scripts on Local Computer Without Script Host
'          Dim strComputer: strComputer = "."
'          Dim blnShowScriptHost: blnShowScriptHost = False
' EXAMPLE: List Running Scripts on Local Computer With Script Host
'          Dim strComputer: strComputer = "."
'          Dim blnShowScriptHost: blnShowScriptHost = True
' EXAMPLE: List Running Scripts on Remote Computer Without Script Host
'          Dim strComputer: strComputer = "server"
'          Dim blnShowScriptHost: blnShowScriptHost = False
' EXAMPLE: List Running Scripts on Remote Computer With Script Host
'          Dim strComputer: strComputer = "server"
'          Dim blnShowScriptHost: blnShowScriptHost = True
'=========================================================================
Option Explicit
' ------ SCRIPT CONFIGURATION ------
Dim strComputer: strComputer = "."
Dim blnShowScriptHost: blnShowScriptHost = False
' ------ END CONFIGURATION ------

Wscript.Echo ListRunningScripts(strComputer)

Private Function ListRunningScripts(strComputer)

    On Error Resume Next

    Dim objWMIService: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Dim colProcesses: Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = " & "'Wscript.exe' OR Name = 'Cscript.exe'")
    Dim objProcess
    Dim strResult: strResult = ""
    Dim count: count = 0
    If Not colProcesses.Count = 0 Then
        For Each objProcess in colProcesses
            count = count + 1
            If blnShowScriptHost = True Then
                strResult = strResult & objProcess.CommandLine & vbCrLf
            Else
                If IsNull(objProcess.CommandLine) OR IsNull(objProcess.ExecutablePath) Then
                    strResult = strResult & "NULL (You need to use Run As Administrator)" & vbCrLf
                Else
                    strResult = strResult & Trim(Replace(Replace(objProcess.CommandLine, objProcess.ExecutablePath, "", 1, 1, 1), objProcess.Name, 1, 1, 1)) & vbCrLf
                End If
            End If
        Next
    End If

    strResult = count & " Scripts are running" & vbCrLf & "----------------------" & vbCrLf & strResult

    If Err.Number <> 0 Then
        Err.Clear
        strResult = "!~ERROR~!"
    End If

    ListRunningScripts = strResult

    On Error Goto 0

End Function

 

Dec 08

IDK.vbs

'=========================================================================
' IDK.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 12/8/2012
' COMMENTS: If you have ever had an issue making a decision like "What Should
' I eat for dinner" or "What should I do tonight?" then IDK aka "I Don't Know"
' is the script for you. It will randomly select a decision for you from a line
' delimited text file you pass to the script. You can run the script with Wscript
' by dragging and dropping your file onto the script or with Cscript by using
' the file path as the argument passed. You can make lists for all types of
' decisions you commonly make, and a few ideas are Yes/No, Magic8Ball, Pizza Shops,
' Restaurants, Movies, etc.
' EXAMPLE: Drag-N-Drop with WScript
'          Drag and Drop a file called Pizza.txt onto IDK.vbs to select a pizza shop
' EXAMPLE: From and command prompt with Cscript
'          C:\>cscript IDK.vbs "C:\My Lists\pizza.txt"
'=========================================================================
Option Explicit

Wscript.Echo IDK(wscript.arguments.item(0))

Private Function IDK(filePath)

    On Error Resume Next

    Const ForReading = 1
    Dim strResult
    Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FileExists(filePath) Then
        strResult = "ERROR - Input File Does Not Exist!"
    Else
        'Parse Input File into Array
        Dim objTextFile: Set objTextFile = objFSO.OpenTextFile(filePath, ForReading)
        Dim arrLine()
        Dim intSize: intSize = 0
        Dim strLine, blnMatch: blnMatch = False
        Do Until objTextFile.AtEndOfStream
            strLine = Trim(objTextFile.Readline)
            If Not strLine = "" Then
                blnMatch = True
                ReDim Preserve arrLine(intSize)
                arrLine(intSize) = strLine
                intSize = intSize + 1
            End If
        Loop
        'Randomly Select
        Dim intHighNumber: intHighNumber = UBound(arrLine)
        Dim intLowNumber: intLowNumber = LBound(arrLine)
        Dim intNumber
        Randomize
        strResult = arrLine(Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber))      
    End If

    'Return Results
    If Err.Number <> 0 Then
        Err.Clear
        strResult = "ERROR - Parsing Input File!"
    End If 
    IDK = strResult

    Set objFSO = Nothing

    On Error Goto 0

End Function

 

Dec 07

Cannot Connect to Windows 7 SMB Shares from XP/2003

If you’ve ever tried connecting to a Windows 7 SMB share from a Windows XP or 2003 computer, you may notice you cannot connect to the server. Being the savvy person you are, you:

  1. Check the share and NTFS permissions on the Windows 7 Share
  2. Run gpresult on your XP/2003 machine and confirm you are a member of the correct groups
  3. Try connecting through start->Search and enter \\windows7 and get an error
    1. What! You cannot even see the server and it’s shares???
  4. You check the Windows 7 firewall and ensure file/print sharing is allowed
    1. It’s even enabled on the connected network (domain/home/public)
    2. Your IP subnet is even allowed
  5. You break out the command line and run net use z: \\windows7\share but get a System Error 58

WHAT GIVES!!!

The problem is Windows 7 uses NTLMv2 and XP/2003 use LM & NTLM.  To resolve this issue, do the following on the Windows 7 computer:

  1. Run GPedit.msc
  2. Expand Computer Configuration->Windows Settings->Security Settings->Local Policies->Security Options
  3. Open the properties for Network security: LAN Manager authentication level
    1. Set the value to: Send LM & NTLM – use NTLMv2 session if negotiated
  4. Run Services.msc
    1. Restart the Server service

You can now map your drive from your XP/2003 box to your Windows 7 computer!