## Recommended Posts

As a newbie I need some help with a script which attempts to ping all the PCs on a WAN, by first pinging each of the branch firewall machines, and then pinging each of the connected PCs. It first tries to load an array with login names from Windows Remote Desktop Connection .rdp files, which are text files. Here is the script, with real IP addresses changed to protect the innocent:

CODE

; 'pingpcs' pings all IP addresses (ending from 101 to 200) with the same root

; as the branch RedBlack IP address, after first establishing that the RedBlack

; is pingable.

;

; If known, the associated login name is displayed in a NotePad file with its

; IP address if pingable. Login names are extracted from the Remote Desktop

; Connection .rdp files associated with each branch.

;

; When Who What

; 10/8/06 jim First written

;

$g_szVersion = "pingpcs 1.1" If WinExists($g_szVersion) Then Exit ; It's already running

AutoItWinSetTitle($g_szVersion) Run("notepad.exe") WinWaitActive("Untitled - Notepad") Send(" Hobson Branch Ping PC Statistics (in ms)" & @LF & @LF) Send("Branch IP Address User Response" & @LF) Ping_Branch("222.222.222.100", "NS1") Ping_Branch("333.333.333.100", "QB1") Ping_Branch("444.444.444.100", "VM1") Ping_Branch("555.555.555.100", "WP1") Ping_Branch("666.666.666.100", "WP2") Exit Func Ping_Branch($RedBlackIP, $Branch) ; First, ping and report branch RedBlack ping stat Dim$login[101]

$rb = StringFormat("%-15s",$RedBlackIP)

$rbping = Ping($RedBlackIP, 1000)

If $rbping = 0 Then Select Case @error = 1$rbmess = "RedBlack is offline"

Case @error = 2

$rbmess = "RedBlack is unreachable" Case @error = 3$rbmess = "RedBlack has bad destination"

Case Else

$rbmess = "RedBlack has 'other errors'" EndSelect WinWaitActive("Untitled - Notepad") Send($Branch & " " & $rb & " " &$rbmess & @LF & @LF)

Exit

Else

$rbp = StringFormat("%3d",$rbping)

Send($Branch & " " &$rb & " RedBlack " & $rbp & @LF)$Branch = " "

EndIf

; Find the root IP address of the RedBlack

$iprootlastdot = StringInStr($RedBlackIP, ".", 0, -1)

$iproot = StringLeft($RedBlackIP, $iprootlastdot) ; Get the filenames of all .rdp files in the current directory ; and read the names into an array indexed by the last part of the IP address For$j = 1 To 100

$login[$j] = "";

Next

$Dir = "P:\it\remote\" &$Branch

FileChangeDir($Dir)$search = FileFindFirstFile("*.rdp")

If $search = -1 Then WinWaitActive("Untitled - Notepad") Send("No *.rdp files found in directory " &$Dir & @LF)

Else

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

;Send("DEB processing next file " & $file & @LF)$fileread = FileOpen($file, 0) ; Check if file opened for reading OK If$fileread <> - 1 Then

Send("DEB successfully opened next file " & $file & @LF)$j = 0

While 1

$rdpline = FileReadLine($fileread)

If @error <> 0 Then ExitLoop

Send("DEB read line " & $rdpline & @LF) If StringInStr($rdpline, "^full address:s:") Then

$ipad = StringTrimLeft($rdpline, 15)

$ipads = StringStripWS($ipad, 2)

If StringInStr($ipads,$iproot) Then

$ipno = StringRight($ipad, 3)

If $ipno > 100 And$ipno < 201 Then

$j = ($ipno - 100)

Send("DEB found good IP address " & $ipads & @LF) Else WinWaitActive("Untitled - Notepad") Send("DEB found strange IP address (trailer) " &$ipads & @LF)

EndIf

Else

Send("DEB found strange IP address (no root) " & $ipads & @LF) EndIf EndIf If StringInStr($rdpline, "^username:s:") And $j > 0 Then$log = StringTrimLeft($rdpline, 11)$login[$j] = StringStripWS($log, 2)

Send("DEB loaded login name " & $login[$j] & " to table " & $j & @LF) ExitLoop EndIf WEnd WinWaitActive("Untitled - Notepad") Send("DEB closing file " &$file & @LF)

FileClose($fileread) EndIf WEnd ; Ping each of the IP addresses XXX.XXX.XXX.101 to XXX.XXX.XXX.200 where ; XXX.XXX.XXX. is the root IP address of the RedBlack. For each successful ; ping, look up the array to find the user name. For$i = 101 To 200

$ipaddress =$iproot & StringFormat("%3d", $i)$response = Ping($ipaddress, 500) If$response > 0 Then

$k =$i - 100

If $login[$k] <> "" Then

$nm = StringFormat("%-10s",$login[$k]) Else$nm = "Unknown "

EndIf

$ipa = StringFormat("%-15s",$ipaddress)

$res = StringFormat("%3d",$response)

Send($Branch & " " &$ipa & " " & $nm & " " &$res & @LF)

EndIf

Next

EndIf

Send(@LF)

; Close the search handle

FileClose($search) EndFunc ;==>Ping_Branch Problem is that$rpbline is not being populated with the contents of the lines in the open .rpb file, as evidenced by the following output notepad file, which shows all the debug (DEB) lines:

Hobson Branch Ping PC Statistics (in ms)

NS1 220.220.220.100 RedBlack 1

DEB successfully opened next file angela.rdp

DEB closing file angela.rdp

DEB successfully opened next file bill.rdp

DEB closing file bill.rdp

DEB successfully opened next file christine.rdp

DEB closing file christine.rdp

DEB successfully opened next file greg.rdp

.

.

Not only that, the script appears to read approximately double the number of lines that exist in the file. Here is the contents of the angela.rpb file:

screen mode id:i:2

desktopwidth:i:1280

desktopheight:i:1024

session bpp:i:24

winposstr:s:0,3,0,0,800,600

auto connect:i:0

compression:i:1

keyboardhook:i:2

audiomode:i:0

redirectdrives:i:0

redirectprinters:i:1

redirectcomports:i:0

redirectsmartcards:i:1

displayconnectionbar:i:1

domain:s:

alternate shell:s:

shell working directory:s:

0700073007700000003660000A800000010000000082C1AAB52010DAC27915F2DD759211C0000000004800000A0000000100

000001C5B9A01EF730129872A9263F6384A4A08020000FA49ACA8DDF1D0C49649E904E872C9B08C821FD3C4C4784201BFB91

41E8189665128DDF6D50A2587436BE3EAFB232CDEABEE0A046F829A647FAE62B37ED083B9D132C38210A19EA759A2D1E3D75

5EBEF66FECB7165D5C0D51A03EDD70E565BDD7D216972281047B4BA51956AFEA5411C67F0CDC2F99D01897F5308903126F83

6C071FEFE99DB54E0DE8FB2E08201626BE0A2B98A9B8EE467743AF4C94304363E9EC4B202561063513DA2A5429A4108CFA73

A30F7B36E0E8D58BC784662F46ABBCAEC15C1A52AFDAB8BCAE7E7F707636A83BB0AAB9F0E1F9C4563857CD050CB3E37D9A97

207CA9D3407D83C329DABA7B4C0EF4420D36F004E825B62FF10890B292751DB13AE67697468CEC6944657EC5C57B8A9CD819

AE5962B1130AC1F658B64C188C897201EABC44F4A308F0DA89E5132A0F4C108E88C277ED3D51DC5432C6DD42C506157A8FFC

10BB5C5ECF203FCA8B079549B679980BC3F3554CE7AC6CA179A10BC5E68D9BF1858093450AC4D29EB77C803706BD33FACD49

08DD79E03AA103FFA9264838A22436F80E10E091FC7173827C4CEC33B5B9CDF4EF598BC96D26179FD948B1400000054E2817

1794755ACEDD69609BC4A54E66CA57573

disable wallpaper:i:1

disable full window drag:i:1

disable themes:i:0

bitmapcachepersistenable:i:1 Note that the file is only read up to when the script finds the 'username:s:' line.

Can anybody tell me what I am doing wrong? I have tried many variations but all produce the same rubbish output. I have searched the forums but haven't found a similar problem. I have also tested on a different PC with same results. Comments gratefully received.

##### Share on other sites

The problem is that the RDP file is in Unicode. Autoit doesn't handle Unicode so fine.

You can convert the RDP file to ANSI by using the DOS type command to dump it to a temporary file. This is a code fragment from your code; you should be able to pick up where it is.

;[...]
Send("No *.rdp files found in directory " & $Dir & @LF) Else While 1$file = FileFindNextFile($search) If @error Then ExitLoop$tmpfile = _TempFile()    ; make a temp file
_RunDOS('type "' & $file & '" > ' &$tmpfile)   ; $tmpfile holds ANSI version of RDP file ;WinWaitActive("Untitled - Notepad") ;Send("DEB processing next file " &$file & @LF)

$fileread = FileOpen($tmpfile, 0)  ; should be able to continue the same from this point.
;[...]

Hope this helps!

BlueBearrOddly enough, this is what I do for fun.

##### Share on other sites

The problem is that the RDP file is in Unicode. Autoit doesn't handle Unicode so fine.

You can convert the RDP file to ANSI by using the DOS type command to dump it to a temporary file. This is a code fragment from your code; you should be able to pick up where it is.

;[...]
Send("No *.rdp files found in directory " & $Dir & @LF) Else While 1$file = FileFindNextFile($search) If @error Then ExitLoop$tmpfile = _TempFile()    ; make a temp file
_RunDOS('type "' & $file & '" > ' &$tmpfile)   ; $tmpfile holds ANSI version of RDP file ;WinWaitActive("Untitled - Notepad") ;Send("DEB processing next file " &$file & @LF)

$fileread = FileOpen($tmpfile, 0)  ; should be able to continue the same from this point.
;[...]

Hope this helps!

Thanks, BlueBearr. That worked like a treat (after I fixed all the other bugs). I am very grateful.

## Create an account

Register a new account

×

• Wiki

• Back

• Git