Package com.jcraft.jsch

Examples of com.jcraft.jsch.Channel


                               
                                session.setConfig("StrictHostKeyChecking", "no") ;

                                session.connect();
       
                                final Channel channel  = session.openChannel(SECURE_CHANNEL);
                                channel.connect();
       
                                m_oSftpChannel = (ChannelSftp) channel;
       
                                if (!session.isConnected())
                                        throw new RemoteFileSystemException("Can't connect to FTP server");
View Full Code Here


public class SSHFileSend {

    public static boolean sendFile(Session session, String sourceFilename, String destFilename) throws IOException, JSchException {

        String command = "scp -p -t '" + destFilename + "'";
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);

        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();

        channel.connect();

        if (checkAck(in) != 0) {
            return false;
        }

        //send "C0644 filesize filename" where filename doesn't contain a /
        long filesize = (new File(sourceFilename)).length();
        command = "C0644 " + filesize + " ";
        if (sourceFilename.lastIndexOf('/') > 0) {
            command += sourceFilename.substring(sourceFilename.lastIndexOf('/') + 1);
        } else if (sourceFilename.lastIndexOf('\\') > 0) {
            command += sourceFilename.substring(sourceFilename.lastIndexOf('\\') + 1);
        } else {
            command += sourceFilename;
        }
        command += "\n";

        out.write(command.getBytes());
        out.flush();

        if (checkAck(in) != 0) {
            throw new IOException("Error while trying to write " + destFilename + " , " + getReason(in));
        }

        //send the contents of the source file
        FileInputStream fis = new FileInputStream(sourceFilename);
        byte[] buf = new byte[1024];
        while (true) {
            int len = fis.read(buf, 0, buf.length);

            if (len <= 0) {
                break;
            }

            out.write(buf, 0, len);
        }

        fis.close();

        //send '\0' to end it
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();

        if (checkAck(in) != 0) {
            throw new IOException("Error while trying to write " + destFilename + " , " + getReason(in));
        }

        out.close();

        channel.disconnect();

        return true;
    }
View Full Code Here

        logger.debug("A iniciar sess�o...");
        session.connect();
       
        // exec 'scp -f rfile' remotely
        String command = "scp -f "+remoteFile;
        Channel channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
       
        // get I/O streams for remote scp
        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();
       
        logger.debug("A abrir canal...");
        channel.connect();
        byte[] buf = new byte[1024];
       
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
View Full Code Here

            session.setConfig(hash);
            session.setPort(port);
            session.setPassword(password);
            session.connect();

            Channel channel = session.openChannel(CHANNEL_SFTP);
            channel.connect();

            channelSftp = (ChannelSftp) channel;
            setHome(channelSftp.pwd());
        }
        catch (JSchException e)
View Full Code Here

            session = jsch.getSession(user, host);
            session.setConfig(hash);
            session.setPort(port);
            session.connect();

            Channel channel = session.openChannel(CHANNEL_SFTP);
            channel.connect();

            channelSftp = (ChannelSftp) channel;
            setHome(channelSftp.pwd());
        }
        catch (JSchException e)
View Full Code Here

      UserInfo ui = new SshUser();
      session.setUserInfo(ui);
      session.connect();

      String command = "scp -p -t  " + targetFileName;
      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) {
        throw new RuntimeException("Failed to scp key file");
      }

      // send "C0644 filesize filename", where filename should not include
      // '/'
      long filesize = (new File(sourcePath)).length();
      command = "C0644 " + filesize + " ";
      if (sourcePath.lastIndexOf('/') > 0) {
        command += sourcePath.substring(sourcePath.lastIndexOf('/') + 1);
      } else {
        command += sourcePath;
      }
      command += "\n";
      out.write(command.getBytes());
      out.flush();
      if (checkAck(in) != 0) {
        // this actually will never happend since checkAck already
        // throws an exception now.
        throw new RuntimeException("Fatal Error scp key file");
      }
      // send a content of lfile
      FileInputStream fis = new FileInputStream(sourcePath);
      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) {
        throw new RuntimeException("Failed to scp  file");
      }
      out.close();
      channel.disconnect();
      session.disconnect();
    } catch (JSchException e) {
      throw new IOException("Unable to copy key file to host: ", e);
    }
  }
View Full Code Here

      Session session = jsch.getSession("root", dnsName, 22);
      UserInfo ui = new SshUser();
      session.setUserInfo(ui);
      session.connect();

      Channel channel = session.openChannel("exec");

      ((ChannelExec) channel).setCommand(command);
      ((ChannelExec) channel).setErrStream(System.out);
      InputStream in = channel.getInputStream();
      channel.connect();
      byte[] tmp = new byte[1024];
      while (true) {
        while (in.available() > 0) {
          int i = in.read(tmp, 0, 1024);
          if (i < 0)
            break;
          // we acully dont want to show the output
          // System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
          // System.out.println("exit-status: " +
          // channel.getExitStatus());
          if (channel.getExitStatus() != 0) {
            return false;
          }
          break;
        }
        try {
          Thread.sleep(1000);
        } catch (InterruptedException ee) {
          Thread.currentThread().interrupt();
        }
      }
      channel.disconnect();
      session.disconnect();
    } catch (JSchException e) {
      return false;
      // throw new IOException("Unable to ssh into master", e);
    }
View Full Code Here

    Session session =  jsch.getSession("root", "192.168.1.1", 22);
    session.setUserInfo(ui);
    session.connect();
    String command = "iptables -L INPUT -nv --line-numbers";
    try {
        Channel channel=session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec)channel).setErrStream(System.err);

        BufferedReader bReader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
        channel.connect();
       
       
        int i = 0;
        while(true){
          while(bReader.ready()){
            i++; 
            StringBuffer sb = new StringBuffer(bReader.readLine());
            if (i>2){
              LinkBean link = new LinkBean();
              link.setLineNumber(sb.substring(0, 2));
              String tmp = sb.substring(3, 10).trim();
              link.setPackets(Integer.parseInt(tmp));
              link.setBytes(Integer.parseInt(sb.substring(11, 16).trim()));
              link.setTarget(sb.substring(18,28));
              System.out.println(link);
            }
            System.out.println(sb);
          }
          if(channel.isClosed()){
            System.out.println("exit-status: "+channel.getExitStatus());
            break;
          }
          try{Thread.sleep(1000);}catch(Exception ee){}
        }
       
       
        channel.disconnect();
        session.disconnect();
      }
      catch(Exception e){
        System.out.println(e);
      }
View Full Code Here

    InputStream in = null;
    try {
      Session session = pContext.createSession();
      session.connect();

      Channel channel = sendCommand(pRemoteFile, session);

     
       out = channel.getOutputStream();
       in = channel.getInputStream();

      channel.connect();

      if (checkAck(in) != 0) {
        System.exit(0);
      }

      sendFileSize(pLocalFile, out, in);

      fis = sendContent(pLocalFile, out, in);
      out.close();

      channel.disconnect();
      session.disconnect();
    } finally {
      IOUtils.closeQuietly(fis);
      IOUtils.closeQuietly(out);
      IOUtils.closeQuietly(in);
View Full Code Here

  private static Channel sendCommand(String pRemoteFile, Session session)
      throws JSchException {
    // exec 'scp -t rfile' remotely
    String command = "scp " + " -t " + pRemoteFile;
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);
    return channel;
  }
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.Channel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.