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 170 171 172 173 174
|
package com.ssh;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import com.bd.bean.ElemIdentite;
import com.bd.bean.TableEnvironnement;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.utilitaires.StringOperation;
/**
* Classe ouvrant une connexion SSH
* pour un serveur distant
*/
public class SshClientTrait {
/**
* Objet de creation pour obtenir une
* connexion distante ssh
*/
private JSch jsch = null;
/**
* Session pour l'execution
*/
private Session session = null;
/**
* host de connexion
*/
private String host = "";
/**
* loggin de connexion
*/
private String loggin = "";
/**
* password de connexion
*/
private String passwd = "";
//****************************************************
// CONSTRUCTEURS
//****************************************************
/**
* Constructeur de classe
* Creation de la connection et d'une session pour la classe
* @param env (contient les parametres de connexion pour un ENV.
*/
public SshClientTrait( TableEnvironnement env ) {
this.host = env.get_adresseIP();
this.loggin = env.get_loggin();
this.passwd = env.get_passwd();
this.jsch = new JSch();
try {
this.session = jsch.getSession( this.loggin, this.host, 22 );
this.session.setPassword( this.passwd );
this.session.setConfig("StrictHostKeyChecking", "no");
this.session.connect();
} catch (JSchException e) {
e.printStackTrace();
}
}
//****************************************************
// METHODES
//****************************************************
/**
* Taille en octets sur les paths en parametre.
* Retourne 'error' si le fichier est absent
*/
public ArrayList<Integer> ShellTailleExec ( ArrayList<String> paths ) {
Channel channel = null;
ArrayList<Integer> tailles = new ArrayList<Integer>();
try {
for ( String s : paths ) {
channel = session.openChannel("exec");
String res = new String();
((ChannelExec)channel).setCommand( "ls -l " + s + " | awk '{ print $5 }'" );
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while( true ) {
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
res += new String(tmp, 0, i);
}
if(channel.isClosed()){ break; }
try{Thread.sleep(1000);}catch(Exception ee){ee.printStackTrace();}
}
if( channel.getExitStatus() == 0 ){
int id = -1;
NumberFormat numForm = NumberFormat.getInstance();
try {
id = numForm.parse(res).intValue();
} catch (ParseException e) {}
tailles.add( id );
}
else tailles.add(-1);
channel.disconnect();
}
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return tailles;
}
/**
* MD5 sur les paths en parametre.
* Retourne 'error' si le fichier est absent
*/
public ArrayList<String> ShellMD5Exec ( ArrayList<String> paths ) {
Channel channel = null;
ArrayList<String> crypts = new ArrayList<String>();
try {
for ( String s : paths ) {
channel = session.openChannel("exec");
String res = new String();
((ChannelExec)channel).setCommand("md5sum " + s);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while( true ) {
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
res += new String(tmp, 0, i);
}
if(channel.isClosed()){ break; }
try{Thread.sleep(1000);}catch(Exception ee){ee.printStackTrace();}
}
if( channel.getExitStatus() == 0 )
crypts.add(res.substring(0, 32).toUpperCase());
else crypts.add("error");
channel.disconnect();
}
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return crypts;
}
public void close() {
this.session.disconnect();
this.jsch = null;
}
} |
Partager