use of com.jcraft.jsch.ChannelExec 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.ChannelExec 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.ChannelExec in project ant-ivy by apache.
the class Scp method getExecChannel.
/**
* @return ChannelExec
* @throws JSchException if something goes wrong
*/
private ChannelExec getExecChannel() throws JSchException {
ChannelExec channel;
channel = (ChannelExec) session.openChannel("exec");
return channel;
}
use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.
the class Scp method getFileinfo.
/**
* Initiates an SCP sequence but stops after getting fileinformation header
*
* @param remoteFile
* to get information for
* @return the file information got
* @throws IOException
* in case of network problems
* @throws RemoteScpException
* in case of problems on the target system (connection ok)
*/
public FileInfo getFileinfo(String remoteFile) throws IOException, RemoteScpException {
ChannelExec channel = null;
FileInfo fileInfo = null;
if (remoteFile == null) {
throw new IllegalArgumentException("Null argument.");
}
String cmd = "scp -p -f \"" + remoteFile + "\"";
try {
channel = getExecChannel();
channel.setCommand(cmd);
fileInfo = receiveStream(channel, remoteFile, null);
channel.disconnect();
} catch (JSchException e) {
throw new IOException("Error during SCP transfer. " + e.getMessage(), e);
} finally {
if (channel != null) {
channel.disconnect();
}
}
return fileInfo;
}
use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.
the class Scp method put.
/**
* Copy a local file to a remote site, uses the specified mode when creating the file on the
* remote side.
*
* @param localFile
* Path and name of local file. Must be absolute.
* @param remoteTargetDir
* Remote target directory where the file has to end up (optional)
* @param remoteTargetName
* file name to use on the target system
* @param mode
* a four digit string (e.g., 0644, see "man chmod", "man open")
* @throws IOException
* in case of network problems
* @throws RemoteScpException
* in case of problems on the target system (connection ok)
*/
@SuppressWarnings("unused")
public void put(String localFile, String remoteTargetDir, String remoteTargetName, String mode) throws IOException, RemoteScpException {
ChannelExec channel = null;
if (localFile == null || remoteTargetName == null) {
throw new IllegalArgumentException("Null argument.");
}
if (mode != null) {
if (mode.length() != MODE_LENGTH) {
throw new IllegalArgumentException("Invalid mode.");
}
for (char c : mode.toCharArray()) {
if (!Character.isDigit(c)) {
throw new IllegalArgumentException("Invalid mode.");
}
}
}
String cmd = "scp -t ";
if (mode != null) {
cmd += "-p ";
}
if (remoteTargetDir != null && remoteTargetDir.length() > 0) {
cmd += "-d " + remoteTargetDir;
}
try {
channel = getExecChannel();
channel.setCommand(cmd);
sendFile(channel, localFile, remoteTargetName, mode);
channel.disconnect();
} catch (JSchException e) {
if (channel != null) {
channel.disconnect();
}
throw new IOException("Error during SCP transfer." + e.getMessage(), e);
}
}
Aggregations