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系统的更多相关文章

  1. java使用ssh连接Linux并执行命令

     方式1:通过设置账号密码和链接地址 maven pom.xml配置: <dependency>         <groupId>com.jcraft</groupId ...

  2. 解决ssh连接linux系统特别慢的问题

    新安装的centos系统,发现ssh连接很慢,因为是测试环境,对安全的要求不高,所以完全可以更快的连接,下面一起来解决这个问题. 一.分析主要原因: 1.SSH的反向DNS解析会消耗大量时间 2.GS ...

  3. root用户无法通过ssh连接Linux系统

    ssh协议为了安全,有些版本默认禁止root用户的登陆 cd /etc/ssh 编辑sshd_config文件 cat sshd_config | grep PermitRootLogin Permi ...

  4. ssh连接linux服务器只显示-bash-4.1#不显示路径解决方法

    ssh连接linux服务器只显示-bash-4.1#不显示路径时,我们只需要修改  ~/.bash_profile文件,如果不存在这个文件,那么新建一个,增加内容  export PS1='[\u@\ ...

  5. 烂泥:【解决】Ubuntu下使用SSH连接centos系统很慢

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天在Ubuntu下使用SSH连接centos系统,发现连接很慢.建议一个连接大约需要30s.很是坑爹,如下: 后来查询相关资料,发现这个是Ubunt ...

  6. Notepad++远程连接Linux系统

    首先在官网下载 https://notepad-plus-plus.org/news/notepad-7.6.4-released.html 在命令行数输入ifconfig 查看自己的Linux的ip ...

  7. 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 ...

  8. SSH连接Linux的Server超时

    SSH连接Linux的Server超时 http://blog.csdn.net/cheng830306/article/details/21796865

  9. Xshell远程连接Linux系统

    一般来说我们连接Linux,会使用到一些远程连接工具 比如:Xshell和Xftp Xshell:远程连接linux系统 Xftp:远程在Linux系统中上传或下载文件 Xshell和Xftp百度云链 ...

随机推荐

  1. CentOS7下安装rabbitmq消息队

    安装如下步骤: 1.首先安装erlang yum install erlang 注:如果出现No package erlang available. rpm -Uvh http://download. ...

  2. Linux时间子系统之二:表示时间的单位和结构

    人们习惯用于表示时间的方法是:年.月.日.时.分.秒.毫秒.星期等等,但是在内核中,为了软件逻辑和代码的方便性,它使用了一些不同的时间表示方法,并为这些表示方法定义了相应的变量和数据结构,本节的内容就 ...

  3. BackBox错误,无法获得锁...资源暂时不可用...无法锁定管理目录

    今天准备给BackBox安装leafpad时,输入 sudo apt install leafpad 后出现了如下的错误提示: E: 无法获得锁 /var/lib/dpkg/lock - open ( ...

  4. SQL注入简单介绍

    一.SQL注入概念   1.sql注入是一种将sql代码添加到输入参数中   2.传递到sql服务器解析并执行的一种攻击手法   举例:某个网站的用户名为name=‘admin’.执行时为select ...

  5. vue2.0组件通信小总结

    1.父组件->子组件 父组件 <parent> <child :child-msg="msg"></child>//这里必须要用 - 代替 ...

  6. 如何查看selenium的版本号

    方法一: 打开cmd,输入python >>> import selenium >>> help(selenium) Help on package seleniu ...

  7. Oracle解决Ora-01653无法扩展表空间问题

    先针对可能性1查看表空间使用情况 SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M) & ...

  8. substr函数的用法

    敲了几个demo,结果如下 substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('1234567890',0,5) :返回结果为 '12345' *从字符串第一个字符开始截 ...

  9. buffer_pool.go

    package nsqd import (     "bytes"     "sync" ) var bp sync.Pool func init() {    ...

  10. hibernate MTM 联合主键

    //适用于表里没有其他列,只有主键 //Course.java实体类 package com.tao.pojo; import java.util.HashSet; import java.util. ...