java使用SSH连接Linux系统
SSH连接linux系统使我们在开发项目中常用到的,现在留下来,做个记录
package com.log; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter; import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; public class Test8 { /**
* 连接linux系统
* @param args
*/
public static void main(String[] args) {
try {
Connection conn = new Connection("192.168.81.129");
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword("root",
"123456");
if (isAuthenticated == false) {
throw new IOException("Authentication failed");
}
Session sess = conn.openSession();
sess.requestPTY("bash");
sess.startShell();
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader stdoutReader = new BufferedReader(
new InputStreamReader(stdout));
BufferedReader stderrReader = new BufferedReader(
new InputStreamReader(stderr));
BufferedReader inputReader = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(sess.getStdin());
String temp = "";
while (!temp.equals("exit")) {
System.out.print("[root@vmone ~]#");
temp = inputReader.readLine();
out.println(temp);
out.flush();
String line = null;
while ((line = stdoutReader.readLine()) != null) {
if (line.length() == 0) {// line等于null从来不会发生,导致程序卡在这里
continue;
} else{
System.out.println(line);
}
}
System.out.println("Here is the output from stderr:");
while (true) {
line = stderrReader.readLine();
if (line == null)
break;
System.out.println(line);
}
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
conn.close();
System.out.println("close connection");
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
} }
下面截图是次程序运行结果

java使用SSH连接Linux系统的更多相关文章
- java使用ssh连接Linux并执行命令
方式1:通过设置账号密码和链接地址 maven pom.xml配置: <dependency> <groupId>com.jcraft</groupId ...
- 解决ssh连接linux系统特别慢的问题
新安装的centos系统,发现ssh连接很慢,因为是测试环境,对安全的要求不高,所以完全可以更快的连接,下面一起来解决这个问题. 一.分析主要原因: 1.SSH的反向DNS解析会消耗大量时间 2.GS ...
- root用户无法通过ssh连接Linux系统
ssh协议为了安全,有些版本默认禁止root用户的登陆 cd /etc/ssh 编辑sshd_config文件 cat sshd_config | grep PermitRootLogin Permi ...
- ssh连接linux服务器只显示-bash-4.1#不显示路径解决方法
ssh连接linux服务器只显示-bash-4.1#不显示路径时,我们只需要修改 ~/.bash_profile文件,如果不存在这个文件,那么新建一个,增加内容 export PS1='[\u@\ ...
- 烂泥:【解决】Ubuntu下使用SSH连接centos系统很慢
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天在Ubuntu下使用SSH连接centos系统,发现连接很慢.建议一个连接大约需要30s.很是坑爹,如下: 后来查询相关资料,发现这个是Ubunt ...
- Notepad++远程连接Linux系统
首先在官网下载 https://notepad-plus-plus.org/news/notepad-7.6.4-released.html 在命令行数输入ifconfig 查看自己的Linux的ip ...
- Python ssh连接Linux服务器报Incompatible ssh peer (no acceptable kex algorithm) 解决方法
python通过ssh连接linux服务器,部分服务器出现如下异常 03:50:48.725 FAIL ftp operation failed, Incompatible ssh peer (no ...
- SSH连接Linux的Server超时
SSH连接Linux的Server超时 http://blog.csdn.net/cheng830306/article/details/21796865
- Xshell远程连接Linux系统
一般来说我们连接Linux,会使用到一些远程连接工具 比如:Xshell和Xftp Xshell:远程连接linux系统 Xftp:远程在Linux系统中上传或下载文件 Xshell和Xftp百度云链 ...
随机推荐
- CentOS7下安装rabbitmq消息队
安装如下步骤: 1.首先安装erlang yum install erlang 注:如果出现No package erlang available. rpm -Uvh http://download. ...
- JavaScript设计模式Item 1—多态
多态的实际含义是:同一操作作用于不同的对象上面,可以产生不同的解释和不同的执行结果.换句话说,给不同的对象发送同一个消息的时候,这些对象会根据这个消息分别给出不同的反馈. 从字面上来理解多态不太容易, ...
- github 用git bash上传项目 最后提示 Everything up-to-date 但没传上去
今天为了把本地的项目远程到githup远程仓库,把好久不用的githup命令重新用了起来,不用怀疑,很正常的出错了. 为了长点记性,总结错误与解决办法如下: 1.我犯得第一个简单错误,是忘了git c ...
- 建立CMenu菜单项,实现选中菜单项点击左键响应事件
这里我只是根据自己的项目做了一些总结,实现点击右键弹出菜单项,点左键选择菜单项: CMenu menu; VERIFY(menu.CreatePopupMenu());//新建一个cmenu菜单项 m ...
- [JSOI2008]星球大战starwar BZOJ1015
并查集 正序处理时间复杂度为n^2,考虑逆序处理,这样,时间复杂度从n^2降为nlogn 附上代码: #include <cstdio> #include <algorithm> ...
- Android 画文字图
画图 private Bitmap getbitmap(String content) { Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.C ...
- 深入css布局篇(3)完结 — margin问题与格式化上下文
深入css布局(3) - margin问题与格式化上下文 在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深入学习一下 ...
- WebService-----Xfire
Xfire相对Axis2 个人感觉相对简单一点,因为myEclipse上可以自动生成.而且在目前来讲,Xfire也是更受欢迎一点. 首先说明Xfire所用jar包:http://download.cs ...
- Oracle系列-锁表与解锁解决方案(大招版)-解决问题才是王道
[Oracle系列-锁表与解锁解决方案(大招版)] --1查看被锁的表 select b.owner,b.object_name,a.session_id,a.locked_mode from v$l ...
- html select 标签设置默认选中
方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select id = " ...