sshtools: read output from a command

A

alcool

hi,
I would like to execute a command and read the output generated

// Create a instance and connect
SshClient ssh = new SshClient();
ssh.setSocketTimeout(30000);
ssh.connect("192.168.1.129");
PasswordAuthenticationClient pwd = new
PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("kondor");

// Execute command
session.executeCommand("date +%Y-%m-%d");

// here I would like to read the current date on the server


thx
bye
 
D

derek

hi,
I would like to execute a command and read the output generated
// Create a instance and connect
SshClient ssh = new SshClient();
ssh.setSocketTimeout(30000);
ssh.connect("192.168.1.129");
PasswordAuthenticationClient pwd = new
PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("kondor");
// Execute command
session.executeCommand("date +%Y-%m-%d");
// here I would like to read the current date on the server
thx
bye

A quick search on the internet showed 3 or 4 libraries with a class named SshClient in it.
What library are you using for the SshClient class? Try reading the API docs for that library first.
 
A

alcool

Solution found!

post the code;

SessionChannelClient session = ssh.openSessionChannel();
InputStream in = session.getInputStream();
session.executeCommand(command);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
while ((line = br.readLine()) != null) {
buffer.append(line);
}
output = buffer.toString();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top