use of com.jcraft.jsch.Channel in project ASAP by salmant.
the class DM 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 ASAP by salmant.
the class DM method restart_haproxy.
// ///////////////////////////////////////////
public static void restart_haproxy() {
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "**************************";
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();
}
}
use of com.jcraft.jsch.Channel in project ASAP by salmant.
the class Run_a_Container method add_to_haproxy.
// ///////////////////////////////////////////////////////////
public void add_to_haproxy() {
String haproy_ip = "194.249.1.110";
String haproy_host_user = "root";
String haproy_host_password = "*****************";
// ////////////////////////////// add new server
try {
String command = "echo " + "\"" + " server " + i_str + "-www " + new_IP + ":5000 check" + "\"" + ">> /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();
}
// ////////////////////////////// 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();
}
}
use of com.jcraft.jsch.Channel in project onebusaway-application-modules by camsys.
the class GtfsRealtimeSource method readFeedFromUrl.
/**
* This method was added to allow accessing an SFTP feed, since the Java URL
* class does not yet support the SFTP protocol.
*
* @param url of the SFTP feed to read
* @return a {@link FeedMessage} constructed from the protocol buffer content
* of the specified url
* @throws IOException
*/
private FeedMessage readFeedFromUrl(String url) throws IOException {
Session session = null;
Channel channel = null;
ChannelSftp downloadChannelSftp = null;
InputStream in = null;
JSch jsch = new JSch();
// Parse SFTP URL
int idx = url.indexOf("//") + 2;
int idx2 = url.indexOf(":", idx);
String user = url.substring(idx, idx2);
idx = idx2 + 1;
idx2 = url.indexOf("@");
String pw = url.substring(idx, idx2);
url = url.substring(idx2 + 1);
idx = url.indexOf(":");
String host = url.substring(0, idx);
String rdir = "";
idx = url.indexOf("/") + 1;
idx2 = url.lastIndexOf("/");
if (idx2 > idx) {
rdir = url.substring(idx, idx2);
} else {
idx2 = idx - 1;
}
String rfile = url.substring(idx2 + 1);
try {
session = jsch.getSession(user, host, 22);
session.setPassword(pw);
session.setConfig("StrictHostKeyChecking", "no");
// Set timeout to 10 seconds
session.connect(10000);
channel = session.openChannel("sftp");
channel.connect();
downloadChannelSftp = (ChannelSftp) channel;
downloadChannelSftp.cd(downloadChannelSftp.getHome() + "/" + rdir);
File downloadFile = new File(downloadChannelSftp.getHome() + "/" + rfile);
in = downloadChannelSftp.get(downloadFile.getName());
return FeedMessage.parseFrom(in, _registry);
} catch (JSchException ex) {
_log.error("connection issue with sftp url " + url);
return getDefaultFeedMessage();
} catch (SftpException e) {
_log.error("connection issue with sftp");
e.printStackTrace();
return getDefaultFeedMessage();
} finally {
try {
if (channel != null)
channel.disconnect();
if (session != null)
session.disconnect();
if (in != null)
in.close();
} catch (IOException ex) {
_log.error("error closing url stream " + url);
}
}
}
use of com.jcraft.jsch.Channel in project structr by structr.
the class SSHTest method setupSftpClient.
/**
* Creates an FTP client, a backend user and logs this user in.
*
* @param username
* @return
*/
protected ChannelSftp setupSftpClient(final String username, final String password) {
try (final Tx tx = app.tx()) {
ftpUser = createFTPUser(username, password);
tx.success();
} catch (FrameworkException fex) {
logger.error("Unable to create SFTP user", fex);
}
JSch jsch = new JSch();
try {
final Session session = jsch.getSession(username, host, sshPort);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect(1000);
final Channel channel = session.openChannel("sftp");
channel.connect(1000);
return (ChannelSftp) channel;
} catch (JSchException ex) {
ex.printStackTrace();
}
return null;
}
Aggregations