Expectit Example for login using ssh and then interact it using terminal

775 views
Skip to first unread message

develope...@gmail.com

unread,
Nov 16, 2017, 10:52:53 PM11/16/17
to Yet another Expect for Java
Hi,

I am trying to create a program to login into server using ssh and then use that server's console. Could you please tell me if it is possible to do it with expect it ? also, is there any example for this ? i just checked https://github.com/Alexey1Gavrilov/ExpectIt/blob/master/expectit-core/src/test/java/net/sf/expectit/SshLocalhostNoEchoExample.java


Alexey Gavrilov

unread,
Nov 17, 2017, 2:51:26 AM11/17/17
to develope...@gmail.com, Yet another Expect for Java
Hi,



It is essentially interaction with a remote shell over SSH.

Regards,
Alexey

On 17 Nov 2017, at 04:52, develope...@gmail.com wrote:

Hi,

I am trying to create a program to login into server using ssh and then use that server's console. Could you please tell me if it is possible to do it with expect it ? also, is there any example for this ? i just checked https://github.com/Alexey1Gavrilov/ExpectIt/blob/master/expectit-core/src/test/java/net/sf/expectit/SshLocalhostNoEchoExample.java


--
You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

develope...@gmail.com

unread,
Nov 17, 2017, 4:31:33 AM11/17/17
to Yet another Expect for Java
Hi Alexy,

Thank you for your reply. I used one of the above example and able to login using ssh and then it will show the logged server's console for me. But i can't enter the commands and execute. What i am looking is that the script should login into server's console as like now and then can able to type the commands in this terminal as like in the normal logged server's terminal.

The following is the code that am currently using.

JSch jSch = new JSch();
Session session = jSch.getSession("root", "xx);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
String passwd = "xx";
session.setPassword(passwd);

session.setConfig(config);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();

Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.build();
try {
expect.expect(contains("# "));
expect.sendLine("pwd");

} finally {
expect.close();
channel.disconnect();
session.disconnect();
}


Last login:
[root@test ~]# w

Alexey Gavrilov

unread,
Nov 17, 2017, 3:19:14 PM11/17/17
to develope...@gmail.com, Yet another Expect for Java
Hi,

OK, I see. If you want to write a program which will interact like a terminal after ExpectIt has logged in, you should add a loop where you would read from the standard input (basically from the keyboard), then write to the ssh output and expect for the command prompt. One you receive the command prompt, you can extract the result and write to the standard output.

Take a look at this example:

Hope this helps,
Alexey

develope...@gmail.com

unread,
Nov 19, 2017, 11:23:03 PM11/19/17
to Yet another Expect for Java
Hi Alexy,

Thank you once again for your info and updates. I have created the following program to login into the console and interact. The problem that i am facing is that am able to connect and send the command to the console but it seems not working as like the normal console of a server, not working with all commands, arrows etc. Do you have any idea about this ? or could you please have a look at the following program ?



import static net.sf.expectit.matcher.Matchers.contains;
import static net.sf.expectit.matcher.Matchers.regexp;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.*;
import java.util.Scanner;


class SshLocalhostNoEchoExample {
public static void main(String[] args) throws JSchException, IOException {


JSch jSch = new JSch();
Session session = jSch.getSession("root", "xxx.xxx.xx.xxx");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
String passwd = "password";
session.setPassword(passwd);

session.setConfig(config);
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();

Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoOutput(System.out)
.withEchoInput(System.err)
.build();
try {
expect.expect(contains("$"));
expect.interact();
expect.sendLine("stty -echo");
expect.expect(contains("$"));

while(true)
{
Scanner fname = new Scanner(System.in);
expect.sendLine(fname.nextLine());
expect.interact();
}
} finally {

}
}
}


phillip.k...@googlemail.com

unread,
May 16, 2019, 1:41:20 PM5/16/19
to Yet another Expect for Java
> To unsubscribe from this group and stop receiving emails from it, send an email to java-e...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Could you maybe provide an example ?
I can't get it working. I also tried the interact method with no success.

final Expect expect = new ExpectBuilder()
.withOutput(shell.getOutputStream())
.withInputs(shell.getInputStream(), shell.getErrorStream())
//.withEchoInput(System.out)
//.withEchoOutput(System.err)
.withInputFilters(removeColors(), removeNonPrintable())
//.withExceptionOnFailure()
.build();
try {
// Terminal like Eingabe von der Console
//Scanner scanner = new Scanner(System.in);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = "";
String result = "";

while(true)
{
input = in.readLine();
expect.sendLine(input);
result = expect.expect(contains("#")).getBefore();
System.out.println(result);

}
} finally {
expect.close();
session.close();
client.close();
}
}
Reply all
Reply to author
Forward
0 new messages