com.jcraft.jsch 下载.jschexception 在linux环境下可以运行吗

3403人阅读
java(13)
采用JSCH实现对linux远程操作(SSH协议),实现在远程linux机器上执行相关的命令
import java.io.BufferedR
import java.io.IOE
import java.io.InputS
import java.io.InputStreamR
import java.nio.charset.C
import com.jcraft.jsch.C
import com.jcraft.jsch.ChannelE
import com.jcraft.jsch.JS
import com.jcraft.jsch.JSchE
import com.jcraft.jsch.S
public class JSchDemo {
private String charset = &UTF-8&; // 设置编码格式
private S // 用户名
private S // 登录密码
private S // 主机IP
private JS
* @param user用户名
* @param passwd密码
* @param host主机IP
public JSchDemo(String user, String passwd, String host) {
this.user =
this.passwd =
this.host =
* 连接到指定的IP
* @throws JSchException
public void connect() throws JSchException {
jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(passwd);
java.util.Properties config = new java.util.Properties();
config.put(&StrictHostKeyChecking&, &no&);
session.setConfig(config);
session.connect();
* 执行相关的命令
public void execCmd() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String command = &&;
BufferedReader reader =
Channel channel =
while ((command = br.readLine()) != null) {
channel = session.openChannel(&exec&);
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in,
Charset.forName(charset)));
String buf =
while ((buf = reader.readLine()) != null) {
System.out.println(buf);
} catch (IOException e) {
e.printStackTrace();
} catch (JSchException e) {
e.printStackTrace();
} finally {
reader.close();
} catch (IOException e) {
e.printStackTrace();
channel.disconnect();
session.disconnect();
public static void main(String[] args) throws Exception {
String user = &root&;
String passwd = &123456&;
String host = &192.168.1.188&;
JSchDemo demo = new JSchDemo(user, passwd, host);
demo.connect();
demo.execCmd();
暂时功能还比较简单
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:153684次
积分:1961
积分:1961
排名:第19879名
原创:41篇
转载:26篇
评论:16条
(1)(1)(1)(1)(3)(4)(5)(1)(2)(1)(3)(7)(9)(8)(20)com.jcraft.jsch.JSchException:&connection&is&closed&by&foreign&hos
最近在给一个快递公司做一个FTP Server,用的是VSFTP 和JSCH
。当用JSCH客户端并发访问VSFTP的时候,报了这个问题。这个问题测试了好几次才测试出来!
可能会有很多原因导致此异常,但我这边最终解决办法是修改linux系统的ssh配置文件的:MaxStartups
存在问题:SSH终端连接数最大为10个
解决方案:
<font COLOR="#)&&&&&&&
修改/etc/ssh/sshd_config中#MaxStartups,将其改为MaxStartups 1000
<font COLOR="#)&&&&&&&
重启SSH服务,/etc/init.d/ssh restart
Debian系统默认连接时间120秒,如果远程终端连接数过多,则会出现超时连接,解决办法如下:
<font COLOR="#)&&&&&&&
修改/etc/ssh/sshd_config中LoginGraceTime 120,将其改为LoginGraceTime
0,其中0表示不限制连接时间
<font COLOR="#)&&&&&&&
重启SSH服务,/etc/init.d/ssh restart
注:重启TELNET服务,/etc/init.d/xinetd restart
重启FTP服务,/etc/init.d/vsftpd restart
另外,/etc/security/limits.conf&
也可以修改最大连接数,但对SSH服务不生效。
存在问题:SSH终端连接数最大为10个&&
这句话实际描述不是十分恰当,MaxStartups默认确实是10(linux),但是这个数字是链接队列中等待握手的数。如果这个数字太小,当并发链接超过10的时候就会出现:
connection is closed by foreign host 。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Java使用SSH从远程服务器下载文件_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Java使用SSH从远程服务器下载文件
来源:Linux社区&
作者:傲风
Telnet、FTP、POP3在网络传输的过程中都是采用明文,容易被监听或者遭到到攻击。而SSH为远程登陆会话和其他的网络服务提供安全协议,通过加密数据防止传输过程中信息泄漏。C/C++可以调用来实现SSH,Java也有相应的开源类库,它是SSH2的一个纯Java实现。
下SSH无密码登录的配置
Linux下实现SSH无密码验证登陆
和CentOS如何配置SSH使得无密码登陆
类SftpDownloader的源代码:
package cn.import java.io.BufferedOutputSimport java.io.Fimport java.io.FileNotFoundEimport java.io.FileOutputSimport java.io.OutputSimport java.util.Pimport mons.io.IOUimport org.apache.log4j.Limport cn.aofeng.dto.SftpConnectIimport cn.aofeng.util.ConsoleProgressBimport com.jcraft.jsch.ChannelSimport com.jcraft.jsch.JSimport com.jcraft.jsch.JSchEimport com.jcraft.jsch.Simport com.jcraft.jsch.SftpEimport com.jcraft.jsch.SftpProgressM/*** 使用SFTP从远程主机下载文件。* * @author 傲风 &&*/public class SftpDownloader {private final static Logger logger = Logger.getLogger(SftpDownloader.class);/*** 使用SFTP从远程主机下载文件。* * @param connectInfo SFTP连接信息* @param localPath 本地文件路径* @param remotePath 远程文件路径*/public void download(SftpConnectInfo connectInfo, String localPath, final String remotePath) {JSch jsch = new JSch();Session session = null;ChannelSftp channel = null;OutputStream outs = null;try {session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), connectInfo.getPort());session.setPassword(connectInfo.getPassword());Properties props = new Properties();props.put("StrictHostKeyChecking", "no");session.setConfig(props);session.connect(5000); // 毫秒channel = (ChannelSftp) session.openChannel("sftp");channel.connect(5000);outs = new BufferedOutputStream(new FileOutputStream(new File(localPath)));channel.get(remotePath, outs, new SftpProgressMonitor() {ConsoleProgressBar progress = null;private long current = 0;@Overridepublic void init(int op, String src, String dest, long max) {progress = new ConsoleProgressBar(0, max, 50);}@Overridepublic void end() {if (logger.isInfoEnabled()) {logger.debug( String.format("download file:%s complete", remotePath) );}}@Overridepublic boolean count(long count) {current +=progress.show(current);return true;}});} catch (JSchException e) {logger.error( String.format("connect remote host[%s:%d] occurs error", connectInfo.getHost(), connectInfo.getPort()), e);} catch (SftpException e) {logger.error( String.format("get remote file:%s occurs error", remotePath), e);} catch (FileNotFoundException e) {logger.error( String.format("can not find local file:%s", localPath), e);} finally {IOUtils.closeQuietly(outs);if (null != channel) {channel.disconnect();}if (null != session) {session.disconnect();}}}}
注:进度条ConsoleProgressBar的源代码查看文章&
更多详情见请继续阅读下一页的精彩内容:
相关资讯 & & &
& (06月18日)
& (05月05日)
& (06月25日)
& (05月23日)
& (02月28日)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款19032人阅读
Linux(27)
J2SE(43)
&&&&&&&&&&& 一个简单的jsch链接linux并执行命令的utils。
&&&&&&&&& import java.io.BufferedR
import java.io.IOE
import java.io.InputS
import java.io.InputStreamR
import com.jcraft.jsch.C
import com.jcraft.jsch.ChannelE
import com.jcraft.jsch.JS
import com.jcraft.jsch.JSchE
import com.jcraft.jsch.S
public class ShellUtils {
private static JS
private static S
* 连接到指定的IP
* @throws JSchException
public static void connect(String user, String passwd, String host) throws JSchException {
jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(passwd);
java.util.Properties config = new java.util.Properties();
config.put(&StrictHostKeyChecking&, &no&);
session.setConfig(config);
session.connect();
* 执行相关的命令
* @throws JSchException
public static void execCmd(String command, String user, String passwd, String host) throws JSchException {
connect(user, passwd, host);
BufferedReader reader =
Channel channel =
while (command != null) {
channel = session.openChannel(&exec&);
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String buf =
while ((buf = reader.readLine()) != null) {
System.out.println(buf);
} catch (IOException e) {
e.printStackTrace();
} catch (JSchException e) {
e.printStackTrace();
} finally {
reader.close();
} catch (IOException e) {
e.printStackTrace();
channel.disconnect();
session.disconnect();
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:501356次
积分:4922
积分:4922
排名:第5720名
原创:83篇
转载:59篇
评论:88条
(1)(4)(1)(1)(2)(4)(9)(5)(8)(4)(95)(9)采用JSCH实现对linux远程操作,执行简单的命令_jsch 执行命令_词汇网
<meta name="keywords" content="采用JSCH实现对linux远程操作,执行简单的命令jsch 执行命令,采用JSCH实现对linux远程操作,能够实现执行简单的命令
采用JSCH实现对linux远程操作,执行简单的命令
责任编辑:词汇网 发表时间: 21:31:34
采用JSCH实现对linux远程操作,能够实现执行简单的命令 标签:
代码片段(1)[全屏查看所有代码] 1.[代码][Java]代码 import java.io.BufferedRimport java.io.IOEimport java.io.InputSimport java.io.InputStreamRimport java.nio.charset.Cimport com.jcraft.jsch.Cimport com.jcraft.jsch.ChannelEimport com.jcraft.jsch.JSimport com.jcraft.jsch.JSchEimport com.jcraft.jsch.Spublic class JSchDemo {private String charset = "UTF-8"; // 设置编码格式private S // 用户名private S // 登录密码private S // 主机IPprivate JSprivate S/** * * @param user用户名 * @param passwd密码 * @param host主机IP */public JSchDemo(String user, String passwd, String host) {this.user =this.passwd =this.host =}/** * 连接到指定的IP * * @throws JSchException */public void connect() throws JSchException {jsch = new JSch();session = jsch.getSession(user, host, 22);session.setPassword(passwd);java.util.Properties config = new java.util.Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);session.connect();}/** * 执行相关的命令 */public void execCmd() {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String command = "";BufferedReader reader =Channel channel =try {while ((command = br.readLine()) != null) {channel = session.openChannel("exec");((ChannelExec) channel).setCommand(command);channel.setInputStream(null);((ChannelExec) channel).setErrStream(System.err);channel.connect();InputStream in = channel.getInputStream();reader = new BufferedReader(new InputStreamReader(in,Charset.forName(charset)));String buf =while ((buf = reader.readLine()) != null) {System.out.println(buf);}}} catch (IOException e) {e.printStackTrace();} catch (JSchException e) {e.printStackTrace();} finally {try {reader.close();} catch (IOException e) {e.printStackTrace();}channel.disconnect();session.disconnect();}}public static void main(String[] args) throws Exception {String user = "root";String passwd = "123456";String host = "192.168.1.188";JSchDemo demo = new JSchDemo(user, passwd, host);demo.connect();demo.execCmd();}}
上一集:没有了 下一集:
相关文章:&&&&&&&&&&
最新添加资讯
24小时热门资讯
附近好友搜索

我要回帖

更多关于 com.jcraft.jsch api 的文章

 

随机推荐