java - JSch ChannelSftp exit status is always -1 -
i utilize jsch library develop sftp client.
the problem both get
, put
methods' status -1.
here code:
class sftpclient { private static final logger log = logger.getlogger(sftpclient.class); /** connection port number */ public static final int port = 22; /** secured protocol name */ private static final string protocol = "sftp"; /** connection time out in milliseconds */ public static final int time_out = 5000; /** class serves central configuration point, , mill session objects configured these settings */ private jsch _client; /** session represents connection ssh server */ private session _session; /** channel connected secured server (as subsystem of ssh server) */ private channelsftp _channelsftp; /** * value returned lastly executed command. */ private int _exitvalue; /** * computer contains url, login , password connect. */ private computer _computer; /** * initialize secured client * @param target - machine want connect */ public sftpclient(computer target) { _client = new jsch(); _computer = target; } protected void connect() throws exception { seek { if (_client == null) { _client = new jsch(); } if (_session == null) { _session = _client.getsession(_computer.getlogin(), _computer.geturl(), port); properties props = new properties(); props.put("stricthostkeychecking", "no"); props.put("compression.s2c", "zlib,none"); props.put("compression.c2s", "zlib,none"); _session.setconfig(props); _session.setpassword(_computer.getpassword()); if (log.isdebugenabled()) { log.debug("connecting "+_computer.geturl()+" login "+_computer.getlogin()+"..."); } } if (!_session.isconnected()) { _session.connect(time_out); } // disconnect previous channel if has not been killed if (_channelsftp != null && _channelsftp.isconnected()) { _channelsftp.disconnect(); } _channelsftp = (channelsftp) _session.openchannel(protocol); _channelsftp.connect(); if (log.isinfoenabled()) { log.info("connected "+_computer.geturl()+" login "+_computer.getlogin()); } } catch(jschexception e) { log.error("auth failed", e); throw e; } } protected void connect(string path) throws exception { connect(); if (_channelsftp != null && _channelsftp.isconnected()) { _channelsftp.cd(path); } } public boolean get(final string remotedirectory, final string remotefile, final string localdirectory) throws exception { boolean res = false; if (log.isinfoenabled()) { log.info("download file "+remotedirectory+"/"+remotefile+" "+_computer+" in "+localdirectory); } if (remotedirectory != null && remotefile != null && !remotefile.isempty() && localdirectory != null) { seek { // connect server , alter directory connect(remotedirectory); // alter local directory _channelsftp.lcd(localdirectory); // download file, keeping same name _channelsftp.get(remotefile, remotefile); // update exit value _exitvalue = _channelsftp.getexitstatus(); if (_exitvalue == 0) { res = true; } if (log.isdebugenabled()) { log.debug("exit status is: "+_exitvalue); } } catch(sftpexception e){ log.error("auth failed", e); throw e; } { if (_channelsftp != null && _channelsftp.isconnected()) { _channelsftp.disconnect(); _channelsftp.exit(); } } } else { log.warn("check remotedirectory ('"+remotedirectory+"') or remotefile ('"+remotefile+"') or localdirectory ('"+localdirectory+"')."); } homecoming res; } public void put(final file localfile, final string destpath) throws exception { if (log.isinfoenabled()) { log.info("send file "+localfile+" "+_computer+" in "+destpath); } if (localfile == null) { _exitvalue = -1; log.error("the given local file null. aborting tranfer."); return; } if (!localfile.exists()) { _exitvalue = -1; log.error("the given local file '"+localfile+"' not exist. aborting tranfer."); return; } final inputstream input = new fileinputstream(localfile); if (input == null || input.available() <= 0) { _exitvalue = -1; log.error("cannot read file "+localfile); return; } seek { connect(destpath); _channelsftp.put(input, localfile.getname()); _exitvalue = _channelsftp.getexitstatus(); if (log.isdebugenabled()) { log.debug("exit status is: "+_exitvalue); } } catch(sftpexception e){ log.error("auth failed", e); throw e; } { if (_channelsftp != null && _channelsftp.isconnected()) { _channelsftp.disconnect(); _channelsftp.exit(); } ioutils.closequietly(input); } } public void disconnect() { if (_channelsftp != null && _channelsftp.isconnected()) { _channelsftp.disconnect(); _channelsftp.exit(); } if (_session != null && _session.isconnected()) { _session.disconnect(); if (log.isinfoenabled()) { log.info("secured ftp disconnected"); } } } }
no exception thrown.
by way, files want upload , download tar files. jsch have ftp binary mode?
files transferred. can utilize them worried exit status.
i had jsch api , says:
the exit status available types of channels, , after channel closed (more exactly, before channel closed).
how can know if exit status available channelsftp
?
looking @ source code looks exitstatus not implemented sftp
channel.setexitstatus(reason_code);
is implemented below 2 cases in session class
case ssh_msg_channel_open_failure: case ssh_msg_channel_request:
and both cases not called atleast successful scenarios tested.
java sftp jsch
No comments:
Post a Comment