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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.util.Scanner;
import javax.swing.JTextArea;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class ClientSsh {
public static Session oSession = null;
JSch jsch ;
String sCommandeRecupSession ;
String FichierALire ;
Scanner LectureClavier ;
JTextArea textarea ;
public ClientSsh(JTextArea I_jtarea)
{
textarea = I_jtarea ;
}
/*
public static void main(String[] arg) {
//String sCommandeRecupSession = ". /app/user/.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/user/.profile;\n sqlplus / as sysdba @test2.sql\n ";
try {
ClientSsh test = new ClientSsh() ;
//test.Cnx_Ssh("user","hostname","password") ;
test.Lecture_Fic_Cnx("/cadnat/ctige/script/java/outils/ssh/cmd.txt") ;
} catch (Exception e) {
System.out.println(e);
}
} */
public int executeCommand(String command) {
int exitCode = -1;
ChannelExec channel = null;
BufferedReader stdoutReader = null;
BufferedReader stderrReader = null;
String line ;
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) {
while ((line = stdoutReader.readLine()) != null) {
//System.out.println(stdoutReader.readLine());
//System.out.println(line) ;
textarea.append(line+"\n") ;
textarea.repaint() ;
}
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;
}
//
public void Lecture_Fic_Cnx(String ficparamcnx)
{
String ligne;
String chaine="" ;
try {
InputStream ips=new FileInputStream(ficparamcnx);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
LectureClavier = new Scanner(System.in);
sCommandeRecupSession=". /app/user/.profile;" ;
while ((ligne=br.readLine())!=null){
if((ligne.substring(0,1)).equals("#")) {
//System.out.println("deb "+sCommandeRecupSession+" fin");
Cnx_Ssh("user","hostname","password",sCommandeRecupSession) ;
System.out.println("\n"+"Voulez vous continuer:");
String reponse = LectureClavier.nextLine();
if (reponse.equals("n") ) {
System.exit(0);
}
//System.out.println("deb "+sCommandeRecupSession+" fin");
sCommandeRecupSession=". /app/user/.profile;" ;
ligne="" ;
}
sCommandeRecupSession=sCommandeRecupSession+"\n"+ligne ;
//System.out.println("deb "+sCommandeRecupSession+" fin");
//System.out.println(ligne);
}
Cnx_Ssh("user","hostname","password",sCommandeRecupSession) ;
//System.out.println("deb "+sCommandeRecupSession+" fin");
br.close();
}
catch(Exception er) {
System.out.println("Pb execution ") ;
er.printStackTrace() ;
System.out.println(er.getMessage()) ;
}
}
//
//
public void Cnx_Ssh(String I_user,String I_hostname,String I_password,String I_cmdapasser)
{
//sCommandeRecupSession = ". /app/user/.profile;sqlplus user/password<<EOF \n select * from cat ;\n exit ;\n EOF \n";
try {
jsch = new JSch();
//oSession = jsch.getSession("myuser", "localhost", 22);
//oSession.setPassword("pwd");
oSession = jsch.getSession(I_user, I_hostname, 22);
oSession.setPassword(I_password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
oSession.setConfig(config);
oSession.connect();
executeCommand(I_cmdapasser);
oSession.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}
//
} |
Partager