SSH application in Java

M

mateusz.zajakala

Hi,

I need to write small application in Java which will connect to
computer with Linux OS and execute "reboot" command there (the
direction is to automate this process and do it on lot of pc's "at
once" by one *kick*). All using SSH. I tried sshtools (get tons of
errors, especially becouse no logfactory), now I'm focused on jsch but
have problems too. Can you (group) please tell me if I'm going right
direction, or perhaps my task could be done way easier? I'll be
thankfull for any ideas and tips.

Thanks in advice,
Mateusz Zajakala
 
C

Christian

Hi,

I need to write small application in Java which will connect to
computer with Linux OS and execute "reboot" command there (the
direction is to automate this process and do it on lot of pc's "at
once" by one *kick*). All using SSH. I tried sshtools (get tons of
errors, especially becouse no logfactory), now I'm focused on jsch but
have problems too. Can you (group) please tell me if I'm going right
direction, or perhaps my task could be done way easier? I'll be
thankfull for any ideas and tips.

Thanks in advice,
Mateusz Zajakala
I have used jsch quite sucessful lately.. it has proven to be very
useful for me.
It may not be very well documented but the examples explain nearly
everything s.o. needs to know to use it.
So yes I think you are on the right path.
 
A

Alexey

Hi,

I need to write small application in Java which will connect to
computer with Linux OS and execute "reboot" command there (the
direction is to automate this process and do it on lot of pc's "at
once" by one *kick*). All using SSH. I tried sshtools (get tons of
errors, especially becouse no logfactory), now I'm focused on jsch but
have problems too. Can you (group) please tell me if I'm going right
direction, or perhaps my task could be done way easier? I'll be
thankfull for any ideas and tips.

Thanks in advice,
Mateusz Zajakala

I've done something similar using Ant's (http://ant.apache.org/) ssh
toools. It's an optional command, so you'll probably need to hunt
down a few external jars and take a bit of time to set everything up.
But all in all, I feel like it was faster than coding it myself and of
course it's all open source, so you can at least use it for reference.
 
M

mateusz.zajakala

now I'm focused on jsch but have problems too.
I have used jsch quite sucessful lately.. it has proven to be very
useful for me.

Looks like it works for me now too. It worked yesterday just after I
posted that message on forum (force majeure?), but I didn't have time
to check it more carefully - will do it today.
So yes I think you are on the right path.

Thanks for confirmation. I like to know if I'm on a good way to the
goal. I'll focus on Jsch more carefully now.
Thanks again,
Mateusz Zajakala
 
M

mateusz.zajakala

I've done something similar using Ant's ssh toools.

Thanks for idea. I'll look on it too, as later I'll have to extend my
application. If Jsch won't fit to solve my problem anymore, I'll have
an alternative.

Thank you,
Mateusz Zajakala
 
M

mateusz.zajakala

Looks like I solved my problem with Jsch. Precisely, with Jsch's
equivalent for C# - SharpSSH (as my application's going to work under
Windows Server, so I decided to use C#). Just for practise I'll
rewrite it to Java now, but that will be just formality. Generally I
looked through all suggestions you gave me but first choosen was just
sufficient. For that kind of problem like mine, using Jsch (SharpSSH)
was simple and good solution.

Thank you for support and ideas.
Mateusz Zajakala
 
Joined
Feb 17, 2012
Messages
1
Reaction score
0
SSH application

Hi,

I am trying to write a client-server data communication under SSH, which application will be payload data sent from the SSH client to the SSH server, but not through SFTP, but the command mode for sending the data packet by packet.

I am using JAVA, could anyone please help to advise me to start it ?

Thanks.

John
 
Joined
Mar 5, 2012
Messages
1
Reaction score
0
help required (trying to automate sending commands in SSH factory)

Hi,

I need help in automation of ssh. I am trying to send commands automatically so that it will fire commands and give me the output of it. As we all know that, ssh is for getting into the node and fire some commands and give output for that command.

I am using ssh factory jar file and i am trying to send commands automatically.
kindly check for the below code which i have tried:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.jscape.inet.ssh.Ssh;
import com.jscape.inet.ssh.SshAdapter;
import com.jscape.inet.ssh.SshConnectedEvent;
import com.jscape.inet.ssh.SshDataReceivedEvent;
import com.jscape.inet.ssh.SshDisconnectedEvent;
import com.jscape.inet.ssh.SshException;
import com.jscape.inet.ssh.SshScript;
import com.jscape.inet.ssh.SshTask;
import com.jscape.inet.ssh.SshTaskEndEvent;
import com.jscape.inet.ssh.SshTaskStartEvent;
import com.jscape.inet.ssh.SshTaskTimeoutException;

import com.jscape.inet.ssh.connection.channels.SessionClient;
import com.jscape.inet.ssh.util.SshParameters;

public class SshScriptTutorial extends SshAdapter {
public SshScriptTutorial() {}

public void executeSshScript(String hostname, String username, String password)
throws SshException, IOException, InterruptedException
{
// assumes that SSH shell prompt is "$" .. this MUST match exactly
String shellPrompt = ">";

// initialize and create new Ssh instance
SshParameters sshParams = new SshParameters(hostname,username,password);

Ssh ssh = new Ssh(sshParams);

// register this class to receive Ssh events
ssh.addSshListener(this);

// create new script object and bind to the given ssh object
SshScript script = new SshScript(ssh);

// add tasks to script object
script.addTask(new SshTask(shellPrompt, "show host", shellPrompt));
script.addTask(new SshTask(shellPrompt, "ssh ssgpun", shellPrompt));

// while sending password, it is not able to fire this.
script.addTask(new SshTask(shellPrompt, "password", ":")); // trying to send password to the server.

// connect to SSH server and execute script
ssh.connect();

// wait until last task is complete
while(!script.isComplete()) {
try {
Thread.sleep(500);
} catch(Exception e) {}
}

// disconnect from server
// ssh.disconnect();
}

public void connected(SshConnectedEvent event) {
System.out.println("Connected to host: " + event.getHost());
}

public void disconnected(SshDisconnectedEvent event) {
System.out.println("Disconnected from host: " + event.getHost());
}

public void dataReceived(SshDataReceivedEvent event) {
System.out.print(event.getData());
}

public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter SSH hostname: ");
// String hostname = reader.readLine();
// System.out.print("Enter SSH username: ");
// String username = reader.readLine();
String hostname = "hostname"; // ip of server
String username = "username";

System.out.print("Enter SSH password: ");
String password = reader.readLine();

SshScriptTutorial tutorial = new SshScriptTutorial();
tutorial.executeSshScript(hostname, username, password);

// System.out.print("Hi");
String cus_pass = reader.readLine();

} catch(Exception e) {
e.printStackTrace();
}
}
}



Output appears as,

Enter SSH password: 4732885288
Connected to host: 150.236.14.11
egw-pnq > show host
Node IP number Reverse SSH tunnel
_______________________________________________________________________
ssgpun 192.168.181.2 yes

egw-pnq > ssh ssgpun
Connecting to ssgpun as user
Password:


Till this i am able to send commands automatically. when i am trying to send password, it is not able to fire it.

I feel that this might be due to setting shell prompt or delay problem. but i dont know how to solve it.

If any1 knows, kindly help me out in this step.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top