Jump to content

justhatched

Members
  • Posts

    5
  • Joined

  • Last visited

justhatched's Achievements

Seeker

Seeker (1/7)

0

Reputation

  1. Stacks of js here, and the continue button is not for posting the form but for changing the display of various elements which then need stuff entered. so the way I would solve is: $oButtons = _IETagNameGetCollection( $oIE, "button" ) for $oButton in $oButtons if StringInStr( "Continue", $oButton.innertext ) > 0 Then _IEAction( $oButton "click" ) ExitLoop EndIf Next Threw in off the top of my head so you may need to check..
  2. Cannot get to your site due to a corporate proxy block, but found that with alot of modern pages some elements only exist in script and get created dynamically in the html. What this means is that you may need to: 1) Trigger the creation of the element by following a link or some other action that triggers the js to create the object 2) Sleep() regularly - loadwait only waits for a page load, not an ajax call c) have an object loadwait function to reduce peppering your code with too many naps, eg: ;routine for waiting until an object exists on a webpage ;returns true if the object exists before global timeout variable value, else False, use flag to search by html id or html name Func bWaitTillObjExists( $sObj, $iTimeout, $bByID ) local $return = False local $iBegin = TimerInit() Do Sleep(50) local $oObj = _iif($bByID,_IEGetObjById( $oIE, $sObj ), _IEGetObjByName( $oIE, $sObj )) until isObj($oObj) or ( TimerDiff($iBegin) > $iTimeout ) ;waiting until Object is found max 10 seconds if isObj($oObj) then $return = True return $return EndFunc Also, depending on the app, you may need to reset the container variable that you are using to search for an object. quite often I need to reset the form object variable as otherwise the newly created form elements are invisible as they did not exist when the form variable was instantiated, only when a field was updated/some action triggered the script.
  3. Thanks susserj, toyed with adding an optional param to the _expect function for a lossy buffer for the same reason, as some scenarios you only need to know it got there, whereas in other scenarios you may need to process the data(eg a list). Was not sure what could generate so much data without asking for it that it would blow up so left it, and it also meant no calling script would fail with param mismatches. The reason I dropped the _Expector function was because it looked like a select case from the calling script would allow a better separation into script vs library, as well as more flexibility. eg you can have as many or few inputs to a select case statement. Horses for courses tho so whatever works best for the one doing the work! This is a nice library for reusing plink functions in all sorts of ways, thanks for putting it up.
  4. Added some tweaks: 2011-06-01 Added escaping of commandline so that if plink is in a dir with spaces it still works, 2011-06-05 Added global timeout const for expect functions so that if the text is not found the calling script can either handle or set an error state(not hang) 2011-06-05 Added return value for _expect and _expectlog functions so that timeout can be trapped, and so that the buffertext can be parsed for other information by the calling script. eg the use case is when the needle text may be found, but the actual text that is required for the calling text may be at different point(s) in the buffer eg scraping a web page or spreadhseet, walking thru some green screens and populating a third system based on green screen data 2011-06-05 Deleted the expector function, as the ability to parse the buffer from the calling script has more flexibilty #include-Once ;#include <Constants.au3> ; ==================================================================================================================================== ; Title....................: plink_wrapper ; AutoIt Version...........: 3.3.6.1 ; Language.................: English ; Description..............: UDF Functions to Control "plink.exe" allowing intelligent response to text displayed in terminal sessions ; Author...................: Joel Susser (susserj) ; Last Modified............: 5/6/2011 (Phil; justhatched) ; Status:..................: Alpha ; Testing Workstation......; WinXP sp3, Vista Business (It likely works with other versions of Windows but I cannot confirm this right now ) ; Tested Version of Autoit.; Autoit 3.3.6.1 ; What is plink? ;plink is a command line terminal emulation program similar to putty that allows rudimentary scripting. ;Requirements ; Autoit 3.3.6.X ; putty.exe, plink.exe. You can acquire these programs at "http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html". ; Hints ; If you are using the ssh protocol I suggest you connect to your server first ; with putty before you use plink for the first time so that you will not ; be disrupted by the authentication certificate requests. ; When entering your userid and password variables I suggest that you add an ; additional space at the end of these strings. I'm not sure why but if you don't it will likely ; cut off the last letters of your userid and password. ; Figuring out what screen information to wait for before continuing the data input stream ; can sometimes be difficult. I suggest using the putty logging feature to record the text that appears on each screen. ; I believe it is advisable to do so in small chunks. ; Also, choose strings to recognize that are unique and at the end of the putty screen capture logging sessions ; for each screen. Avoid terminal escape coding. ; ================================================================================================================================ #comments-start Changelog: 4-12-2011 no changes yet 2011-06-01 Added escaping of commandline so that if plink is in a dir with spaces it still works, 2011-06-05 Added global timeout const for expect functions so that if the text is not found the calling script can either handle or set an error state 2011-06-05 Added return value for _expect and _expectlog functions so that timeout can be trapped, and so that the text can be parsed for other information by the calling script. eg the use case is when the text may be found, but the actual text that is required for the calling text may be at a different point in the buffer eg scraping a web page or spreadhseet, walking thru some green screens and populating a third system based on green screen data 2011-06-05 Deleted the expector function, as the ability to parse the buffer from the calling script has more flexibilty #comments-end ;#Current List of Functions======================================================================================================= ;_Start_plink($_plink_loc,$_plinkserver) ;_Plink_close() ;_Init_plink_log($_plink_logfile) ;_Expect($match_text) ;_Expector($match_text1, $say1, $match_text2, $say2, $match_text3, $say3) ;_Expectlog($match_text) ;_Say($output) ;_SayPlus($output) ;_Plink_close_logfile($_plink_logfile_handle) ; =============================================================================================================================== ; #VARIABLES# ==================================================================================================================== global $_plinkhandle="" ; used to handle starting of plink global $_plinkserver="" ; name of server you wish to connect to global $_plink_loc ="" ; location of plink.exe executable on workstation global $_plink_display_messages=false ; display interim messages to screen (default false) global $_plink_display_message_time=1 ; time in seconds messages are displayed global $_plink_logging=false ; record plink log file (default false) global $_plink_logfile="" ; location of plink log file global $_plink_logfile_handle="" ; plink log file handle global Const $_plink_timeout = 5000 ; Global timeout for expect based functions so no hanging.... ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Plink_close ; Description ...: closes plink.exe process ; Author ........: Joel Susser (susserj) ; Syntax.........: $_Plink_close() ; Parameters ....: none required ; example .......: _Plink_close() ; ; Remarks .......: plink.exe should only be running once ; =============================================================================================================================== func _Plink_close() ;If there are any stray plink sessions kill them if ProcessExists("plink.exe") then ProcessClose("plink.exe") Else return false endif EndFunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Start_plink ; Description ...: open a new plink.exe terminal session ; Syntax.........: $_plinkhandle=_Start_plink($_plink_loc, $_plinkserver) ; Parameters ....: $_plink_loc is the location of the plink.exe ececutable on you workstation ; Parameters ....: $_plinkserver is the location of the server you wish to access ; Example........: $_plinkhandle = _Start_plink("c:/putty/plink.exe", "testserver.com") ; ; Author ........: Joel Susser (susserj) ; Return values .: $plinkhandle, pid of cmd processor ; Remarks........; Currently configured to use ssh protocol ; Remarks .......: Needs to be made more robust ; =============================================================================================================================== ;start the plink session func _Start_plink($_plink_loc,$_plinkserver) _Plink_close(); close any stray plink sessions before starting if $_plink_loc = "" then MsgBox(0, "Error", "Unable to open plink.exe",10) return false Exit endif if $_plinkserver = "" then MsgBox(0, "Error", "Unable to open server",10) Exit return false endif $_plinkhandle = Run(@comspec & " /c" & $_plink_loc & " -ssh " & $_plinkserver,"",@SW_HIDE, 7) ; $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD) return $_plinkhandle endFunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Init_plink_log ; Description ...: open a new file handle for saving a log file recording your plink terminal session ; Syntax.........: _Init_plink_log($_plink_logfile) ; Parameters ....: $_plink_logfile is the location of the log file ; Example........: _Init_plink_log("c:/putty/plink.log") ; Author ........: Joel Susser (susserj) ; Remarks .......: If the file exists it will be ovewritten (2) ; Remarks........: Initializing the log file does not mean logging gets turned on. ; remarks........; Logging gets turned on with the flag $_plink_logging=true. The default is false ; Remarks........; Sometimes the log files get too long and you just want to log a small section of code see $_Expectlog() ; Remarks........; ; =============================================================================================================================== ;Initialize plink session log file func _Init_plink_log($_plink_logfile) $_plink_logfile_handle = FileOpen($_plink_logfile, 2) ; Check if file opened for writing OK If $_plink_logfile_handle = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf return true endfunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Expect ; Description ...: Pause until expected text is displayed on output stream of terminal in less than $iTimeout global const ; Syntax.........: _Expect("text string") ; Parameters ....: one parameter containing a text string. ; Returns........: Stdoutread buffer up to the matched text string, else 0 if timed out ; Example........: _Expect("Password:") ; Author ........: Joel Susser (susserj) ; Remarks .......: If the flag $_plink_logging is set to true then it will record all the data sent to the output screen ; Remarks........: while it is waiting for the required text to appear. If it runs to long and doesn't find the text this ; remarks........; file can get very big so be careful. ; Remarks........; If the flag $plink_display_message is set to true then it will popup a messages showing you that the text is found. ; Remarks........; I usaully leave the $_plink_display_messages flag on but reduce the time display to 1 second. However, during ; Remards........; development I usually bump it up to 5 seconds. ; =============================================================================================================================== func _Expect($match_text) local $text local $sBuffertext local $found local $iBegin = TimerInit() While 1 if TimerDiff($iBegin) > $_plink_timeout then return false $text = StdoutRead($_plinkhandle) $sBuffertext = $sBuffertext & $text if $_plink_logging Then filewriteline($_plink_logfile_handle,"**********************NEW SECTION************************") filewrite($_plink_logfile_handle, $match_text) filewrite($_plink_logfile_handle, $text) filewriteline($_plink_logfile_handle,"**********************END SECTION************************") endif $found = StringRegExp($text,$match_text) If $found = 1 Then If $_plink_display_messages Then MsgBox(4096, $match_text, $text, $_plink_display_message_time) ExitLoop endif sleep(100) Wend return $sBuffertext EndFunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Expectlog ; Description ...: Similar to _Expect but if forces a logging of the data for a particular search patern in the input stream ; Description ...: Pause until text is displayed on output stream of terminal ; Syntax.........: _Expectlog("text string") ; Parameters ....: one parameter containing a text string. ; Example........: _Epector("Password:") ; Author ........: Joel Susser (susserj) ; Remarks .......: ; Remarks........: As I indicated previously, the log files can get very big. ; remarks........; This function allows targeted logging related to a specific area of your script. ; Remarks........; It is primarily used for debugging. After your script is functional, you will likely convert your _Expectlog ; Remarks........; function calls to _Expect. ; Remards........; ; =============================================================================================================================== func _Expectlog($match_text) local $text local $sBuffertext local $iBegin = TimerInit() local $found While 1 if TimerDiff($iBegin) > $_plink_timeout then return false $text = StdoutRead($_plinkhandle) $sBuffertext = $sBuffertext & $text filewrite($_plink_logfile_handle,"**********************" & $match_text & "************************" ) filewrite($_plink_logfile_handle,$match_text) filewrite($_plink_logfile_handle,$text) $found = StringRegExp($text,$match_text) If $found = 1 Then MsgBox(4096, $match_text, $text, 10) ExitLoop endif sleep(100) Wend return $sBuffertext EndFunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Say ; Description ...: Send the following text string to the input stream of the terminal session ; Syntax.........: _Say("text") ; Parameters ....: Function takes on parameter which contains the text to sent to the input stream. ; Example........: _Say("y") ; Author ........: Joel Susser (susserj) ; Remarks .......: Don't try and say to much at once. Wait for the screen to appear with the prompt for information ; Remards........; This say does not perform a carrage return. If you need a carrage return use _SayPlus ; =============================================================================================================================== ; Say Function func _Say($output) StdinWrite($_plinkhandle, $output) endfunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _SayPlus ; Description ...: Send the following text string to the input stream of the terminal session ; Syntax.........: _SayPlus("text") ; Parameters ....: Function takes on parameter which contains the text to sent to the input stream. ; Example........: _SayPlus("y") ; Author ........: Joel Susser (susserj) ; Remarks .......: Don't try and say to much at once. Wait for the screen to appear with the prompt for information ; Remards........; This type of say performs a carrage return. If you don't need a carrage return use _Say() ; =============================================================================================================================== func _SayPlus($output) If $_plink_display_messages Then MsgBox(4096, "Output", $output, $_plink_display_message_time) StdinWrite($_plinkhandle, $output & @CR) endfunc ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _Plink_close_logfile ; Description ...: close the plink logging file ; Syntax.........: _Plink_close_logfile($_plink_logfile_handle_) ; Parameters ....: $_Plink_close_logfile_handle ; Example........: N/A ; Author ........: Joel Susser (susserj) ; Remarks .......: ; Remards........; ; =============================================================================================================================== ;Close log file func _Plink_close_logfile($_plink_logfile_handle) FileClose($_plink_logfile_handle) endfunc ; =============================================================================================================================== ; Sample Script Starts Here============================================================================================================= ; #cs ;Initialize Variables------------------------------------------------------------------------------------------------------------ $username="admin " ; It seems necessary to put an extra space after the login name. The last character is being cut off. $password="admin1 "; It seems necessary to put an extra space after the password. The last characters is being cut off. $_plink_logging=true ; Turn Logging ON (default is off) $_plink_display_messages=true ; Turn Screen Messages ON (default is on) $_plink_display_message_time=5 ; Display message for 5 secs (default 1 sec) ;-------------------------------------------------------------------------------------------------------------------------------- ; Initizations------------------------------------------------------------------------------------------------------------------- $_plinkhandle=_Start_plink("c:\putty\plink.exe","hostname.com"); Initialized plink connection to server _Init_plink_log("c:\putty\plink.log"); Initialize log file. Required even it not being used ; ------------------------------------------------------------------------------------------------------------------------------- ; Terminal Emulation Session Interaction _Expect("login as:") _SayPlus($username) _Expect("Password:") _SayPlus($password) $buffertext = _Expect("hostname >") if not $buffertext Then ;we have timed out waiting for our string, so failed to login _Plink_close() _Plink_close_logfile($_plink_logfile_handle);shutdown log file Exit Else ;record something about the machine we have logged into, and optionally apply some logic to what is found _SayPlus("ls") _Expect("hostname >") _SayPlus("exit") EndIf ;SHUTDOWN----------------------------------------------------------------------------------------------------------------------- _Plink_close(); shutdown plink session _Plink_close_logfile($_plink_logfile_handle);shutdown log file ; ------------------------------------------------------------------------------------------------------------------------------ #ce
  5. This is really nice and just what I have been looking for! Just a tiny suggestion - escaping/quoting the command string for paths with spaces so it doesn't break - took me a little while to figure out why I had a blank with no errors...
×
×
  • Create New...