jsch ligne en sortie tronquée
Bonjour,
Je fais du ssh via le code ci-dessous, tout fonctionne à merveille ... sauf l'affichage en sortie(InputStream stdout) qui tronque systématiquement le premier caractère.Par exemple si je passe la commande sous unix "ls -al"
j'ai le résultat suivant :
rw-r----- 1 root dba 1785 27 jan 10:56 UserInfo.java
rw-r----- 1 root dba 12514 27 jan 10:56 Util.java
à la place de :
drw-r----- 1 root dba 1785 27 jan 10:56 UserInfo.java
-rw-r----- 1 root dba 12514 27 jan 10:56 Util.java
J'ai regardé le code source de Channel.java et il est indiqué au niveau des "stream" : 32*1024 // this value should be customizable.
faut-il modifier ce paramètre.
Merci pour votre aide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class TestSSH {
public static Session oSession = null;
public static void main(String[] arg) {
//String sCommandeRecupSession = ". /app/o3p80/.profile;sqlplus system/manager@demoweb \n set heading on pages 32767 lin 32767 trims ON; \n select count(*) as SESSION_NBR from v$session s; \n select s.sid ||'¤'|| s.serial# ||'¤'|| s.osuser ||'¤'|| s.program ||'¤'|| s.status as SESSION_VALEUR from v$session s; \n exit; \n exit; \n";
//String sCommandeRecupSession = ". /app/o3p80/.profile;\n sqlplus / as sysdba @test2.sql\n ";
String sCommandeRecupSession = ". /app/o3p80/.profile;ls -al\n";
try {
JSch jsch = new JSch();
oSession = jsch.getSession("user", "hostname", 22);
oSession.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
oSession.setConfig(config);
oSession.connect();
executeCommand(sCommandeRecupSession);
oSession.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}
public static int executeCommand(String command) {
int exitCode = -1;
ChannelExec channel = null;
BufferedReader stdoutReader = null;
BufferedReader stderrReader = null;
try {
channel = (ChannelExec) oSession.openChannel("exec");
channel.setCommand(command + "\n");
InputStream stdout = channel.getInputStream();
InputStream stderr = channel.getErrStream();
channel.connect();
stdoutReader = new BufferedReader(new InputStreamReader(stdout));
stderrReader = new BufferedReader(new InputStreamReader(stderr));
while (stdoutReader.read() != -1)
System.out.println(stdoutReader.readLine());
exitCode = channel.getExitStatus();
return exitCode;
} catch (IOException e) {
System.out.println("Cannot execute remote command: " + command
+ " : " + e.getMessage());
} catch (Exception e) {
System.out.println("Cannot execute remote command: " + command
+ " : " + e.getMessage());
} finally {
try {
stdoutReader.close();
stderrReader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (channel != null) {
channel.disconnect();
}
}
return exitCode;
}
} |