Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pull output from CMD window?

Status
Not open for further replies.

Lasersec1

Programmer
Dec 14, 2019
2
0
0
GB
I am trying to write a script that will detect if an iPhone is connected to the routers WiFi. Initially I worked on the premise that all I needed to do was Ping the iPhone's fixed IP address of 192.168.1.12. Unfortunately after spending a few hours building and testing a script I discovered that the iPhone goes into sleep mode timeout if not used thus switching WiFi and Bluetooth to standby to conserve battery power.. so pinging the iPhone's IP address fails.

After that I discovered that the Vigor2860Ln router does in fact maintain the IP address of the iPhone even when sleeping so I came up with the following VBScript which works fine - I can login to the router and produce the required output in a CMD window but I cannot find how to pull the returned output to see if the IP address 192.168.1.12 exists

Any help would be appreciated.


'(Start of Script)--------------------------

Set MyObject = CreateObject("WScript.Shell")

MyObject.run "cmd"
WScript.Sleep 500

MyObject.SendKeys"telnet 192.168.1.1"
MyObject.SendKeys("{Enter}")
WScript.Sleep 500

MyObject.SendKeys"admin" '(UserName)
MyObject.SendKeys("{Enter}")
WScript.Sleep 500

MyObject.SendKeys"******" '(User Password)
MyObject.SendKeys("{Enter}")
WScript.Sleep 500

MyObject.SendKeys "wl stalist show " '(Query Router Data)
MyObject.SendKeys "quit{ENTER}"

'(End of Script)--------------------------


(The above script Produces a CMD window containing the following.)

Index Status IP Address MAC Address Associated with
1 A 192.168.1.16 MAC Address 1 SSID #1
2 A 192.168.1.15 MAC Address 2 SSID #1
3 A 192.168.1.13 MAC Address 3 SSID #1
4 A 192.168.1.12 MAC Address 4 SSID #1 (Does this IP or MAC address exist? - If not then iPhone is NOT connected)
 
If you look at my example vbscript in thread329-1798189 you will see one method of getting the output from a CMD window.

 
Many thanks for your suggestion strongm. However looking at your thread it seems to deal a lot more with RTF which I know nothing about and cannot see how I would implement that in my script. However since my original post I have managed to research further information and have now answered my own question as can be seen from my finished code below, and works as I need it to. The only drawback I have found is that if the focus moves away from the CMD window when the script is running it causes the script to crash and burn. This could happen if the user is working on the PC, or even if another program takes the focus. So now that I have proved the functionality of the script I am now looking to re-write what I have in VB6 or VB.NET or maybe PowerShell using sockets which should do away with a CMD window and which should also negate focus loss :)

I hope my code helps others who are trying to get their iPhones to be seen on their networks (When phone is in range) and which could then be used to control other devices such as opening/closing ports for security or even activate/de-activate an alarm system. Android phones apparantly have an option to keep alive their WiFi/Bluetooth wheras IOS does not.



'(Start of Script)--------------------------

Const ForReading = 1

Do

WScript.Sleep 15*60*1000

set WshShell = WScript.CreateObject("WScript.Shell")
'WshShell.run("telnet.exe 192.168.1.1")
WshShell.run"telnet.exe -f telnet.log 192.168.1.1"
WScript.Sleep 100

WshShell.SendKeys"UserName"
WshShell.SendKeys("{Enter}")
WScript.Sleep 100

WshShell.SendKeys"PassWord"
WshShell.SendKeys("{Enter}")
WScript.Sleep 100

WshShell.SendKeys "wl stalist show "
WshShell.SendKeys("{Enter}")
WScript.Sleep 100

WshShell.SendKeys "quit{ENTER}"

WScript.Sleep 1000
WshShell.SendKeys " exit"
WshShell.SendKeys("{Enter}")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("telnet.log", ForReading)

strText = objFile.ReadAll

if InStr(strText, "192.168.1.12") >0 then
WshShell.Run "cscript ""D:\0 - My Documents\Backup\Router Draytek\Port Control\VOFF.vbs""" '(Close Ports)
Else
WshShell.Run "cscript ""D:\0 - My Documents\Backup\Router Draytek\Port Control\VON.vbs""" '(Open Ports)
End If

objFile.Close

Loop

'(End of Script)--------------------------
 
>seems to deal a lot more with RTF

Er … the specific problem being dealt with there was RTF, yes - but the solution involved grabbing the output (which happens to be the clean RTF in that example, but could be anything) from a command window (which is what you want to do). That being said, you'd have needed to do a significant amount of rewriting of your script for that particular method to work - which you can now safely avoid since you seem to have a working solution


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top