use of com.jcraft.jsch.Channel in project ASAP by salmant.
the class Terminate_a_Container method remove_from_haproxy.
// ///////////////////////////////////////////////////////////
public void remove_from_haproxy() {
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "***************";
// ////////////////////////////// add new server
try {
String command = "sed " + "\'/" + " server " + i_str + "-www " + new_IP + ":5000 check/d" + "\' /etc/haproxy/haproxy.cfg" + " > /etc/haproxy/test.cfg ; mv /etc/haproxy/test.cfg /etc/haproxy/haproxy.cfg";
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
;
session.setPassword(haproy_host_password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
// System.out.println(line);
}
bufferedReader.close();
inputReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
channel.disconnect();
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("error in remove_from_haproxy() first section");
}
// ////////////////////////////// reload HAProxy
try {
String command = "sh /haproxy.sh";
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
;
session.setPassword(haproy_host_password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
// System.out.println(line);
}
bufferedReader.close();
inputReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
channel.disconnect();
session.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
// to be deleted !!!!
System.out.println("error in remove_from_haproxy() second section");
}
}
use of com.jcraft.jsch.Channel in project ASAP by salmant.
the class HPA method How_many_existing_servers.
// ///////////////////////////////////////////
public static int How_many_existing_servers() {
int i = 0;
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "********************";
try {
String command = "cat /etc/haproxy/haproxy.cfg";
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
;
session.setPassword(haproy_host_password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("-www"))
i++;
// System.out.println(line);
}
bufferedReader.close();
inputReader.close();
} catch (IOException ex) {
ex.printStackTrace();
return i;
}
channel.disconnect();
session.disconnect();
return i;
} catch (Exception ex) {
ex.printStackTrace();
return i;
}
}
use of com.jcraft.jsch.Channel in project OsmAnd-tools by osmandapp.
the class IndexUploader method uploadToSSH.
public void uploadToSSH(File f, String description, String size, String date, UploadSSHCredentials cred) throws IOException, JSchException {
log.info("Uploading file " + f.getName() + " " + size + " MB " + date + " of " + description);
// Upload to ftp
JSch jSch = new JSch();
boolean knownHosts = false;
if (cred.knownHosts != null) {
jSch.setKnownHosts(cred.knownHosts);
knownHosts = true;
}
if (cred.privateKey != null) {
jSch.addIdentity(cred.privateKey);
}
String serverName = cred.url;
if (serverName.startsWith("ssh://")) {
serverName = serverName.substring("ssh://".length());
}
Session session = jSch.getSession(cred.user, serverName);
if (cred.password != null) {
session.setPassword(cred.password);
}
if (!knownHosts) {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
}
String rfile = cred.path + "/" + f.getName();
String lfile = f.getAbsolutePath();
session.connect();
// exec 'scp -t rfile' remotely
String command = "scp -p -t \"" + rfile + "\"";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = (new File(lfile)).length();
command = "C0644 " + filesize + " ";
if (lfile.lastIndexOf('/') > 0) {
command += lfile.substring(lfile.lastIndexOf('/') + 1);
} else {
command += lfile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
// send a content of lfile
FileInputStream fis = new FileInputStream(lfile);
byte[] buf = new byte[1024];
try {
int len;
while ((len = fis.read(buf, 0, buf.length)) > 0) {
// out.flush();
out.write(buf, 0, len);
}
} finally {
fis.close();
}
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
channel.disconnect();
session.disconnect();
return;
}
out.close();
channel.disconnect();
session.disconnect();
log.info("Finish uploading file index");
}
Aggregations