0

I had to run a script in putty via java. I used channel("Shell") but My code prints out the entire thing right from connecting to the session etc. where as i need oly the output of my command which i save sent through pritnstream.

My Code :

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();

    }

}

Output :

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

Code wise its fine. But what i need in particular is the output after executing the commander.

from the above output i want only the output that i have mentioned below ie the output of

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

Expected output:

        <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

Can anyone help me out how to achieve this useng Channel("Shell") cause channel("exec") is not working out for my commands.

T Kanagaraj
  • 245
  • 5
  • 20
  • A "shell" channel implements an interactive-style session. So you'd naturally expect to read everything the server would print during an interactive session, including things like the welcome message and command echo. An "exec" session would be better suited to what you're trying to do. You should stick with your [original question](http://stackoverflow.com/questions/27648974/not-able-to-run-putty-scrip-via-java-jsch) and get your exec code working. – Kenster Dec 27 '14 at 02:44
  • In Exec command Irel_Wrapper cannot be executed. Exec can run only putty commands like mv or cd find .| grep like that..when i tried with Irel wraper which is a script writtern, exec was not able to execute it. Is there any way to run scripts using exec ?? – T Kanagaraj Dec 27 '14 at 05:47

0 Answers0