Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've been used the ExpectJ Java library to automate some jobs on a Linux server, however now I need to automate a job that sends a different text response for each execution. Here is the output:

Last unsuccessful login: Fri Mar 30 15:21:31 CST 2012 
********************************************************************************

********************************************************************************
[YOU HAVE NEW MAIL] 
local-login [/home/user]%

And in my code I have the following:

s.expect("Login as:");
s.send("userlogin\n");
s.expect("userlogin@iphost's password:");
s.send("password\n");

I receive the output shown, but I wasn't be able to send the next command because I don't know how read the last answer, I tried to use a regular expression but it doesn't work.

share|improve this question
    
Are you having trouble with the command prompt string (ending with "%") or with the "userlogin@iphost password:" string? –  Arcege Mar 31 '12 at 0:34
    
The complete welcome test form the server –  fers Apr 1 '12 at 2:21
    
Why don't you just expect the ]% ? –  Stephane Chazelas Aug 12 '13 at 11:14
add comment

2 Answers

The preceding banner text does not need to be handled. Expect will wait until one of the conditions is met (like matching a string/regexp or timing out). Text before it are buffered for possible later use, but are not part of the match. What you need to handle is looking for the prompt string: local-login [/home/user]%. If you have a good idea what it may look like (for example, will always have '[' directory ']%', then you can write a regular expression to match against that:

.*\[(/[^/]+)+\]%

Make sure that you do not have a newline/carriage return in the regular expression since the output will not have one.

share|improve this answer
    
expectj is an java implementation of expect unix tool, so , are you talking about Expect or expectj? –  fers Apr 2 '12 at 3:22
    
I'm talking about an implementation of Expect (C, Python, Java or otherwise). If expectj can't handle regular expressions, then it isn't much of a very good (or useful) implementation. The documentation reads "pattern", try a regular expression and see. –  Arcege Apr 2 '12 at 4:02
add comment

I do not think you are able to use regular expressions to match in ExpectJ. It seems like they are relying on String.indexOf to find the match

if (line.toString().trim().toUpperCase().indexOf(pattern.toUpperCase()) != -1)

where pattern is the supplied String to expect.

So try instead to match on a substring and see if it works better.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.