jsch


JSCH Channel Is Not Opened (New)


PROBLEM: Connecting remote site by SFTP using JSCH library results in "Channel is not opened". Code works fine from one internet connection, but fails from another. Another is done via proxy. Nevertheless, I can connect by SFTP from another, using FileZilla client.
RESEARCH: Tried increasing connection timout and setting useDNS: false from similar questions. I've got session and channel opned from it. Still, after timout, channel connection fails.
QUESTION: How to get connection.
Below is the code and error stack trace extracts:
JSch jsch = new JSch();
UserInfo userInfo ;
jsch.setKnownHosts(KNOWN_HOSTS);
session = jsch.getSession(
Menue.SITE_LOGIN,
Menue.SITE_HOST,
Menue.SITE_PORT);
session.setPassword(
Menue.SITE_PASSWORD);
if (Menue.USE_PROXY) {
session.setProxy(
new ProxyHTTP(
Menue.PROXY_HOST,
Menue.PROXY_PORT));
}
session.setConfig(
"StrictHostKeyChecking",
"yes");
session.connect();
channel = session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(60 * 1000); // Error: channel is not opened.
Stacktrace:
com.jcraft.jsch.JSchException: channel is not opened. at
com.jcraft.jsch.Channel.sendChannelOpen(Channel.java:765) at
com.jcraft.jsch.Channel.connect(Channel.java:151)
The output on mistake was non-informative (as well as scarse documentation on the library). The reason was in managing server rsa-keys on connection. I wanted no ui dialogs and full automation (UserInfo disabled).
Also StrickedHostKeyChecking should be no, although it's unsafe. Setting it to "no" allows autoadding rsa-keys to local keys repository (e.g. ~/.ssh/known_hosts).
Finally resulted in a code:
try {
JSch jsch = new JSch();
jsch.setKnownHosts("~/.ssh/known_hosts");
Session session = jsch.getSession(
"my_login",
"my.host",
22);
session.setPassword(
"password");
// Autoadd system rsa-keys to system file like known_hosts by
// disabling strick keys checking:
java.util.Properties config = new java.util.Properties();
config.put(
"StrictHostKeyChecking",
"no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println(
sftpChannel.ls("/"));
session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
These articles helped:
Original example on KnownHosts
Adding rsa to known_hosts
Unknown hostkey management

Related Links

JSCH read from input stream hangs from time to time
Hundreds of Apache SSHD clients leave hundreds of OpenSSH daemon processes running
Jsch - ssh connection through ssh tunnel
Changing password using Jsch and ChannelExec
how to delete local file using jsch after file is sent to remote SFTP server directory
com.jcraft.jsch.JSchException: Auth fail error
Creating a new file in SSH enabled system using JSCH
How to interact with the script after its execution using Jsch
java jsch -executing multiple unix commands
How to get one stream from error stream and input stream when calling a script using JSCH
SFTP using jschexception verify false sometimes fails
how to increase the jsch buffer size?
The cipher 'aes256-cbc' is required, but it is not available
How to pause/resume procedure while/after upload file by ChannelSftp
jsch + running multiple command in shell after SU and then close channal
NGit/Nsch Generating SSH Keypair (c# Jgit)

Categories

HOME
cookies
zeromq
gremlin
ngrx
kde
smarty
relative-path
electron
spring-jdbc
yarn
frameworks
baqend
wamp
in-app-purchase
gorm
php-7.1
moonmail
django-simple-history
progressive-web-apps
solaris-10
http-status-code-504
text-rendering
shared-hosting
karma-jasmine
lldb
mustache.php
chromium-embedded
kudan
tibco-mdm
restful-authentication
android-widget
vlsi
sparse-matrix
virtualdub
entitlements
web-api-testing
libssl
librato
sql-server-2012-express
bcd
binary-data
pdb
winrt-xaml-toolkit
grails-3.1
hexo
bower-install
file-format
stacked
dartium
twitch
reportbuilder
sas-visual-analytics
keydown
checkboxlist
.net-4.6.2
segment
executenonquery
taffy
sqlclient
font-size
windows-mobile-6.5
crosswalk-runtime
smart-table
cubes
removechild
sts-springsourcetoolsuite
intrusion-detection
login-control
autorest
contact-list
master-slave
sdf
pundit
rgeo
php-ci
sonarqube5.1.2
website-monitoring
gwidgets
pick
qpainter
gulp-less
sgen
contenttype
valueconverter
braille
meteor-velocity
tidy
ruby-datamapper
rabl
limejs
http-unit
itmstransporter
bluepill
hosts-file
custom-backend
propagation
cxxtest
h.323
zend-translate
web-application-design
castle-monorail
compiler-specific
boost-smart-ptr
commodore
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App