Bash Unix | ssh退出不同的用户配置文件

时间:2013-11-21 18:02:10

标签: java linux bash unix ssh

我在JAVA工作,试图成功实现com.sshtools.j2ssh.session.SessionChannelClient。我发现我的代码非常适合那些在/ bin / bash等配置文件中没有特殊命令的人。像我一样,我的个人资料默认是bash,效果很好。但是,用户将/ bin / bash添加到他们的个人资料中的本地ksh无法使用我的代码。

这是我代码的核心......

    try {
        // Build our connection, and connect
        sessionChannelClient = new SessionChannelClient();
        sshClient = new SshClient();
        sshClient.connect(server, new IgnoreHostKeyVerification());

        // build authentication, and authenticate
        PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
        auth.setUsername(username);
        auth.setPassword(password);
        int authResult = sshClient.authenticate(auth);
        System.out.println("authResult:" +authResult);

        //start working with the session
        sessionChannelClient = sshClient.openSessionChannel();
        SessionOutputReader sor = new SessionOutputReader(sessionChannelClient);
        boolean terminalGood = sessionChannelClient.requestPseudoTerminal("gogrid", 80, 24, 0, 0, "");
        if (!terminalGood){System.out.println("no terminal created..."); return 99;}

        if (sessionChannelClient.startShell()) {

            //build an in/out so we can see the text to and from unix
            ChannelInputStream channelInputStream = sessionChannelClient.getInputStream();
            ChannelOutputStream channelOutputStream = sessionChannelClient.getOutputStream();
            //System.out.println(in);

            // RUN THE COMMAND
            System.out.println("Sending command to UNIX.....");
            channelOutputStream.write(command.getBytes());
            Thread.sleep(1000 * 5);//unix needs time to run the commands

            System.out.println("ScriptName | " + script);
            System.out.println("providePassword flag|" + providePassword);
            if( script.equals("propSync.shl")|| script.equals("nonsrcSync.shl")){providePassword = false;}

            //The java app called by the script needs a password
            if (providePassword) {
                channelOutputStream.write(tempPassword.getBytes());
                Thread.sleep(1000 * 3);//unix needs time to run the commands
            }
            else{ System.out.println("providePassword not required it seems for -->" + script); }

            // Close the channel
            String exit = "exit\n";
            while(sessionChannelClient.getState().getValue() != ChannelState.CHANNEL_CLOSED){
                System.out.println("lets try to exit the sessionChannelClient...." + sessionChannelClient.getState().getValue());
                channelOutputStream.write(exit.getBytes()); // without this, it wont log out correctly
                Thread.sleep(1000 * 5);//unix needs time to run the commands
            }
            exitCode = sessionChannelClient.getExitCode();
            System.out.println("just outside of while loop SessionState:" + sessionChannelClient.getState().getValue());
            System.out.println("just outside of while loop EXITCODE:" + exitCode);
            sessionChannelClient.close();
        } 
        else { System.out.println("the shell refused to start"); return 99;}
    } 
    catch (Exception e) { printOUtMessages(e, "Exception", script, server, command); return 99;} 
    finally { sshClient.disconnect(); }

    //Wrap up and return back
    calculateTime(startTime, exitCode, baseMessage);
    return exitCode; // non-zero means failure

0 个答案:

没有答案