Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: SSH session with server

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SSH session with server

    Hi all!

    I am a beginner at Java, just signed in the forum.
    I am currently trying to setup an ssh session with a server. I found that I can use JSch package for this reason. So I have imorted relevant .jar file and set CLASSPATH variable accordingly in my Tomcat folders.
    I am trying to javac a code that I found in the internet in order to check if it's running, just for testing reasons but I think I am missing something. The error I receive is the following:

    sshver2.java:1: error: class, interface, or enum expected
    SshClient.java.*;

    Is there a mistake on the way I have imported the libraries or should I include something additional in my folders?
    What steps should I do in order to run this code?

    I am attaching the code I am trying to run on my tomcat server (copyright by James Carr)

     
    SshClient.java.*;
     
    package com.rozi.*;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.regex.Pattern;
     
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
    import com.jcraft.jsch.UserInfo;
    import java.io.*;
    import com.jcraft.jsch.*;
     
    public class SshClient {
    private JSch shell;
    private Session session;
    private Channel channel;
    private static OutputStream out;
    private static InputStream in;
     
    public void connect(String username, String password, String host, int port)
    throws JSchException, IOException, InterruptedException {
    shell = new JSch();
    session = shell.getSession(username, host, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();
     
    channel=session.openChannel("shell");
    channel.setInputStream(null);
    channel.setOutputStream(null);
     
    in=channel.getInputStream();
    out =channel.getOutputStream();
    ((ChannelShell)channel).setPtyType("vt102");
    channel.connect();
    }
     
    public String send(String command) throws IOException, InterruptedException {
    byte[] tmp=new byte[1024];
     
    out.write((command+";echo \"z4a3ce4f3317Z\"").getBytes());
    out.write(("\n").getBytes());
    out.flush();
     
    String result = "";
    while(true){
    while(in.available()>0){
    int i=in.read(tmp, 0, 1024);
    if(i<0)
    break;
     
    result = result + (new String(tmp, 0, i));
    }
    if(result.indexOf("z4a3ce4f3317Z") != -1){
    break;
    }
    try{Thread.sleep(100);}catch(Exception ee){}
    }
    return result;
    }
     
    public boolean isConnected() {
    return (channel != null && channel.isConnected());
    }
     
    public void disconnect() {
    if (isConnected()) {
    channel.disconnect();
    session.disconnect();
    }
    }
    }



    Pleeeeeeease for your help!!! I am stuck! I have the impression that I am doing something very very wrong!!!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSH session with server

    SshClient.java.*;
    The compiler says that is not a valid java statement.
    What is the purpose of that line?
    Make it a comment by adding // in column one.

    The code is not properly formatted for viewing. Nested statements should be indented 3-4 spaces to make the code easier to read.
    For example:
          for(int n=2; n < 6; n+=3) {
             int counter = 0; 
             for (int i = n; i <= 3*n; i++){
                System.out.println("n="+n + ", i="+i + ", cntr="+counter);
                while(i > +n){            //  +n ???
                   i--;        
                   counter++;
                }
             } //  end for(i)
             System.out.println("n="+n + " cntr="+counter);
          } // end for(n)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SSH session with server

    Hi Norm!

    Thanks for your reply! I do not know exactly the purpose of the line.
    As I noticed the code is not mine and I am trying to see this example in order to understand how JSch library is working.
    I took the code from the following site.

    SSH Over Java | James Carr

    It was supposed to work, so I hoped that I could understand the code, but I believe that I probably do not run it properly. I made it comment but now I take the following message:

    19:error:class SshClient is public, should be declared in a file named SshClient.java public class SshClient{

    Do you have any ideas?????

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSH session with server

    19:error:class SshClient is public, should be declared in a file named SshClient.java
    The file holding a public class should be named the same as the classname. Rename the file as suggested in the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Paris (January 10th, 2013)

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SSH session with server

    Hi Norm,

    Thanks a lot for your reply! I haven't looked at this code yet since I am lost in my own code!!! I am trying to create my own class for the session. So I will deal with this later on... Anyway thank you very very much. I will keep you updated!

  7. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SSH session with server

    Hi Norm!

    Ooups!!! This is the definition of a very silly mistake...

    SshClient.java is the name of the program!!!!!!! And I simply copied this in the code!!! Really silly!!! But ok, I am still a newbi!!!!
    Trying to make the code run I added .* and then I left this like that:
    SshClient.java.*;

    Afterwards, I hadn't organized the com.rozi folder properly!!!!! Ok!!!! I hope I will ask something more serious next time!!

    Thanks again for your feedback!!!!

    BB

  8. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SSH session with server

    Hi Norm and All,

    The final code that I run is the following one.

    package gr.kestrel.sshstuff;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.regex.Pattern;

    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
    import com.jcraft.jsch.UserInfo;
    import java.io.*;
    import com.jcraft.jsch.*;

    public class SshClient {
    private JSch shell;
    private Session session;
    private Channel channel;
    private static OutputStream out;
    private static InputStream in;

    public void connect(String username, String password, String host, int port)
    throws JSchException, IOException, InterruptedException {
    shell = new JSch();
    session = shell.getSession(username, host, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();

    channel=session.openChannel("shell");
    channel.setInputStream(null);
    channel.setOutputStream(null);

    in=channel.getInputStream();
    out =channel.getOutputStream();
    ((ChannelShell)channel).setPtyType("vt102");
    channel.connect();
    }

    public String send(String command) throws IOException, InterruptedException {
    byte[] tmp=new byte[1024];

    out.write((command+";echo \"z4a3ce4f3317Z\"").getBytes());
    out.write(("\n").getBytes());
    out.flush();

    String result = "";
    while(true){
    while(in.available()>0){
    int i=in.read(tmp, 0, 1024);
    if(i<0)
    break;

    result = result + (new String(tmp, 0, i));
    }
    if(result.indexOf("z4a3ce4f3317Z") != -1){
    break;
    }
    try{Thread.sleep(100);}catch(Exception ee){}
    }
    return result;
    }

    public boolean isConnected() {
    return (channel != null && channel.isConnected());
    }

    public void disconnect() {
    if (isConnected()) {
    channel.disconnect();
    session.disconnect();
    }
    }
    }



    I tried to test it with the following Test.java script.



    import gr.kestrel.sshstuff.*;

    public class Test {
    public static void main(String[] args) {
    try{
    SshClient client = new SshClient();
    client.connect("lab", "lab123", "192.168.200.220", 22);
    System.out.println("\nConnection Established\n");
    client.send("show interfaces terse");
    System.out.println("\nCommands are being given to the system...Please wait...\n");
    System.out.println(client.send("show interfaces terse"));
    client.disconnect();
    }catch(Exception e){
    System.out.println(e);
    }
    }
    }


    My aim is to establish a connection with a firewall and then fire some print commands in the firewall and then take the printout. From the firewall commands, I can understand that a session is attempted to be established. However this action fails. The connection is never shown to be established, according to the firewall logs. The Test.java that I run gives the following printout...


    Connection Established

    Commands are being given to the system...Please wait...

    show interfaces terse;echo "z4a3ce4f3317Z"



    I think that something is wrong with the class SshClient... Any ideas?

    --- Update ---

    Hi Norm and All again,

    I am attaching in the correct format once again.

    The final code that I run is the following one.



     
    package gr.kestrel.sshstuff;
     
    import java.io.BufferedReader;
     import java.io.IOException;
     import java.io.InputStreamReader;
     import java.io.OutputStream;
     import java.util.regex.Pattern;
     
    import com.jcraft.jsch.Channel;
     import com.jcraft.jsch.JSch;
     import com.jcraft.jsch.JSchException;
     import com.jcraft.jsch.Session;
     import com.jcraft.jsch.UserInfo;
     import java.io.*;
     import com.jcraft.jsch.*;
     
    public class SshClient {
     private JSch shell;
     private Session session;
     private Channel channel;
     private static OutputStream out;
     private static InputStream in;
     
     public void connect(String username, String password, String host, int port)
     throws JSchException, IOException, InterruptedException {
     shell = new JSch();
     session = shell.getSession(username, host, port);
     session.setPassword(password);
     session.setConfig("StrictHostKeyChecking", "no");
     session.connect();
     
     channel=session.openChannel("shell");
     channel.setInputStream(null);
     channel.setOutputStream(null);
     
     in=channel.getInputStream();
     out =channel.getOutputStream();
     ((ChannelShell)channel).setPtyType("vt102");
     channel.connect();
     }
     
     public String send(String command) throws IOException, InterruptedException {
     byte[] tmp=new byte[1024];
     
     out.write((command+";echo \"z4a3ce4f3317Z\"").getBytes());
     out.write(("\n").getBytes());
     out.flush();
     
     String result = "";
     while(true){
     while(in.available()>0){
     int i=in.read(tmp, 0, 1024);
     if(i<0)
     break;
     
     result = result + (new String(tmp, 0, i));
     }
     if(result.indexOf("z4a3ce4f3317Z") != -1){
     break;
     }
     try{Thread.sleep(100);}catch(Exception ee){}
     }
     return result;
     }
     
     public boolean isConnected() {
     return (channel != null && channel.isConnected());
     }
     
     public void disconnect() {
     if (isConnected()) {
     channel.disconnect();
     session.disconnect();
     }
     }
     }



    I tried to test it with the following Test.java script.



     
    import gr.kestrel.sshstuff.*;
     
    public class Test {
     public static void main(String[] args) {
     try{
     SshClient client = new SshClient();
     client.connect("lab", "lab123", "192.168.200.220", 22);
     System.out.println("\nConnection Established\n");
     client.send("show interfaces terse");
     System.out.println("\nCommands are being given to the system...Please wait...\n");
     System.out.println(client.send("show interfaces terse"));
     client.disconnect();
     }catch(Exception e){
     System.out.println(e);
     }
     }
     }

    My aim is to establish a connection with a firewall and then fire some print commands in the firewall and then take the printout. From the firewall commands, I can understand that a session is attempted to be established. However this action fails. The connection is never shown to be established, according to the firewall logs. The Test.java that I run gives the following printout...


    Connection Established

    Commands are being given to the system...Please wait...

    show interfaces terse;echo "z4a3ce4f3317Z"



    I think that something is wrong with the class SshClient... Any ideas?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSH session with server

    Who wrote the code you are working with? Did they write any documentation about how to use it and what it should do?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SSH session with server

    Quote Originally Posted by Norm View Post
    Who wrote the code you are working with? Did they write any documentation about how to use it and what it should do?
    Hi Norm,

    The code is by James Carr. See link below

    SSH Over Java | James Carr

    There is some documentation and there are also other options, with different implementations. I will try them today.
    However, I would really like to know why this doesn't work since in the documentation it says that it should work for another server.
    Is it possible that the problem is created because I am trying to reach a firewall?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSH session with server

    I would really like to know why this doesn't work since in the documentation it says that it should work
    Ask the author.


    The code is not properly formatted for viewing. Nested statements should be indented 3-4 spaces to make the code easier to read. See post#2
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SSH session with server

    Hi Norm and All,

    After having some tests, I removed ";" from the code of send method below and put a \n, since the box I was trying to communicate could not "understand" this character as carriage return.

    out.write((command+";echo \"z4a3ce4f3317Z\"").getBytes());

    So the correct line is the following:

    out.write((command+"\necho \"z4a3ce4f3317Z\"").getBytes());




    Everything is working properly now.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: SSH session with server

    Thanks for the update.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Session Timeouut
    By srautpyaa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 19th, 2012, 08:20 AM
  2. How to connect and fire command to SSH server using Gyanmed SSH2 API.
    By Shanul in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 4th, 2012, 07:57 AM
  3. SSH and hostgator, connection exception
    By youknow12 in forum Java Networking
    Replies: 0
    Last Post: June 22nd, 2012, 09:52 PM
  4. What classes do I need to establish an ssh connection?
    By slashdevslashnull in forum Java Networking
    Replies: 2
    Last Post: June 30th, 2011, 10:41 PM
  5. [SOLVED] SSH
    By rachana in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 09:01 AM