JSch - 执行shell并与执行交互

时间:2017-08-03 14:54:28

标签: shell jsch

我需要连接一台机器,执行.sh并在执行过程中给出一些参数。但是,在执行.sh并发送第一个参数后,它们会给我一些错误,我不知道自己需要做什么。我如何与.sh执行交互?帮助!

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class shUnixShell {

    public static void main(String[] args) throws Exception {
        JSch jsch = new JSch();
        String user = "aaaaa"; 
        String host = "zz.xx.zz.xx"; 
        String passwd = "bbbbb"; 
        int port = 22;
        Session session = jsch.getSession(user, host, port);
        session.setPassword(passwd);

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

        session.connect();

        Channel channel = session.openChannel("shell");
        OutputStream ops = channel.getOutputStream();
        PrintStream ps = new PrintStream(ops, true);

        channel.connect();
        InputStream input = channel.getInputStream();

        ps.println("ls -ltr");
        Thread.sleep(1000);
        ps.println("cd /opt/ngin/core/ddesb/ARP/V72");
        Thread.sleep(300);
        ps.println("./Offline_CON.sh");
        Thread.sleep(10000);
        ps.println("sp");
        Thread.sleep(6000);
        ps.println("1");
        Thread.sleep(6000);
        ps.println("11971054860");
        Thread.sleep(6000);
        ps.println("n");
        Thread.sleep(6000);
        ps.println("11971054860");
        Thread.sleep(6000);
        ps.println("1");
        Thread.sleep(6000);
        ps.println("60");
        Thread.sleep(6000);
        ps.println("11");
        Thread.sleep(6000);
        ps.println("010");
        Thread.sleep(6000);
        ps.println("15");
        Thread.sleep(6000);

        printResult(input, channel);

        channel.disconnect();
        session.disconnect();
    }

    /**
     * @param input
     * @param channel
     */
    private static void printResult(InputStream input, Channel channel) throws Exception {
        int SIZE = 1024;
        byte[] tmp = new byte[SIZE];
        while (true) {
            while (input.available() > 0) {
                int i = input.read(tmp, 0, SIZE);
                if (i < 0)
                    break;
                System.out.print(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(300);
            } catch (Exception ee) {
            }
        }
    }
}

Eclipse控制台:

Last login: Thu Aug  3 11:32:20 2017 from 10.125.88.173

ls -ltr


 Active Here

[11:37:10] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ ~]$ ls -ltr
[00mtotal 68
-rwxrwxrwx 1    2760 Mar 30  2016 [01;32mprocessa_consumo_dados_v3.sh.old[00m
-rw-r--r-- 1     174 Abr  1  2016 [00mdesktop.ini[00m
-rw-rw-r-- 1       6 Jun  3  2016 [00mteste.txt[00m
-rw-rw-r-- 1       0 Set  5  2016 [00mLCP[00m
-rw-rw-r-- 1    6616 Set 13  2016 [00mwget-log[00m
-rw-rw-r-- 1    6616 Set 13  2016 [00mwget-log.1[00m
-rw-rw-r-- 1    6616 Set 13  2016 [00mwget-log.2[00m
-rw-rw-r-- 1       0 Set 14  2016 [00ma.out[00m
-rwxrwxrwx 1    3073 Fev  9 14:51 [01;32mprocessa_consumo_dados_v4.sh[00m
-rwxrwxrwx 1    3130 Jul 19 15:34 [01;32mprocessa_raton.sh[00m
-rwxrwxrwx 1   17392 Jul 20 11:21 [01;32mconsumo.sh[00m
drwxrwxrwx 2    4096 Jul 24 15:59 [34;42mft[00m
[m[11:37:10] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ ~]$ 
[11:37:10] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ ~]$ cd /opt/ngin/core/ddesb/ARP/V7 
2
[11:37:11] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ [K
[11:37:11] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ ./Offline_CON.sh
[H[J
--------------------------------
NGIN 14.6*
NOTA - todos os valores indicados por (*), representam o valor
       default de cada funcao, tornando facultativo o input
       de dados

--------------------------------
Informe a regional (mg, base, con, ne, prsc, rjes, rs, sp*): sp

SP
sp
Digite a quantidade de chamadas a serem processadas (1)*: ./Offline_CON.sh: line 75: printf: sp: invalid number
Digite o Numero de A: ./Offline_CON.sh: line 81: [: sp: integer expression expected
./Offline_CON.sh: line 207: printf: sp: invalid number
./Offline_CON.sh: line 209: printf: sp: invalid number
0CONT_SP_20170803113711_0000.dat FCDR_SP.DAT 300
9CONT_AP_20170803113711_0000.dat         000000000##################################
# Arquivo processado com sucesso #
##################################
CONT_SP_20170803113711_sp.dat
##################
Processando chamada de VOZ no SMPC (CON_SP)
PL/SQL procedure successfully completed.
Final do processamento!
##################
# Arquivo de log #
##################
03 Aug 2017 11:37:25,604: Service MED_CON_IMPORT_SP [33841]  Started File [CONT_SP_20170803113711_sp.dat] import
03 Aug 2017 11:37:25,604: [Service FileToTable] Reading File: CONT_SP_20170803113711_sp.dat
03 Aug 2017 11:37:25,915: Total Records: 0
03 Aug 2017 11:37:25,915: Total Records OK: 0
03 Aug 2017 11:37:25,915: Total Records with Error: 0
03 Aug 2017 11:37:25,915: Total Errors:    0
03 Aug 2017 11:37:25,916: Service MED_CON_IMPORT_SP [33841] [324ms]
########################
#Arquivo de Resultados #
########################
1

11971054860

n

11971054860

1

60

11

010

15

SP
CONT_SP_20170803113711_sp.res
cat: /opt/ngin/interfaces/CON/SP/tratados/CONT_SP_20170803113711_sp.res: No such file or directory
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 1
-bash: 1: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 11971054860
-bash: 11971054860: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ n
-bash: n: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 11971054860
-bash: 11971054860: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 1
-bash: 1: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 60
-bash: 60: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 11
-bash: 11: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 010
-bash: 010: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 15
-bash: 15: command not found
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 
[11:38:17] [46;37;1m EVL [44;37;1m CAMPINAS [0m  @ V72]$ 

Stackoverflow要求提供更多信息,但我不知道自己需要做什么。

1 个答案:

答案 0 :(得分:0)

已解决

只需改变:

ps.println("ls -ltr");
ps.println("./Hue.sh");

有关:

ps.append("ls -ltr\n");
ps.append("./Hue.sh\n");