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 

local-login [/home/user]%

And in my code I have the following:

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

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.

  • Are you having trouble with the command prompt string (ending with "%") or with the "userlogin@iphost password:" string?
    – Arcege
    Commented Mar 31, 2012 at 0:34
  • The complete welcome test form the server
    – fers
    Commented Apr 1, 2012 at 2:21
  • Why don't you just expect the ]% ? Commented Aug 12, 2013 at 11:14

2 Answers 2


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.

  • expectj is an java implementation of expect unix tool, so , are you talking about Expect or expectj?
    – fers
    Commented Apr 2, 2012 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
    Commented Apr 2, 2012 at 4:02

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .