如何遍历从Channel(Shell)PrintStream获得的输出

时间:2014-12-26 07:14:05

标签: java shell jsch

我必须通过java在putty中运行一个脚本。我使用了频道(“Shell”),但我的代码从连接到会话等打印出整个事物。 在哪里我需要oly我的命令的输出,我保存通过pritnstream发送。

我的代码:

jsch = new JSch();
    session = jsch.getSession(username, host, 22);
    session.setPassword(pass);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    System.out.println("Attempting to Connect..!");
    session.setConfig("PreferredAuthentications",
            "publickey,keyboard-interactive,password");
    session.connect();
    Channel channel = session.openChannel("shell");

        ArrayList<String> array=new ArrayList<String>();

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

        commander.println("cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel");    
        commander.println("irel_wrapper");
        commander.close();
        InputStream outputstream_from_the_channel = channel.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
        String line;

        while ((line = br.readLine()) != null){


            System.out.println(line);

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

        session.disconnect();

    }

}

输出:

|-----------------------------------------------------------------|
| This system is for the use of authorized users only.            |
| Individuals using this computer system without authority, or in |
| excess of their authority, are subject to having all of their   |
| activities on this system monitored and recorded by system      |
| personnel.                                                      |
|                                                                 |
| In the course of monitoring individuals improperly using this   |
| system, or in the course of system maintenance, the activities  |
| of authorized users may also be monitored.                      |
|                                                                 |
| Anyone using this system expressly consents to such monitoring  |
| and is advised that if such monitoring reveals possible         |
| evidence of criminal activity, system personnel may provide the |
| evidence of such monitoring to law enforcement officials.       |
|-----------------------------------------------------------------|

+ export PATH=:/bin:/usr/ucb:/usr/ccs/bin:/usr/sbin:.
+ . /hta1/home/pinDap75a/.batch_profile
+ pin_bindir=/pin/d03/opt/bin
+ ifw_bindir=/pre/d03/opt/bin
+ + /usr/ucb/whoami
user=pinDap75a
+ + hostname
host=brmmlapp1
+ [[ -d /pin/d03/opt/bin ]]
+ [[ -d /pre/d03/opt/bin ]]
+ + /pre/d03/opt/bin/GetEntry.pl pinDap75a brmmlapp1 inf_bindir
cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel

irel_wrapper

pin_bindir=/pin/d03/opt/bin
+ + /pre/d03/opt/bin/GetEntry.pl pinDap75a brmmlapp1 ifw_bindir
ifw_bindir=/pre/d03/opt/bin
+ bindir=/pin/d03/opt/bin
+ [[ -d /pin/d03/opt/bin ]]
+ [[ ! -d /pin/d03/opt/bin ]]
+ [[ -d /pre/d03/opt/bin ]]
+ server=P
+ bindir=/pre/d03/opt/bin
+ export BP_server_type=P
+ bp_vars_file=/tmp/.bp_vars.17189
+ [[ ! -a /tmp/.bp_vars.17189 ]]
+ /pre/d03/opt/bin/GetEntry.pl pinDap75a brmmlapp1
+ awk -F= {
                printf "export ";
                if(length($2)==0)
                {
                                print "BP_"$1"=\"\"";
                }
                else
                {
                        print "BP_"$1"=\""$2"\"";
                }
        }
+ sed s/ = /=/
+ 1> /tmp/.bp_vars.17189
+ . /tmp/.bp_vars.17189
+ export BP_dm_aethos_n_be=4
+ export BP_ssbo_dbuser=SSBODAP75A
+ export BP_ba_reports_deltabill_email=dontSend
+ export BP_ebill_big_xmx=2048M
+ export BP_trial_num_main_thrds=3
+ export BP_ra_reporting=RADAP05
+ export BP_dm_aethos_max_per_fe=16
+ export BP_ftp_cobra=preDf053
+ export BP_master_env=
+ export BP_wbi_dbuser=pinDap75a
+ export BP_ifw_rtp=43751
+ export BP_ebill_num_main_thrds=3
+ export BP_dm_vf_partner_max_per_fe=16
+ export BP_ifw_var1=/pre/d02/pinDap75a/var
+ export BP_inf_encrypted_dbpass=&aes|09|0D5E11BFDD97D2769D9B0DBFBD1BBF7EFFFD8C7AB0280DC284F7BDC9F342F00630
+ export BP_nz_num_main_thrds=3
+ export BP_cm_proxy_n_be=2
+ export BP_cm_proxy=44012
+ export BP_csdi_xmx=1024M
+ export BP_aufj_jvm_count=3
+ export BP_oraweb_dbuser=pinDap75a
+ export BP_random_features=on
+ export BP_wap_kill_agent_port=30308
+ export BP_pin_deferred_act_fetch_size=5000
+ export BP_dm_ifw_sync_shmsize=33554432
+ export BP_web_online_host_list=
+ export BP_dm=11752
+ export BP_billrev_big_xmx=2048M
+ export BP_trial_xms=1024M
+ export BP_dm_trans_be_max=dm dm_n_be 4
+ export BP_inf_delta_db=
+ export BP_pin_mta_fetch_size_datamig_adjust_alloc=50000
+ export BP_aufj_xms=1024M
+ export BP_ba_reports_postbillerr_email=dontSend
+ export BP_pin_collect_per_batch=100
+ export BP_ifw_eventhandler1=41751
+ export BP_ebill_big_xms=2048M
+ export BP_slave_env_list=pinDap75b-brmmlapp2;
+ export BP_dm_n_fe=dm dm_n_fe 2
+ export BP_aya_xms=1024M

//Lot more got triggered but i cut them down
....
brmmlapp1[/hta1/home/pinDap75a] >
brmmlapp1[/hta1/home/pinDap75a] >cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel
+ cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel
brmmlapp1[/pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel] >brmmlapp1[/pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel] >
brmmlapp1[/pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel] >irel _wrapper
+ irel_wrapper
Usage: irel_wrapper <template_name> [-debug]
    <template_name> - Name of the template, to be part of the
                      command line arguments for each batch run.
                      Current options are:
                      P1_VLIVE_ZERO, P1_TXT_ZERO, P1_GSMV_ZERO, P1_GSMVT_ZERO,
                      P1_GPRS_ZERO, P1_OGPRS_ZERO, P1_PXT_ZERO,
                      P1_VLIVE, P1_PXT, P1_GSMV, P1_GSMVT, P1_WAP,
                      P1_TXT, P1_GPRS, P1_OGPRS,
                      P2_VLIVE, P2_PXT, P2_GSMV, P2_GSMVT, P2_WAP,
                      P2_TXT, P2_GPRS,
                      P3A_GSMV, P3B_GSMV, P3C_GSMV, P3D_GSMV, P3E_GSMV,
                      P3A_GPRS, P3B_GPRS, P3C_GPRS, P3D_GPRS, P3E_GPRS,
                      P3A_TXT, P3B_TXT, P3C_TXT, P3D_TXT, P3E_TXT,
                      P3A_PXT, P3B_PXT, P3C_PXT, P3D_PXT, P3E_PXT,
                      P3_GSMVT, P3_GSMF, P3_GSMD, P3_WAP, P3_VLIVE,
                      SUSPENSE, SUSPENSE_UPDATE

代码明智的罚款。但我特别需要的是执行指挥官后的输出。

从上面的输出我只想要我在下面提到的输出,即

的输出
**commander.println("cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel");    
        commander.println("irel_wrapper");**

预期产出:

        <template_name> - Name of the template, to be part of the
                          command line arguments for each batch run.
                          Current options are:
                          P1_VLIVE_ZERO, P1_TXT_ZERO, P1_GSMV_ZERO, P1_GSMVT_ZERO,
                          P1_GPRS_ZERO, P1_OGPRS_ZERO, P1_PXT_ZERO,
                          P1_VLIVE, P1_PXT, P1_GSMV, P1_GSMVT, P1_WAP,
                          P1_TXT, P1_GPRS, P1_OGPRS,
                          P2_VLIVE, P2_PXT, P2_GSMV, P2_GSMVT, P2_WAP,
                          P2_TXT, P2_GPRS,
                          P3A_GSMV, P3B_GSMV, P3C_GSMV, P3D_GSMV, P3E_GSMV,
                          P3A_GPRS, P3B_GPRS, P3C_GPRS, P3D_GPRS, P3E_GPRS,
                          P3A_TXT, P3B_TXT, P3C_TXT, P3D_TXT, P3E_TXT,
                          P3A_PXT, P3B_PXT, P3C_PXT, P3D_PXT, P3E_PXT,
                          P3_GSMVT, P3_GSMF, P3_GSMD, P3_WAP, P3_VLIVE,
                          SUSPENSE, SUSPENSE_UPDATE

任何人都可以帮我解决如何实现这个使用频道(“Shell”)导致频道(“exec”)无法解决我的命令。

0 个答案:

没有答案