SSH application in Java

Discussion in 'Java' started by mateusz.zajakala@gmail.com, Aug 21, 2007.

  1. Guest

    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
     
    , Aug 21, 2007
    #1
    1. Advertising

  2. Christian Guest

    schrieb:
    > 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.
     
    Christian, Aug 21, 2007
    #2
    1. Advertising

  3. Alexey Guest

    On Aug 21, 9:29 am, wrote:
    > 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.
     
    Alexey, Aug 21, 2007
    #3
  4. Guest

    > >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
     
    , Aug 22, 2007
    #4
  5. Guest

    On 21 Sie, 21:16, Alexey <> wrote:
    > 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
     
    , Aug 22, 2007
    #5
  6. Roedy Green Guest

    Roedy Green, Aug 22, 2007
    #6
  7. Guest

    You might look at SSH Factory. It has a SshSession class which is
    ideal for this sort of thing (automating SSH sessions).

    http://www.jscape.com/sshfactory/


    wrote:
    > 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
     
    , Aug 23, 2007
    #7
  8. Guest

    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
     
    , Aug 23, 2007
    #8
  9. johnlin

    Joined:
    Feb 17, 2012
    Messages:
    1
    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
     
    johnlin, Feb 17, 2012
    #9
  10. sathish mg

    Joined:
    Mar 5, 2012
    Messages:
    1
    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.
     
    sathish mg, Mar 5, 2012
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loial
    Replies:
    0
    Views:
    440
    loial
    Feb 3, 2009
  2. loial
    Replies:
    0
    Views:
    427
    loial
    Feb 10, 2009
  3. Tench Johnson
    Replies:
    1
    Views:
    326
    Biff Tannen
    Apr 23, 2010
  4. salamond

    net/ssh in ruby. ssh.exec fails

    salamond, Feb 17, 2011, in forum: Ruby
    Replies:
    0
    Views:
    277
    salamond
    Feb 17, 2011
  5. Deepika
    Replies:
    2
    Views:
    238
    Deepika
    Nov 14, 2006
Loading...

Share This Page