Jump to content

Recommended Posts

Posted

Just a random idea but since you are playing in VBS and WMI anyway you could do it all in VBS if your webserver runs windows by using WinRM and WMI

 

If you install WinRM on the server and client then you can run WMI querys over the net, the code from below allows you to raname files through WMI so you should be able to copy a premade html template with your location to be location.html depending on which school you were in.

 

Rename or copy a file (VBScript) - Windows Server Cookbook

 

A completely random solution but uses some pretty cool toys and allows you to stick to native VBS. The other options listed above are probably easier though.

Posted

Well I've gone for the simple ftp the ipaddress.txt file up to webserver and then check its contents on my main php index page and display cross ref my ip with the school.

 

Current code is in 2 files - one vbs and one bat (you don't seem to be able to jsut ftp in vbs without extra components)

strComputer = "."
Dim result

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
   ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objItem in colItems
'    Wscript.Echo objItem.MACAddress
   For Each strAddress in objItem.IPAddress
if left(strAddress,3) = "10." then
'        	Wscript.Echo strAddress
	result=WriteFileText("c:\ipaddress.txt",strAddress)
end if
   Next
Next

Set WShell = CreateObject("WScript.Shell")
'WShell.Run WShell.ExpandEnvironmentStrings("%COMSPEC%") "/c c:\ftpipaddress.bat" 
WShell.Run("%COMSPEC% /K c:\ftpipaddress.bat"), 1, True 


Function WriteFileText(sFilePath, sText)
   Dim objFSO 'As FileSystemObject
   Dim objTextFile 'As Object
  
   Const ForReading = 1
   Const ForWriting = 2
   Const ForAppending = 8
  
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objTextFile = objFSO.CreateTextFile(sFilePath, True)
  
   ' Write a line.
   objTextFile.Write (sText)

   objTextFile.Close
End Function

 

and

@echo off
echo user *********> ftpcmd.dat
echo ********>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put c:\ipaddress.txt>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat www.*********.sch.uk
del ftpcmd.dat
exit

 

@synack - as usual you suggestion is good but I don't have that sort of access to the server (and no idea if its windows or linux :) )

 

Ta for suggestions and if anyone has a more elegant all-in-one script (maybe a bat file that can parse the output of ipconfig to find a 10.* address - my dos bat coding not good enough for that :) )

 

regards

 

Simon

untitled.PNG

Posted

:)

 

I like your thinking :)

 

Yes - that would find the line with IP

 

in it and I could parse the rest of it in PHP so it is a solution :)

 

But I was looking for the DOS guru who can make tea using the cmd line and just extract the first 10.xxx.yyy.zzz address it finds active :)

 

 

regards

 

Simon

Posted (edited)

If you want a DOS version to pick out the first 10.x.yy.zzz line, how's this?

 

save this as ip.bat then run it. THe commands won't run properly directly from the commandline because of a weirdness around %%s

 

@echo off
ipconfig | findstr "10.[0-9]" | findstr "Address" > ip.txt
for /F "usebackq tokens=15* delims= " %%a in (ip.txt) do echo %%a
del ip.txt

Edited by sahmeepee
it's important to be tidy ;)
  • Thanks 1
Posted
If you want a DOS version to pick out the first 10.x.yy.zzz line, how's this?

 

save this as ip.bat then run it. THe commands won't run properly directly from the commandline because of a weirdness around %%s

 

@echo off
ipconfig | findstr "10.[0-9]" | findstr "Address" > ip.txt
for /F "usebackq tokens=15* delims= " %%a in (ip.txt) do echo %%a
del ip.txt

 

I can't do all the whiz bang stuff in a bat file as per above lol - Im more used to vbs and more modern languages.

Posted (edited)

Not need to resort to DOS :eek:

 

This way gets rid of one textfile all together and does it all (other than the actual FTPing) in VBS

 

WriteLocationFile "c:\ipaddress.txt",GetLocalIP 
SendViaFTP
DeleteLocationFile "c:\ipaddress.txt"

Sub WriteLocationFile(sFilePath, sText)
   Dim objFSO 'As FileSystemObject
   Dim objTextFile 'As Object
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objTextFile = objFSO.CreateTextFile(sFilePath, True)
   objTextFile.Write (sText)	' Write a line.
   objTextFile.Close
End Sub

Sub SendViaFTP()
   Dim WSHShell
   Set WSHShell = WScript.CreateObject("WScript.Shell")
   WSHShell.Run "ftp", 1, false ' open normal window and do not wait
   Wscript.Sleep 200		'sleep for 0.2 of a second
   WSHShell.SendKeys "open www.*********.sch.uk" & VbCrLf
   Wscript.Sleep 1000		'sleep for 0.2 of a second
   WSHShell.SendKeys "User *********" & VbCrLf
   Wscript.Sleep 200		'sleep for 0.2 of a second
   WSHShell.SendKeys "********" & VbCrLf
   Wscript.Sleep 200		'sleep for 0.2 of a second
   WSHShell.SendKeys "bin" & VbCrLf
   Wscript.Sleep 200		'sleep for 0.2 of a second
   WSHShell.SendKeys "put c:\ipaddress.txt" & VbCrLf
   Wscript.Sleep 200		'sleep for 0.2 of a second
   WSHShell.SendKeys "quit" & VbCrLf
   Set WSHShell = Nothing
End Sub

Sub DeleteLocationFile(strFilePath)
   set objFSO = CreateObject("Scripting.FileSystemObject")
   objFSO.DeleteFile(strFilePath)
End Sub

Function GetLocalIP
   strComputer = "."
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
   Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
   For Each objItem in colItems
       For Each strAddress in objItem.IPAddress
    if left(strAddress,3) = "10." then
               GetLocalIP = strAddress
           end if
       Next
   Next
End Function

 

Not sure about the " WSHShell.SendKeys "User *********" & VbCrLf" line as now that it is not connecting inline you may just need to provide the username without the prefix. The sleep intervals could also be cut down lots, i just left them long to see what was going on, I would leave a gap of 1.5 seconds for it to actually connect though depending on how quick your FTP server is.

Edited by SYNACK
  • Thanks 1
Posted (edited)

I can see how that is much more elegant (ahem!) So without resorting to 12 pages of VBscript ;) ...

 

@echo off
REM Take down your particulars:
set IPUpdaterHostname=www.yourschool.sch.uk
set IPUpdaterUsername=yourFTPUsername
set IPUpdaterPassword=yourFTPPassword(Leave blank to be prompted)

REM Extracting the IP address:
ipconfig | findstr "10.[0-9]" | findstr "Address" > ip.txt
for /F "tokens=15 delims= " %%a in (ip.txt) do echo %%a>ip.txt

REM Uploading the IP address:
if not defined IPUpdaterPassword set /p IPUpdaterPassword=Enter your password: 
echo open %IPUpdaterHostname% > IPUpdaterFTP.txt
echo %IPUpdaterUsername% >> IPUpdaterFTP.txt
echo %IPUpdaterPassword% >> IPUpdaterFTP.txt
echo put ip.txt >> IPUpdaterFTP.txt
echo quit >> IPUpdaterFTP.txt
ftp -s:IPUpdaterFTP.txt

REM Tidying up:
del ip.txt && del IPUpdaterFTP.txt
set IPUpdaterHostname=&&set IPUpdaterUsername=&&set IPUpdaterPassword=

 

If you want to hard-code the settings into the script you can trim those 4 lines near the top of course :)

 

It's only a couple more lines to add a text file lookup so it would upload your location instead of your IP address.

Edited by sahmeepee
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...