Archive for the ‘DOS’ Category

h1

Skype and Web Servers

08/08/2009

I briefly had a problem with the web server that’s running my personal wiki yesterday.  Try as I might it just wouldn’t start-up.  I hadn’t had a problem before and after three or four attempts at clicking the start option I took a step back and surveyed my machine – a useful option in many circumstances.

I spotted the culprit straight away as I had come across the issue before – Skype.  When Skype starts up if the default port (51907 on my current instance of Skype) is unavailable (because another application is using it or more likely a firewall is blocking it) it tries to set-up on a port that is more likely to be available – 80 or 443 which are used by web servers for insecure and secure traffic respectively.

Whilst there is an advanced option in Skype to force it away from these two alternative ports often there are no other convenient ones to choose from.  This is because it is good practice security to block unused ports and Skype isn’t always a top runner so to speak on ports to have open.

The moral of the story is simple – start your web server before you start Skype.

However this got me to wondering what else might pick a port that could conflict and how would you know what it was without just systematically working through all the applications and services you have running … which for me could be a large number.  My solution was to use some DOS commands.  NETSTAT can be used to examine what ports are in use by what processes.  TASKLIST can be used then to identify the executable.

To smooth things over a little I’ve concocted a little script to automate this process.  It’s really just a bit of a nicer front end to the whole thing and whilst several command prompt windows may briefly flash up as it runs, the script will take in the information from the commands and process them to filter out the pertinent information.

Option Explicit

Dim objCommandPort
Dim astrResults
Dim astrInfoLine
Dim strOutput
Dim strPortCommand
Dim strPID
Dim strApps

strPortCommand = "netstat -ano" 

set objCommandPort = New clsDOSCommandExecutor

objCommandPort.ExecuteCommand(strPortCommand)
strOutput = objCommandPort.GetOutput
astrResults = split(strOutput, vbCrLf)

strApps = "PORT: APPLICATION" & vbCrLf
AssessPort "80"
strApps = strApps & vbCrLf
AssessPort "443"

MsgBox strApps, vbInformation + vbOKOnly, "Applications using web server ports"

Sub AssessPort(p_strPort)
 Dim intCounter

 For intCounter = 1 to Ubound(astrResults)
 If inStr(astrResults(intCounter), ":" & p_strPort) > 0 Then
 astrInfoLine = split(astrResults(intCounter))
 strPID = astrInfoLine(ubound(astrInfoLine))
 strApps = strApps & vbCrLf & p_strPort & ": " & AppOnPort(strPID)
 End If
 Next
End Sub

Function AppOnPort(p_strPID)
 Dim objCommandPID
 Dim strPIDCommand
 Dim strPIDOut
 Dim astrPIDOut

 set objCommandPID = New clsDOSCommandExecutor
 strPIDCommand = "tasklist /fi ""PID eq " & p_strPID & """"

 objCommandPID.ExecuteCommand(strPIDCommand)

 strPIDOut = Right(objCommandPID.GetOutput, len(objCommandPID.GetOutput) - inStrRev(objCommandPID.GetOutput, "=" & vbcrlf))
 astrPIDOut = split(strPIDOut)

 AppOnPort = Replace(astrPIDout(0), vbCrLf, "")
End Function

Class clsDOSCommandExecutor
 Dim objShell, objExec
 Dim strCommand
 Dim strError
 Dim objError
 Dim objOutput
 Dim strOutput

 Sub ExecuteCommand(p_strCommand)
 strCommand = "cmd /c " & p_strCommand
 Set objShell = CreateObject("Wscript.Shell" )

 objShell.Exec(strCommand)

 Set objExec = objShell.Exec(strCommand)

 Do Until objExec.Status
 Wscript.Sleep 200
 Loop

 Set objError = objExec.StdErr
 strError = objError.ReadAll

 Set objOutput = objExec.stdOut
 strOutput = objOutput.ReadAll
 End Sub

 Function GetOutput()
 GetOutput = strOutput
 End Function

 Function GetError()
 GetError = strError
 End Function

 Function Failed()
 If strError = "" Then
 Failed = false
 Else
 Failed = true
 End If
 End Function
End Class
Advertisements
h1

DOS Command Class for VBScript

08/08/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

Command Line Utility to Create Test Files

31/07/2009
Using the utility on the command line

Using the utility on the command line

In the past I’ve used FSUTIL to create large test files of data to process.  Unfortunately you need administrator permissions to use this utility.  I decided to write a command line utility (called “Make Test File” but abbreviated to MTF.EXE) to allow non-administrators to do the same.  It’s written in VB.NET so it isn’t as quick as FSUTIL, but as they say “it does the job”, and it fulfilled my immediate needs.

It takes two parameters – the size of the file (in bytes) and the file path to write the file to.  To see an example of how to use it and the output click on the thumbnail image on the right.  The resulting file just contains zeros.

If there’s any demand I may look into additional features such as being able to specify what the file is filled with or creation of multiple files using some sort of incremental naming convention.

h1

Don’t log on as admin just yet

23/07/2009

On Windows XP you may find that you need to carry out some tasks as an administrator for which you would logon as  an administrator (local or network/domain).  If the user is already logged on then you can choose to run as administrator for many activities by selecting run as from a file’s context menu.  However not every type of file has this option.

My preferred method to get around this is to run a file management application as an administrator and then any file action from within that is run as an administrator.  The RUNAS executable is accessible from the command line and along with the right selection of parameters should make this quite easy to do.  Unfortunately a restriction around instances (and the fact that the Windows Desktop is an instance of EXPLORER.EXE) means that EXPLORER.EXE can’t be run as – at least not without changing some settings in Windows.

The alternative is to use the fact that Internet Explorer is a standard component and has a synonymity with explorer.  So to quickly access the C drive in explorer (via Internet Explorer) I suggest popping the following line into a shortcut and deploying it to every machine or at least a quickly accessible folder on your network (with a short name you can then quickly type into a Run dialog).

runas /user:administrator "C:\Program Files\Internet Explorer\iexplore.exe c:"

This assumes your admin account is called “administrator” (add an ‘@yourdomain’ after this for domain admin access on the appropriate domain) and Internet Explorer is installed under program files in a folder called Internet Explorer.  You could similarly make this a .BAT file rather than a shortcut file (.LNK),  but I think the shortcut is a slightly neater looking option.

When run a command prompt window will be opened and ask for the password.  Type this in and press enter/return to have Windows open your nice admin file management session.  Just remember to close the window when you’ve finished … you don’t want to leave end users with admin access.

h1

Continuous Ping

31/01/2009

Sometimes when testing network connectivity to a server on the local LAN or a web site I use the ubiquitous PING to do the job.  If I need to do it repeatedly I tend to write a quick DOS batch file (a text based script with a file extension of BAT or CMD).

For example this script would repeatedly ping localhost – the name used for the machine the script is being run on.

@echo off
:START
Ping -n 1 localhost
Goto START

A few years ago I even extended this to include a ping count but it didn’t really add much. I decided it would be more useful to have something that at least gave an indication of how many “good and bad” pings had occurred. At the same time I also found that I wasn’t always necessarily at my computer as I might be tinkering with cables and other networking gear so having some sort of audible cue as to how the pings were going.

I put together the following VB Script to do just this. It hasn’t been tested extensively and I’m sure that there could be a bit more validation. When the script is run it asks for the host to ping and a time at which to stop pinging. It then opens up an Internet Explorer window that displays the information about the pinging.

Option Explicit

'Settings:
'- Number of PC speaker beeps on ping successes/failures
Const BEEPS_ON_PING = 1
Const BEEPS_ON_NO_PING = 3
'- Milliseconds of pause between pings
Const PAUSE_MS = 200
'- Milliseconds pause before automatically closing the progress window
Const AUTO_CLOSE = True
Const FINAL_PAUSE = 5000

'Variable definitions
Dim strHost
Dim dtEnd
Dim objShell, objExplorer
Dim intSuccessfulPingCount, intUnsuccessfulPingCount

'Initialise
Set objExplorer = CreateObject("InternetExplorer.Application")

objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Width = 400
objExplorer.Height = 400
objExplorer.Document.Body.Style.Cursor = "default"
intSuccessfulPingCount = 0
intUnsuccessfulPingCount = 0


'If we don't have a host then ask for one
If Wscript.Arguments.Count < 1 Then
	strHost = InputBox("Please enter the host to ping", "Enter Host", "localhost")
Else
	strHost = Wscript.Arguments(0)
End If

'Double check what we have
If Len(strHost)  0
	'Carry out the ping
	If Ping(strHost, False) = True Then
		'Ping successful
		intSuccessfulPingCount = intSuccessfulPingCount + 1
		Beep(BEEPS_ON_PING)
		WScript.Sleep(PAUSE_MS)
	Else
		'Ping unsuccessful
		intUnsuccessfulPingCount = intUnsuccessfulPingCount + 1
		Beep(BEEPS_ON_NO_PING)
		WScript.Sleep(PAUSE_MS)
	End If
	UpdatePings "<p><b>RUNNING...</b></p>"
Wend

'Finalise
objExplorer.Document.Body.Style.Cursor = "default"
If AUTO_CLOSE Then
	UpdatePings("<p><b>AUTO CLOSING...</b></p>")
	Wscript.Sleep FINAL_PAUSE
	objExplorer.Quit
Else
	UpdatePings("<p><b>PING CYCLE COMPLETED</b></p>")
End If


'Update the display of pings
Sub UpdatePings(p_strSuffix)
	Dim strDisplay

	strDisplay = ""

	strDisplay = strDisplay & "<p>"
	strDisplay = strDisplay & "Finish pinging @ "
	strDisplay = strDisplay & dtEnd
	strDisplay = strDisplay & "<br />"
	strDisplay = strDisplay & "Currently ... "
	strDisplay = strDisplay & Now()
	strDisplay = strDisplay & "</p>"

	strDisplay = strDisplay & "<hr>"

	strDisplay = strDisplay & "<p>"
	strDisplay = strDisplay & "<table border='0'>"
	strDisplay = strDisplay & "<tr>"
	strDisplay = strDisplay & "<td><font color='green'>Successful pings</font></td>"
	strDisplay = strDisplay & "<td><font color='green'>"
	strDisplay = strDisplay & intSuccessfulPingCount
	strDisplay = strDisplay & "</font></td>"
	strDisplay = strDisplay & "</tr>"
	strDisplay = strDisplay & "<tr>"
	strDisplay = strDisplay & "<td><font color='red'>Unsuccessful pings</font></td>"
	strDisplay = strDisplay & "<td><font color='red'>"
	strDisplay = strDisplay & intUnsuccessfulPingCount
	strDisplay = strDisplay & "</font></td>"
	strDisplay = strDisplay & "</tr>"
	strDisplay = strDisplay & "</table>"
	strDisplay = strDisplay & "</p>"

	strDisplay = strDisplay & "<hr>"

	strDisplay = strDisplay & "<p>"
	strDisplay = strDisplay & "Auto Close Enabled: "
	strDisplay = strDisplay & AUTO_CLOSE
	strDisplay = strDisplay & "</p>"

	strDisplay = strDisplay & "<hr>"

	strDisplay = strDisplay & p_strSuffix

	objExplorer.Document.Body.InnerHTML = strDisplay
End Sub


'This function pings the specified host
Function Ping(p_strHost, p_boolDisplay)
	Dim objPing, objStatus

	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery ("select * from Win32_PingStatus where address = '" & p_strHost & "'")

	For Each objStatus in objPing
		If IsNull(objStatus.StatusCode) or objStatus.StatusCode0 then
			Ping = False
			If p_boolDisplay Then
				WScript.Echo "Status code is " & objStatus.StatusCode
			End If
		Else
			Ping = True
			If p_boolDisplay Then
				Wscript.Echo "Bytes = " & vbTab & objStatus.BufferSize
				Wscript.Echo "Time (ms) = " & vbTab & objStatus.ResponseTime
				Wscript.Echo "TTL (s) = " & vbTab & objStatus.ResponseTimeToLive
			End If
		End if
	Next
End Function 


'Beep the PC speaker a number of times equal to the p_intBeeps parameter
Function Beep(p_intBeeps)

	Dim objShell, intCount, strCommand

	'Check we have a valid number of beeps not fool proof but it will do for now
	Beep = False
	If IsNumeric(p_intBeeps) Then
		If p_intBeeps > 0 Then
			Beep = True
		End If
	End If

	If Beep Then
		'Build the command string to run
		strCommand = "cmd /c echo"
		For intCount = 1 to p_intBeeps
			strCommand = strCommand & " " & chr(007)
		Next

		'Run the command in the background
		Set objShell = Wscript.CreateObject("wscript.Shell")
		objShell.Run strCommand, 0

		Beep = True
	End If
End Function

There are also a few options in the script to specify numbers of beeps for successful and unsuccessful pings as well as auto closing of the Internet Explorer window and lengths of pauses between pings and before auto closing the window.