Jump to content

TCPnametoIP not working in array?


 Share

Recommended Posts

im trying to write a program to check if other programs have succeeded on workstations before running another. for security reasons i have edited out some parts, but i cant get the program to ping parts of an array created from a .inf file string split. this code pulls a message box with no IP addresses in it at all. we need the ip addresses to be pulled so we can ping the workstations from the server

#include <array.au3>
#include <date.au3>
#include <file.au3>
$InfoFile = @WorkingDir & "\CloseProgramServer.inf"
$Infohandle = FileOpen($Infofile, 0)   ; 0 opens the file for reading only
If $Infohandle = -1 Then Exit
$n = 1
$bSuccess = True
While 1
    $line = FileReadLine($Infohandle)
    If @error = -1 Then ExitLoop
    $line = StringStripWS($line, 3)    ; 3 strips the whitespace from both ends
    $strlocation = StringinStr($line, ";", 0, 1, 1, 1)
    If $line <> Chr(0) And $strlocation <> 1 Then
  If $n = 1 Then
   $n = 2
   $WriteThisFile = $line
   $ctrlarray = StringSplit($line, Chr(44), 1) ; Chr(44) is the comma
   FileDelete($WriteThisFile)  ; Need to test this on a network using \\ServerName\FileName
   MsgBox (0, "", "File Deleted : " & $ctrlarray [1])
  Else
   $ctrlarray = StringSplit($line, Chr(44), 1) ; Chr(44) is the comma
     TCPStartup()
     MsgBox ( 0, "IP Address", "IP Address is : " & TCPNameToIP ($ctrlarray[2]))
     TCPShutdown()
   MsgBox (0, "", "The name of the file is : " & $ctrlarray [1] & @CRLF & "The name of the Computer is : " & $ctrlarray [2])
  EndIf
    EndIf
    If $bSuccess = False Then
  FileClose($Infohandle)
  Exit
    EndIf
Wend
FileClose ($infohandle)
$WriteThisFileHandle = FileOpen($WriteThisFile, 1)
If $WriteThisFileHandle <> -1 Then
   FileWriteLine($WriteThisFileHandle, "All files have been successfully closed")
   FileClose($WriteThisFileHandle)
EndIf
Exit
Link to comment
Share on other sites

it appears that this is something the script language cannot handle. im now looking at writing it to open command prompt and send the keystrokes to cmd from autoit. this is starting to get reallly long as i now need to find a way to get the program to reread the .inf file and put the PC names in. any suggestions?

Link to comment
Share on other sites

so it turns out that yes, you do have to use command prompt and send the keys to get around this issue. do not try to ping a hostname from an array. rather open command prompt and send the keystrokes. yes you can send the keystrokes from a name in an array

sorry for triple post, but i felt this was worth doing as someone else may have the same issue and this would at least show them the workaround rather than them spending 2 and a half days wracking their brain trying to figure it out

Link to comment
Share on other sites

What does your inf file contain? It seems as if this should be fairly easy and shouldn't require you to send keystrokes to a command prompt. If you could show how your .inf file is laid out, it would help to figure out what you're doing wrong.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

.inf file is as such

file-name-for-purpose.txt

filename.txt ,hostname

filename.txt ,hostname

filename.txt ,hostname

go as far down as you want with the filename.txt ,hostname. ive got a loop in there that causes the program to read the first line. then change line number to 2 and loop with a new line till the end of the .inf file

Link to comment
Share on other sites

One thing I have found out when working with TCPNameToIP in a loop, do not to include TCPStartUp and TCPShutdown in the loop with it, even when they are called from within a fuction. You get very inconsistent results.

Put them out side the your main loop where TCPNameToIP is used, your While loop, or at beginning and ending of the main script.

TCPStartup()
While 1
  TCPNameToIP($sName)
WEnd
TCPShutdown()

Adam

Link to comment
Share on other sites

Blockinput(1) ; Disables mouse and keyboard (mostly)

BlockInput(0) ; Reenables mouse and keyboard

Vista/Win7 only works if the script is run with elevated privileges.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

and im actually running into a bigger problem. again i cant show the whole script because of security reasons but im stuck at this part. basically checking to see if the pc's are there before the database shuts down. doesnt read to end of file. in fact doesnt read the file at all!

Func _PingTestCheck ( $bcomputer )
Run ("cmd.exe")
Sleep (4000)
Send ( $cmdsnd & "{ENTER}" )
Sleep (10000)
Send ("exit{ENTER}")
Sleep (1000)
$frmcmdline = FileReadLine(@WorkingDir & "\work\" & $ctrlarray [2] & ".txt")
If @error = 1 Then
  $errorline = "Cannot open the Ping test file."
  _WriteErrorMsg( $errorline )
  FileDelete (@workingDir & "\work\" & $ctrlarray [2] & ".txt")
  Exit
EndIf
$frmcmd = @WorkingDir & "\work\" & $ctrlarray[2] & ".txt"
$frmcmdline = StringStripWS($frmcmdline, 3)
$cmdsubstr = StringInStr($frmcmdline, "Ping request could not find host")
If $cmdsubstr = 0 Then
  FileClose ($frmcmd)
  While 2
   FileReadLine($frmcmd)
   If @error = -1 Then ExitLoop
   $cmdsubstr = StringInStr($frmcmdline, "Lost = 4")
   If $cmdsubstr <> 0 Then
    $bcomputer = False
    ExitLoop
   EndIf
     WEnd
Else
  $bcomputer = False
EndIf
EndFunc
Link to comment
Share on other sites

AutoIt has a built in Ping function, but if you want to use the ping command, have a look at the following function. It uses $STDERR_MERGED for the option flag for Run and StdoutRead to read the output stream, so you do not need to use a file.

#include <Constants.au3>
; #FUNCTION# ====================================================================================================================
; Name ..........: _PingComputer
; Description ...: Ping a computer using the Ping command.
; Syntax ........: _PingComputer($sComputer)
; Parameters ....: $sComputer - A string of the computer name.
; Return values .: Success - 1
;                 Failure - 0, sets @error to:
;                 |1 - Unable to ping computer.
; Author ........: AdamUL
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _PingComputer($sComputer)
Local $iPIDPing = Run("Ping " & $sComputer, "", @SW_HIDE, $STDERR_MERGED)
ProcessWaitClose($iPIDPing)
Local $sPingOutput = StringStripWS(StdoutRead($iPIDPing), 3)
If StringInStr($sPingOutput, "Ping request could not find host") Or StringInStr($sPingOutput, "(100% loss)") Then Return SetError(1, 0, 0)
Return 1
EndFunc

Edit: Forgot "#include <Constants.au3>" in code block.

Adam

Edited by AdamUL
Link to comment
Share on other sites

I don't get why TCPnametoIP didn't work for you, i tried it using an array and it worked just fine.

If you're only lookinforward on finding out the ipaddress seens to me you should use StdoutRead instead of running cmd prompt.

There must be easier ways, but heres my way out

#include <Constants.au3>
#include <String.au3>
Global $ctrlarray[3]
$ctrlarray[2] = "sed063" ; Network Computer Name
 
Func _FindIP ($hostname)
 
$ping = Run ("Ping " & $hostname & ' -n 1', @SystemDir, @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose ($ping)
$pingresult = StdoutRead($ping)
$ip = _StringBetween ($pingresult, '[', ']')
 
Return $ip[0]
 
EndFunc
 
$ip = _FindIP ($ctrlarray[2])
MsgBox (0, "IP number", $ip)
Edited by Zio
Link to comment
Share on other sites

idk. ive got a 99% functional program at the moment. figured out how to pull the program into a way you guys can actually help me out. and made the .inf file 100 times easier to read and configure. broke it into 2 programs. one that calls the special one. heres what im running into

#include <array.au3>
#include <date.au3>
#include <file.au3>
$InfoFile = @WorkingDir & "\CloseProgramServer.inf"
$Infohandle = FileOpen($Infofile, 0)
If $infohandle = -1 Then
$errorline = "There is no .inf file found. The program cannot run properly"
_WriteErrorMsg( $errorline )
Exit
EndIf
DirCreate (@WorkingDir & "\work")
DirCreate (@WorkingDir & "\Error Logs")
$n = 1
$bSuccess = True
$errdate =  @WorkingDir & "\Error Logs\" & _NowCalc() & ".txt"
If $infohandle = -1 Then Fileopen ($errdate, 1)
If $infohandle = -1 Then FileWriteLine ($errdate, "There is no .inf file found. The program cannot run properly"
If $Infohandle = -1 Then Exit
While 1
    $line = FileReadLine($Infohandle)
    If @error = -1 Then ExitLoop
    $line = StringStripWS($line, 3)
    $strlocation = StringinStr($line, ";", 0, 1, 1, 1)
    If $line <> Chr(0) And $strlocation <> 1 Then
  If $n = 1 Then
   $n = 2
   $WriteThisFile = $line
   $ctrlarray = StringSplit($line, Chr(44), 1)
   FileDelete($WriteThisFile)
  Else
   $ctrlarray = StringSplit($line, Chr(44), 1)
   $cmdsnd = ("ping -n 5 " & $ctrlarray [2] & " >>" &Chr(34) & @WorkingDir & "\work\" & $ctrlarray [2] & ".txt" & Chr(34))
   If FileExists ( $ctrlarray [1]) Then
    $fileage = _DateDiff ( 'h', _ConvertTimeFormat ( FileGetTime ( $ctrlarray [1], 0, 1 ) ), _NowCalc ( ) )
    If $fileage > 12 Then
     $bcptrexists = True
     _PingTestCheck ($bcptrexists)
     MsgBox(0,"",$bcptrexists)
     If $bcptrexists = True Then
              $errorline = "CloseProgram.exe not recent on " & $ctrlarray[2]
                    _WriteErrorMsg( $errorline )
        FileDelete (@WorkingDir & "\work\" & $ctrlarray [2] & ".txt")
                    Exit
        EndIf
    EndIf
   Else
    $bcptrexists = True
    _PingTestCheck ($bcptrexists)
    If $bcptrexists = True Then
     $errorline = "CloseProgram.exe results not existant from " & $ctrlarray[2]
     _WriteErrorMsg( $errorline )
     FileDelete (@WorkingDir & "\work\" & $ctrlarray [2] & ".txt")
     Exit
    EndIf
   EndIf
  EndIf
    EndIf
    If $bSuccess = False Then
  FileClose($Infohandle)
  Exit
    EndIf
Wend
FileClose ($infohandle)
; RunWait (@WorkingDir & "\Closeprogram.exe")
; Sleep (60000)
MsgBox (0, "Wewt!!", "This is where CloseProgram.exe would now run on the server" )
$WriteThisFileHandle = FileOpen($WriteThisFile, 1)
If $WriteThisFileHandle <> -1 Then
   FileWriteLine($WriteThisFileHandle, "All files have been successfully closed")
   FileClose($WriteThisFileHandle)
EndIf
Exit

Func _ConvertTimeFormat ( $_FileTime )
    Return StringMid ( $_FileTime, 1 , 4 ) & '/' & StringMid ( $_FileTime, 5 , 2 ) & '/' & StringMid ( $_FileTime, 7 , 2 ) & _
    ' ' & StringMid ( $_FileTime, 9 , 2 ) & ':' & StringMid ( $_FileTime, 11 , 2 ) & ':' & StringMid ( $_FileTime, 13 , 2 )
EndFunc
Func _WriteErrorMsg( $errormsg )
   $NowDate = _NowDate()
   $DateArray = StringSplit ( $NowDate, "/")
   $Now = $DateArray[1] & '-' & $DateArray[2] & '-' & $DateArray[3]
   $errdate =  @WorkingDir & "\Error Logs\Error-" & $Now & ".txt"
   $FileHandle = Fileopen ($errdate, 1)
   FileWriteLine ($FileHandle, $errormsg)
   FileClose ($FileHandle)
EndFunc
Func _PingTestCheck ( ByRef ($bcomputer) )
Run ("cmd.exe")
Sleep (4000)
Send ( $cmdsnd & "{ENTER}" )
Sleep (10000)
Send ("exit{ENTER}")
Sleep (1000)
$frmcmdline = FileReadLine(@WorkingDir & "\work\" & $ctrlarray [2] & ".txt")
If @error = 1 Then
  $errorline = "Cannot open the Ping test file."
  _WriteErrorMsg( $errorline )
  FileDelete (@workingDir & "\work\" & $ctrlarray [2] & ".txt")
  Exit
EndIf
$frmcmdline = StringStripWS($frmcmdline, 3)
MsgBox (0,"","current file line : " & $frmcmdline)
$cmdsubstr = StringInStr($frmcmdline, "Ping request could not find host")
MsgBox (0,"","$cmdsubstr = " & $cmdsubstr)
If $cmdsubstr = 0 Then
  While 2
   $cmdsubstr = StringInStr($frmcmdline, "Lost = 4")
   FileReadLine($cmdsubstr)
   MsgBox(0,"","the file line being read is : " & FileReadLine($cmdsubstr))
   If @error = -1 Then
    MsgBox(0,"","the file line being read is : " & FileReadLine($cmdsubstr))
    ExitLoop
   EndIf
   If $cmdsubstr <> 0 Then
    $bcomputer = False
    ExitLoop
   EndIf
     WEnd
Else
  MsgBox(0,"","$cmdsubstr <> 0")
  MsgBox(0,"",$bcomputer)
  $bcomputer = False
  MsgBox(0,"",$bcomputer)
EndIf
EndFunc

the message boxes are for testing purposes of course. and the reason i didnt use the built in "ping" command was because i could not ping hostnames from an array. dont ask why, i honestly dont know, but it worked better this way actually. because of what im trying to do.

my problem is that when i reference the _pingtestcheck function results, it comes up false as need be, but the as soon as it exits the function it becomes true again. any advice? ive been racking my brain on this stupid thing for over a week now

Link to comment
Share on other sites

You should, inside the function, use Return instead of setting $bcomputer to False, cuz that won't change the original variable (the one you used to call the function).

In that way, instead of setting $bcomputer = FALSE you should use

Return False

Remember that when calling the function you should do it like

$bcptrexists = True

$bcptrexists = _PingTestCheck ($bcptrexists)

That way, when exiting the function and assuming you used Return, if $bcptrexists is changed to False, it will also be changed to False outside the function.

I guess that will most likely work out for ya.

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...