OK back for another shout

Well work still busy, and still struggling to find the time to study for my ROUTE exam. But that does not mean I have been doing nothing.

Work sent me on my ITIL Foundation course last week and please to say I passed, so thats one more string to the bow. I was suprised it was a lot more intresting that I thought, and while it still was in my view ‘common sence’. seeing it all togather with all the termonology, does mean I can now look back on some conversations I have had in the past and make some sence of them. For any one looking to get in to the managed service side of things, or wants a foot in the door for such company, it going to be a good cert to have.

The other thing that has been keeping me away from the CCNP is learning scripting and c#. I have already talked about writing some VB scripts to control Secure CRT. but of course to do this you need CRT installed on the PC in the first place. So taking it one step further I started looking at SecureCRT client pack, this is a instulation, that once installed on a PC, you can then call the functions when programing in any .net language, such as VB.net or C#.net This gives much more control over the application and gives the end user a much cleaner interface to use. However you still need to have the client pack installed and this costs money and the hassle of instulation.

So my next stop was Sharp SSH , this is a free SSH libuary with some simmler funcanality to the SecureCRT function pack (for what I want) and allows eaiser distribution of the final aplication.

So after a few steps, and lots of playing around, I now have the core understanding I need to start creating my vision. So as well as working on my CCNP, looking after my Daugher, and atempting to move house. Not forgetting the extra hours that come from managing at work. I am working on creating an application that will allow you to create a libruay of scripts you wish for CISCO IOS/CATOS devices. Including any pre / post checks you require and how to ack on there out comes, Plus allow you to define any varibles that will change between runs. Then allow you to feed in the varibles at run time (either manualy or via a bulk method), and carry out the work completely automated.

Really this is jsut a project for me to learn some more in depth scripting / programing, but as people who carry out mutiply changes, this can often be a repetive job, so I am hoping once complete I will end up with a tool that is very useful for my current postition.

So if you wondered where I was thats about sums it up. Life is great just very busy, and just waiting to get our house sorted, then I will be setting up a nice quiet area to study and getting stuck right back in to CCNP and getting back in to updating and adding to this blog.

night all

DevilWAH

PS. Oh and Miss Lilith is now walking, from bum shuffling to walking has taken less than 10 days, and not only is she walking but climbing to!! she is very proud to show how she can climb on to the couch, and thinks it is very funny to give Mum and Dad heart attacks by almost falling. But its a great time and wonderful watching her grow. being waved good bye to in the mornings, and greated with a huge smile and laughter when I get home, make life very special at the moment. 🙂

VB again

Well again its been a while since I posted, and once again I have been sidelined from CISCO studies.

A few weeks back I was asked to audit 1500 ports for a client, so thre was no way I was doing that by hand, I am a manger after all you know ;). But it was a great excuse to great a bit of scripting experience in scripting / programming.

Secure CRT has great scripting support and after a few hours playing around I had a script that can read from an excel spread sheet and carry out a list of checks and record the results.

1500 ports spread over 260 devices = 15 min, compared to the 20hr + I worked out it could have taken by hand including preparing the report.

But this lead me a bit further, what would be nice is a program that you could input any amount of code, where varibles where highlighted like.


conf t
int ***port***
description ***des***
speed ****speed****

along with a spread sheet that contained columns containing the variables, and the program could loop through the rows implementing each line as it goes.

In my job there is the potential of having to implement the same config on multiply devices with different values (ie assign ports to vlans)

So for the last few weeks, VBA, VB script and C# have been taking up my life, its been great fun and while maybe not my CCNP still good skills to have as a network engineer to be able to automate things and increase throughput.

hope to be back soon with more updates…

On and other news…. Lilith was one at the weekend, happy birthday my beautiful little girl, you are and angel and very much loved by me and your mum 🙂 XX

DevilWAH

And now for some thing completely different…. (VBscript to show logged on users)

Now let me start by saying I am no scripting guru, in fact apart from a few batch files and a C++ course many years ago I am a novice. However I have the ideas of how scripting / programming works and over the years have managed to throw to gather a few basic scripts.

I did not intend this blog to ever contain scripting (or at least not for a few years yet), but I came across this in work and thought others might find it useful. Now before we start this is a rehash of others work, links to there sites are at the bottom of the post and I would like to thank them for posting there work to the public.

The background to this is that I am currently working on a project to implement grid computing to process long running mathematical modelling jobs. The software CONDOR is a job scheduling application running on a server. The mathematician break there jobs up in to small chunks (maybe severely hundred jobs in total), and the CONDOR server finds available desktop PC’s they are inactive and sends the job to run on them. IF you have used “folding at home” or “Boinc” you will have been the client in the same kind of system.

Now while CONDOR can determine the user activity in terms of keyboard/mouse activity and CPU usage to decided if a computer is free to run jobs, there is no built in way to use the logged in status of the PC to control jobs. The idea is we want jobs to run when users log out of there PC’s, but stop and be prevented from running once a user logs in. To do this I needed to create a script that could return a “True” or “False” result to the question “is any one logged in to this computer”.

My first Idea was PSloggedon from Sysinternals, however this does not return a true false value and would require wrapping up in a second script to parse the output. So I decided to search around for a VB script that could do it all in one step.

This lead me to the following code,

strComputer = "servername"   ' " use "." for local computer 

Set objWMI = GetObject("winmgmts:" _ 
              & "{impersonationLevel=impersonate}!\\" _ 
              & strComputer & "\root\cimv2") 

Set colSessions = objWMI.ExecQuery _ 
    ("Select * from Win32_LogonSession Where LogonType = 10") 

If colSessions.Count = 0 Then 
   Wscript.Echo "No interactive users found" 
Else 
   WScript.Echo "RDP Sessions:"
   For Each objSession in colSessions 
     
     Set colList = objWMI.ExecQuery("Associators of " _ 
         & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _ 
         & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) 
     For Each objItem in colList 
       WScript.Echo "Username: " & objItem.Name & " FullName: " & objItem.FullName 
     Next 
   Next 
End If 

Not exactly what I need but at least it will find and display the logged in users, so seems a good starting place, and easy to tidy up.

First of all we don’t want it to tell us a list of users, just return a true of false statement. This is just a case of replacing Wscript.Echo "No interactive users found" with Wscript.Echo "False" , and replacing the whole of the code under the else statment with simply Wscript.Echo "True".

Secondly this script uses the statment “LogonType = 10” from windowsecurity.com we can see that this will give us remote connections, as we want local logged on users we need to replace that with “LogonType = 2”. Ending up with the revised code below.


strComputer = "."   ' " use "." for local computer 
Set objWMI = GetObject("winmgmts:" _ 
           & "{impersonationLevel=impersonate}!\\" _ 
           & strComputer & "\root\cimv2")
Set colSessions = objWMI.ExecQuery _ 
  ("Select * from Win32_LogonSession Where LogonType = 2") 
     
If colSessions.Count = 0 Then     
  Wscript.Echo "FALSE" 
Else
  WScript.Echo "True"
  
End If

Oh and notice we needed to replace the servername with “.” as suggested. Saving this as a .vbs file and running it and a pop up box will display “True” (after all you are logged on to the PC 😉 )

And that was my script done..

Apart from the fact for it to work in CONDOR I needed it to be able to return out put to stdout, which requires it to be running under Cscript.exe (cscrfipt is command line, Wsccript will interact with windows, in this script one will out put to the command line, one will pop up the same out put but in a small window). Now there are several ways to do this, simple run it from the command line by calling it using cscript.exe c:\csccript.exe script.vbs , or you can wrap it up in a batch file. Howevver I came across a nice little trick to enable the script to check if it is run as cscript and if not switch to csccript.


Public objShell : Set objShell = CreateObject("WScript.Shell")
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
    objShell.Run "cscript " & chr(34) & WScript.ScriptFullName & chr(34) & " //Nologo" & chr(34), 0, False
 
    WScript.Quit
End If

Simply pasting this code above the main script, (And I also updated the Wscript.echo to be

Set objStdOut = WScript.StdOut
  objStdOut.Write "UserLoggedIn = True"  

for CONDOR reasons, and we end up with the complete code below.


Public objShell : Set objShell = CreateObject("WScript.Shell")
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
    objShell.Run "cscript " & chr(34) & WScript.ScriptFullName & chr(34) & " //Nologo" & chr(34), 0, False
 
    WScript.Quit
End If
strComputer = "."   ' " use "." for local computer 
Set objWMI = GetObject("winmgmts:" _ 
           & "{impersonationLevel=impersonate}!\\" _ 
           & strComputer & "\root\cimv2")
set colSessions = objWMI.ExecQuery _ 
  ("Select * from Win32_LogonSession Where LogonType = 2") 
     
If colSessions.Count = 0 Then     
  Set objStdOut = WScript.StdOut
  objStdOut.Write "UserLoggedIn = False"
   
  
Else
  Set objStdOut = WScript.StdOut
  objStdOut.Write "UserLoggedIn = True"  
End If

Now run that and how ever you start it, if you don’t use Cscript.exe it will resubmit it’s self using Cscript.exe and output the result to stdout.

Now by playing around with the logonType and other bits of this code we could make it quite useful, it will return the logged on status of a remote machine if you replace the “.” with a valid remote computer name.

Well I hope that may be useful to some, and my thanks goes to those who came before me and provided the foundations, Please visit there sites to see many other great scripting examples.

VBScript – How to show logged on users?

Force Cscripe

DevilWAH