Jump to content

Help with FileReadLine


 Share

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)

WinWaitActive("Untitled - Notepad")

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

;WinWaitActive("Untitled - Notepad")

;Send("DEB processing next file " & $file & @LF)

$fileread = FileOpen($file, 0)

; Check if file opened for reading OK

If $fileread <> - 1 Then

WinWaitActive("Untitled - Notepad")

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

$j = 0

While 1

$rdpline = FileReadLine($fileread)

If @error <> 0 Then ExitLoop

WinWaitActive("Untitled - Notepad")

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)

WinWaitActive("Untitled - Notepad")

Send("DEB found good IP address " & $ipads & @LF)

Else

WinWaitActive("Untitled - Notepad")

Send("DEB found strange IP address (trailer) " & $ipads & @LF)

EndIf

Else

WinWaitActive("Untitled - Notepad")

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)

WinWaitActive("Untitled - Notepad")

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)

WinWaitActive("Untitled - Notepad")

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

EndIf

Next

EndIf

WinWaitActive("Untitled - Notepad")

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)

Branch IP Address User Response

NS1 220.220.220.100 RedBlack 1

DEB successfully opened next file angela.rdp

DEB read line ÿþs

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB closing file angela.rdp

DEB successfully opened next file bill.rdp

DEB read line ÿþs

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB closing file bill.rdp

DEB successfully opened next file christine.rdp

DEB read line ÿþs

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB closing file christine.rdp

DEB successfully opened next file greg.rdp

DEB read line ÿþs

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

DEB read line

.

.

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

full address:s:220.220.220.103

compression:i:1

keyboardhook:i:2

audiomode:i:0

redirectdrives:i:0

redirectprinters:i:1

redirectcomports:i:0

redirectsmartcards:i:1

displayconnectionbar:i:1

username:s:angela

domain:s:

alternate shell:s:

shell working directory:s:

password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB010000008BDFC8C85E2AB84C80285CB7B317DB8E000000000800000

0700073007700000003660000A800000010000000082C1AAB52010DAC27915F2DD759211C0000000004800000A0000000100

000001C5B9A01EF730129872A9263F6384A4A08020000FA49ACA8DDF1D0C49649E904E872C9B08C821FD3C4C4784201BFB91

41E8189665128DDF6D50A2587436BE3EAFB232CDEABEE0A046F829A647FAE62B37ED083B9D132C38210A19EA759A2D1E3D75

B69E1477BAA10CFA6A1B0FDFAE6BE018905227269FDBFC8F5BB02ED1B02AAA73665079C88EAD5EE241752118D2D154CEE149

5EBEF66FECB7165D5C0D51A03EDD70E565BDD7D216972281047B4BA51956AFEA5411C67F0CDC2F99D01897F5308903126F83

6C071FEFE99DB54E0DE8FB2E08201626BE0A2B98A9B8EE467743AF4C94304363E9EC4B202561063513DA2A5429A4108CFA73

A30F7B36E0E8D58BC784662F46ABBCAEC15C1A52AFDAB8BCAE7E7F707636A83BB0AAB9F0E1F9C4563857CD050CB3E37D9A97

C073D1C83ACCF4A969609CAF91926FB3875B48655D22B727B85F9DD43E72682C27CDC7CACC6AAD0725E8FC8D35C8C776598F

207CA9D3407D83C329DABA7B4C0EF4420D36F004E825B62FF10890B292751DB13AE67697468CEC6944657EC5C57B8A9CD819

AE5962B1130AC1F658B64C188C897201EABC44F4A308F0DA89E5132A0F4C108E88C277ED3D51DC5432C6DD42C506157A8FFC

10BB5C5ECF203FCA8B079549B679980BC3F3554CE7AC6CA179A10BC5E68D9BF1858093450AC4D29EB77C803706BD33FACD49

08DD79E03AA103FFA9264838A22436F80E10E091FC7173827C4CEC33B5B9CDF4EF598BC96D26179FD948B1400000054E2817

1794755ACEDD69609BC4A54E66CA57573

disable wallpaper:i:1

disable full window drag:i:1

disable menu anims: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.

Link to comment
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.
Link to comment
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. :whistle:

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...