use of com.jcraft.jsch.Channel in project litle-sdk-for-java by Vantiv.
the class Communication method sendLitleRequestFileToSFTP.
/**
* This method sends the request file to Litle's server sFTP
* @param requestFile
* @param configuration
* @throws IOException
*/
public void sendLitleRequestFileToSFTP(File requestFile, Properties configuration) throws IOException {
String username = configuration.getProperty("sftpUsername");
String password = configuration.getProperty("sftpPassword");
String hostname = configuration.getProperty("batchHost");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = null;
Session session = null;
try {
jsch = new JSch();
session = jsch.getSession(username, hostname);
session.setConfig(config);
session.setPassword(password);
session.connect();
} catch (JSchException e) {
throw new LitleBatchException("Exception connection to Litle", e);
}
Channel channel = null;
try {
channel = session.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
throw new LitleBatchException("Exception connection to Litle", e);
}
ChannelSftp sftp = (ChannelSftp) channel;
boolean printxml = configuration.getProperty("printxml") != null && configuration.getProperty("printxml").equalsIgnoreCase("true");
if (printxml) {
BufferedReader reader = new BufferedReader(new FileReader(requestFile));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
try {
sftp.put(requestFile.getAbsolutePath(), "inbound/" + requestFile.getName() + ".prg");
sftp.rename("inbound/" + requestFile.getName() + ".prg", "inbound/" + requestFile.getName() + ".asc");
} catch (SftpException e) {
throw new LitleBatchException("Exception SFTP operation", e);
}
channel.disconnect();
session.disconnect();
}
use of com.jcraft.jsch.Channel in project opentest by mcdcorp.
the class PutToSftp method run.
@Override
public void run() {
super.run();
String sftpHost = this.readStringArgument("sftpHost");
Integer sftpPort = this.readIntArgument("sftpPort", 22);
String userName = this.readStringArgument("userName");
String password = this.readStringArgument("password");
String sourceDir = this.readStringArgument("sourceDir");
String sourceFileName = this.readStringArgument("sourceFile");
String destinationDir = this.readStringArgument("destinationDir");
String destinationFileName = this.readStringArgument("destinationFile", sourceFileName);
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(userName, sftpHost, sftpPort);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
this.log.trace("Connected to SFTP host");
channel = session.openChannel("sftp");
channel.connect();
this.log.trace("The SFTP channel was opened and connected");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(destinationDir);
File sourceFile = new File(sourceDir, sourceFileName);
FileInputStream inputStream = new FileInputStream(sourceFile);
channelSftp.put(inputStream, destinationFileName);
inputStream.close();
} catch (Exception ex) {
throw new RuntimeException("SFTP transfer failed", ex);
} finally {
if (channelSftp != null) {
channelSftp.exit();
}
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
use of com.jcraft.jsch.Channel in project KeyBox by skavanagh.
the class SSHUtil method pushUpload.
/**
* distributes uploaded item to system defined
*
* @param hostSystem object contains host system information
* @param session an established SSH session
* @param source source file
* @param destination destination file
* @return status uploaded file
*/
public static HostSystem pushUpload(HostSystem hostSystem, Session session, String source, String destination) {
hostSystem.setStatusCd(HostSystem.SUCCESS_STATUS);
Channel channel = null;
ChannelSftp c = null;
try (FileInputStream file = new FileInputStream(source)) {
channel = session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(CHANNEL_TIMEOUT);
c = (ChannelSftp) channel;
destination = destination.replaceAll("~\\/|~", "");
c.put(file, destination);
} catch (JSchException | IOException | SftpException ex) {
log.info(ex.toString(), ex);
hostSystem.setErrorMsg(ex.getMessage());
hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS);
}
// exit
if (c != null) {
c.exit();
}
// disconnect
if (channel != null) {
channel.disconnect();
}
return hostSystem;
}
use of com.jcraft.jsch.Channel in project coprhd-controller by CoprHD.
the class CinderUtils method checkProviderConnection.
private static void checkProviderConnection(StorageProvider storageProvider) throws JSchException, SftpException, IOException {
ChannelSftp sftp = null;
Session session = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(storageProvider.getUserName(), storageProvider.getIPAddress(), storageProvider.getPortNumber());
session.setPassword(storageProvider.getPassword());
Hashtable<String, String> config = new Hashtable<String, String>();
config.put(STRICT_HOST_KEY_CHECKING, NO);
session.setConfig(config);
session.connect(timeout);
_log.debug("Session Connected...");
Channel channel = session.openChannel("sftp");
sftp = (ChannelSftp) channel;
sftp.connect(connectTimeout);
} finally {
if (sftp != null) {
sftp.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
use of com.jcraft.jsch.Channel in project coprhd-controller by CoprHD.
the class IsilonSshApi method executeSsh.
/**
* Executes a command on the Isilon CLI.
*
* @param command command to execute on the Isilon CLI.
* @param request payload for the command
* @return result of executing the command.
*/
public IsilonXMLApiResult executeSsh(String command, String request) {
IsilonXMLApiResult result = new IsilonXMLApiResult();
if ((_host == null) || (_userName == null) || (_password == null)) {
_log.error("Invalid connection parameter");
result.setCommandFailed();
return result;
}
String cmd = "isi " + command + " " + request;
_log.info("executeSsh: cmd: " + cmd);
InputStream in = null;
Session session = null;
Channel channel = null;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(_userName, _host, DEFAULT_PORT);
session.setPassword(_password);
session.setConfig(config);
session.connect();
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
channel.setInputStream(null);
in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[BUFFER_SIZE];
StringBuilder cmdResults = new StringBuilder();
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, BUFFER_SIZE);
if (i < 0)
break;
cmdResults.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
_log.info("Ssh exit status: " + channel.getExitStatus());
result.setMessage(cmdResults.toString());
// Set the command result status.
if (channel.getExitStatus() == 0) {
StringTokenizer st = new StringTokenizer(cmdResults.toString());
if (st.hasMoreTokens()) {
// data mover name
st.nextToken();
}
String res = "";
if (st.hasMoreTokens()) {
// contains status or result.
res = st.nextToken();
}
if (res.equalsIgnoreCase("done")) {
result.setCommandSuccess();
} else if (res.equalsIgnoreCase("error")) {
result.setCommandFailed();
} else {
result.setCommandSuccess();
}
} else {
result.setCommandFailed();
}
break;
}
try {
Thread.sleep(_respDelay);
} catch (InterruptedException e) {
_log.error("VNX File executeSsh Communication thread interrupted for command: " + cmd, e);
}
}
_log.info("executeSsh: Done");
} catch (Exception e) {
_log.error("VNX File executeSsh connection failed while attempting to execute: " + cmd, e);
result.setCommandFailed();
result.setMessage(e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
return result;
}
Aggregations