我如何在JAVA JSch的会话中同时使用exec和shell

时间:2014-10-03 08:01:17

标签: java ssh jsch

我有同时使用exec和shell的问题。 我有两个代码和平,他们分别工作得很好。

首先:

    FileInputStream fis=null;
    JSch jsch=new JSch();
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");

    Session session=jsch.getSession("user", "server", 22);
    session.setPassword("password");
    session.setConfig(config);
    session.connect();

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

    OutputStream inputstream_for_the_channel = channel.getOutputStream();
    PrintStream commander = new PrintStream(inputstream_for_the_channel, true);

    channel.setOutputStream(System.out, true);

    channel.connect();

    commander.println("1");
    commander.println("cd /u02/logs");
    commander.println("touch 1.txt");

    boolean ptimestamp=false;

    commander.close();

    out.close();

    session.disconnect();


    do {
        Thread.sleep(1000);
    } while(!channel.isEOF());

    session.disconnect();

第二

    FileInputStream fis=null;
    try{

        //String lfile=arg[0];
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");


        JSch jsch=new JSch();

        Session session=jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();

        boolean ptimestamp = false;

        // exec 'scp -t rfile' remotely
        String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
        Channel channel=session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        System.out.println(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();
        setFilesize(filesize);

        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();
        return "success";
    }
    catch(Exception e){
        System.out.println(e);
        try{if(fis!=null)fis.close();}catch(Exception ee){}
        setErrMes(e.toString());
        return "fail";
    }
}

第一个和平意图在连接后选择1(/ etc / profile中的自动登录脚本): 登录为:用户 user @ server的密码: 上次登录时间:2014年10月2日星期四15:59:08来自Machine1 请输入括号中列出的号码,然后按Enter键切换到所选用户: 管理员(1)|显影剂(2):1

如果您输入1 - 您将拥有系统中的管理员权限,您可以在任何地方创建文件。

旨在通过SSH SCP在远程系统上的特定位置发送文件的第二个代码和平。

请知道,克服这个问题的人帮助我解决它或提供一些可以帮助我的建议。 如果我使用exec,我无法将文件复制到远程,因为我没有权利在某个目录中写...

0 个答案:

没有答案