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 4 of 4

Thread: ssh server throwing exception

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default ssh server throwing exception

    hi im writing an ssh server and client im using apache mina as the ssh deamon and jsch for the client
    but am getting a NumberFormatException on the server i think when transmitting the time from client to server but cant find the problem
    server code:
      public static void setupSftpServer(){
            SshServer sshd = SshServer.setUpDefaultServer();
            sshd.setPort(2228);
            sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
     
            List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
            userAuthFactories.add(new UserAuthNone.Factory());
            sshd.setUserAuthFactories(userAuthFactories);
     
            sshd.setCommandFactory(new ScpCommandFactory());
     
            List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
            namedFactoryList.add(new SftpSubsystem.Factory());
            sshd.setSubsystemFactories(namedFactoryList);
     
            try {
                sshd.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    the client code
        public static void tfc(String[] arg){//args==>file1 user@remotehost:file2
     
            if(arg.length!=2){
                System.err.println("usage: java ScpTo file1 user@remotehost:file2");
                System.exit(-1);
            }
     
            FileInputStream fis=null;
            try{
     
                String lfile=arg[0];
                //System.out.println(lfile);
                String user=arg[1].substring(0, arg[1].indexOf('@'));
                //System.out.println(user);
                arg[1]=arg[1].substring(arg[1].indexOf('@')+1);
                String host=arg[1].substring(0, arg[1].indexOf(':'));
                // System.out.println(host);
                String rfile=arg[1].substring(arg[1].indexOf(':')+1);
                // System.out.println(rfile);
     
                JSch jsch1=new JSch();
                Session session;
     
                session = jsch1.getSession(user, host, 22);
                System.out.println(user+"  "+host);
     
                // username and password will be given via UserInfo interface.
                UserInfo ui=new KnownHosts.MyUserInfo();
                session.setUserInfo(ui);
                //set up aes
    //session.setConfig("cipher.s2c", "aes128-cbc,3des-cbc,blowfish-cbc");
    //session.setConfig("cipher.c2s", "aes128-cbc,3des-cbc,blowfish-cbc");
    //session.setConfig("CheckCiphers", "aes128-cbc");
    //done setting aes
                System.out.println("aaa");
                session.connect();//error here
                System.out.println("bbb");
                boolean ptimestamp = true;
     
                // exec 'scp -t rfile' remotely
                String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
                Channel channel=session.openChannel("exec");
                ((ChannelExec)channel).setCommand(command);
     
                // get I/O streams for remote scp
                OutputStream out=channel.getOutputStream();
                InputStream in=channel.getInputStream();
     
                channel.connect();
     
                if(checkAck(in)!=0){
                    System.exit(0);
                }
     
                File _lfile = new File(lfile);
     
                if(ptimestamp){
                    command="T "+(_lfile.lastModified()/1000)+" 0";
                    // The access time should be sent here,
                    // but it is not accessible with JavaAPI ;-<
                    command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
                    out.write(command.getBytes()); out.flush();
                    if(checkAck(in)!=0){
                        System.exit(0);
                    }
                }
     
                // send "C0644 filesize filename", where filename should not include '/'
                long filesize=_lfile.length();
                command="C0644 "+filesize+" ";
     
                if(lfile.lastIndexOf('/')>0){
                    command+=lfile.substring(lfile.lastIndexOf('/')+1);
                }else{
                    command+=lfile;
                }
                command+="\n";
                out.write(command.getBytes()); out.flush();
                if(checkAck(in)!=0){
                    System.exit(0);
                }
     
                // send a content of lfile
                fis=new FileInputStream(lfile);
                byte[] buf=new byte[1024];
                while(true){
                    int len=fis.read(buf, 0, buf.length);
                    if(len<=0) break;
                    out.write(buf, 0, len); //out.flush();
                }
                fis.close();
                fis=null;
                // send '\0'
                buf[0]=0; out.write(buf, 0, 1); out.flush();
                if(checkAck(in)!=0){
                    System.exit(0);
                }
                out.close();
     
                channel.disconnect();
                session.disconnect();
     
                System.exit(0);
            }catch(JSchException ex1){
                ex1.printStackTrace();
                   try{if(fis!=null){
                    fis.close();
                }
                }catch(Exception ee){
                    System.out.println("ft error");
                }
            } catch (IOException ex) {
                java.util.logging.Logger.getLogger(ScpTo.class.getName()).log(Level.SEVERE, null, ex);
                   try{if(fis!=null){
                    fis.close();
                }
                }catch(Exception ee){
                    System.out.println("ft error");
                }
            }
     
     
        }//end main
     
        static int checkAck(InputStream in) throws IOException{
            int b=in.read();
            // b may be 0 for success,
            // 1 for error,
            // 2 for fatal error,
            // -1
            if(b==0) {
                return b;
            }
            if(b==-1) {
                return b;
            }
     
            if(b==1 || b==2){
                StringBuffer sb=new StringBuffer();
                int c;
                do {
                    c=in.read();
                    sb.append((char)c);
                }
                while(c!='\n');
                if(b==1){ // error
                    System.out.print(sb.toString());
                }
                if(b==2){ // fatal error
                    System.out.print(sb.toString());
                }
            }else if(b==0){
                System.out.println("file sent");
            }
            return b;
        }//end checkack
     
    }//end class
    the error from server (the client throws no errors)
    Exception in thread "ScpCommand: scp -p -t /home/user/k" java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Long.parseLong(Long.java:601)
    	at java.lang.Long.parseLong(Long.java:631)
    	at org.apache.sshd.common.scp.ScpHelper.parseTime(ScpHelper.java:420)
    	at org.apache.sshd.common.scp.ScpHelper.receive(ScpHelper.java:98)
    	at org.apache.sshd.server.command.ScpCommand.run(ScpCommand.java:140)
    	at java.lang.Thread.run(Thread.java:745)
    when i change port of client to 22 it sends it to my open ssh server and works fine but when i send it to apache mina i get this error anyone have any idea where its going wrong thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ssh server throwing exception

    java.lang.NumberFormatException: For input string: ""
    ...
    at org.apache.sshd.common.scp.ScpHelper.parseTime(Scp Helper.java:420)
    At line 420 in ScpHelper the code calls parseLong with an empty String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    83
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: ssh server throwing exception

    k but on in this part of code i am sending false data with T so why is it null on server?
    if(ptimestamp){
                    command="T "+(_lfile.lastModified()/1000)+" 0";
                    // The access time should be sent here,
                    // but it is not accessible with JavaAPI ;-<
                    command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
                    out.write(command.getBytes()); out.flush();
                    if(checkAck(in)!=0){
                        System.exit(0);
                    }
                }
    here is switch statement on server side it doesint seems possible to try catch and input false data there
     long[] time = null;
            for (;;)
            {
                String line;
                boolean isDir = false;
                int c = readAck(true);
                switch (c)
                {
                    case -1:
                        return;
                    case 'D':
                        isDir = true;
                    case 'C':
                        line = ((char) c) + readLine();
                        log.debug("Received header: " + line);
                        break;
                    case 'T':
                        line = ((char) c) + readLine();
                        log.debug("Received header: " + line);
                        time = parseTime(line);
                        ack();
                        continue;
                    case 'E':
                        line = ((char) c) + readLine();
                        log.debug("Received header: " + line);
                        ack();
                        return;
                    default:
                        //a real ack that has been acted upon already
                        continue;
                }
    any ideas apart from edit source of librarys as i would probobly break em
    also why does it work with open ssh

    --- Update ---

    k have working should be no space after Tswitch and time should be seperated with spaces not : well that only took about 10 hours lol so in case others have same problem
    Calendar cal = Calendar.getInstance();
                cal.getTime();
                SimpleDateFormat sdf = new SimpleDateFormat("HH mm ss");
                String time= sdf.format(cal.getTime());
     
                if(ptimestamp){
                    command="T"+time;//(_lfile.lastModified()/1000)+" 0";
                    command+=(" "+(_lfile.lastModified()/1000)+" 0\n");               
                    out.write(command.getBytes());              
                    out.flush();
                    if(checkAck(in)!=0){
                        System.exit(0);
                    }
                }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ssh server throwing exception

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

Similar Threads

  1. how to create ssh server
    By bean in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2017, 01:55 AM
  2. SSH session with server
    By Paris in forum Java Networking
    Replies: 11
    Last Post: January 21st, 2013, 06:35 AM
  3. throwing Exception
    By Syahdeini in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2012, 06:05 PM
  4. 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
  5. SSH and hostgator, connection exception
    By youknow12 in forum Java Networking
    Replies: 0
    Last Post: June 22nd, 2012, 09:52 PM