From: <ex...@th...> - 2004-07-12 16:46:09
|
I'm trying to write a script in perl, using the expect.pm module, which will connect via ssh to a remote system and run a series of commands. I can connect and run commands, but am having trouble with the interactive aspects of the script. I need to check the status of the system and make calls accordingly, but cannot find a way to make "blocking" calls (expect, wait, send) or catch the return value (e.g. send ("ls\n") and catch the results). Could you folks provide me with sample code or links to documentation that could help with this? I've read through the man, er perldoc, page and could not find solutions there. thanks, Andrew ex...@th... |
From: Bala A. <ba...@sb...> - 2004-07-13 02:23:47
|
Expect doesn't have a blocking mode or concept. Except gets chunks from the kernel buffer and it isn't always keeping up with the interacting program. That is, expect is usually behind and the application it is interacting with is usually ahead. To sync up both, (to block like you alluded to) , you need to loop on the pattern you are looking for. You need to consume all the patterns that are available on the stdout, produced either by you "sending" them or produced by the application. If you pass up any input, then your expect program may appear to work, but not consistently. Take ls \n for example. You need to loop looking for the command to return to unix prompt such $ or # or > or what have you. While waiting - you should consume everything that comes your way by using expect(3,'-re', "(.*?)\n") (note .*? won't consume \n thus you have hard code \n. Every time you consume you should put that out to a buffer such as $buffer .= $obj->match() ; Sometimes you may have to use expect_before() but you have to play with it to see what suits your response best. I will be happy to post some samples if you need more help. Good luck expecting. B Ayres --- ex...@th... wrote: > I'm trying to write a script in perl, using the > expect.pm module, which will > connect via ssh to a remote system and run a series > of commands. I can connect > and run commands, but am having trouble with the > interactive aspects of the script. > > I need to check the status of the system and make > calls accordingly, but cannot > find a way to make "blocking" calls (expect, wait, > send) or catch the return > value (e.g. send ("ls\n") and catch the results). > > Could you folks provide me with sample code or links > to documentation that could > help with this? I've read through the man, er > perldoc, page and could not find > solutions there. > > thanks, > Andrew > ex...@th... > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & > Training. > Attend Black Hat Briefings & Training, Las Vegas > July 24-29 - > digital self defense, top technical experts, no > vendor pitches, > unmatched networking opportunities. Visit > www.blackhat.com > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > |
From: Austin S. <te...@of...> - 2004-07-13 06:57:36
|
On Mon, Jul 12, 2004 at 07:23:41PM -0700, Bala Ayres wrote: > Expect doesn't have a blocking mode or concept. Not quite true, case in point: $session->expect(); If you pass undef as the first argument to expect() it will block until it has matching patterns. In the example above it will block until EOF. This is useful e.g. where you wait for a process about to end but want to make sure it finishes spitting out its output first. > Except > gets chunks from the kernel buffer and it isn't always > keeping up with the interacting program. That is, > expect is usually behind and the application it is > interacting with is usually ahead. To sync up both, > (to block like you alluded to) , you need to loop on > the pattern you are looking for. You need to consume > all the patterns that are available on the stdout, > produced either by you "sending" them or produced by > the application. If you pass up any input, then your > expect program may appear to work, but not > consistently. > > > Take ls \n for example. You need to loop looking for > the command to return to unix prompt such $ or # or > > or what have you. > > While waiting - you should consume everything that > comes your way by using expect(3,'-re', "(.*?)\n") > (note .*? won't consume \n thus you have hard code \n. > You can do a nonblocking grab of existing output by doing expect(0, '.*'). The advantage being that if there is no data available it won't wait the 3 seconds as in the example above. > Every time you consume you should put that out to a > buffer such as $buffer .= $obj->match() ; > Sometimes you may have to use expect_before() but you > have to play with it to see what suits your response > best. > > I will be happy to post some samples if you need more > help. Good luck expecting. > There are also a few examples included with the tutorial. They are generally out of date, but maybe a good place to start as well. If you have code that would be of general use please copy it here, perhaps it would be useful to add to the module. Austin |