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百度云链 ...
随机推荐
- 20.QT-Qpixmap实现图片鼠标缩放,鼠标拖动示例(详解)
通过 QPainter 绘画实现,以本地图片985*740为例 如下图所示: 效果如下所示: 实现原理 主要通过以下函数实现: , ); //平铺显示pixmap //x y w h :表示绘画区域 ...
- (一)linux定时任务的设置 crontab 基础实践
为当前用户创建cron服务 1. 键入 crontab -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...
- 解决Debian 9 iwlwifi固件缺失导致无法连接无线网络的问题
本文由荒原之梦原创,原文链接:http://zhaokaifeng.com/?p=692 前言: 本文介绍了解决Debian9安装完成后无法连接wifi的问题以及一些扩展知识. 问题描述: 安装Deb ...
- tkinter中text文本与scroll滚动条控件(五)
text与scroll控件 import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry("30 ...
- 1. Java面向对象之泛型-认识泛型
package generic; class Point{ private Object x; private Object y; public Object getX() { return x; ...
- JFree图表
************************************************************************************* 使用JFree生成统计图片, ...
- golang接口三个特性
类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍.go是一个静态性语言,每个变量都有静态的类型,因此每个变量在编译阶段中有明确的变量类型,比如像:int.float32.MyTy ...
- 【BZOJ 2844】: albus就是要第一个出场
题目大意: 给一个长度为n的序列,将其子集的异或值排序得到B数组,给定一个数字Q,保证Q在B中出现过,询问Q在B中第一次出现的下标. 题解: 感觉和hdu3949第K小异或值有一像,然而发现要求出现次 ...
- bzoj 1426 收集邮票
f[i]:当前已拥有i种邮票,还需要买的邮票数的期望值. g[i]:当前已拥有i种邮票,还需要的钱的期望值. 每张邮票初始都是1元钱,每买一张邮票,还没购买的邮票每张都涨价1元. f[i]=1+(n ...
- POJ_2653_Pick-up sticks_判断线段相交
POJ_2653_Pick-up sticks_判断线段相交 Description Stan has n sticks of various length. He throws them one a ...