zoukankan      html  css  js  c++  java
  • J2SSH

    import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 import java.io.OutputStream;
     5 
     6 import com.sshtools.j2ssh.SshClient;
     7 import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
     8 import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
     9 import com.sshtools.j2ssh.session.SessionChannelClient;
    10 
    11 public class Main {
    12 
    13     public static void main(String[] args) {
    14         SshClient ssh = new SshClient();
    15         PasswordAuthenticationClient authentication = new PasswordAuthenticationClient();
    16         authentication.setUsername("root");
    17         authentication.setPassword("123");
    18         try {
    19             ssh.connect("192.168.94.254"22new HostsKeyVerification());
    20             if (ssh.authenticate(authentication) == AuthenticationProtocolState.COMPLETE) {
    21                 SessionChannelClient session = ssh.openSessionChannel();
    22                 // session.setEnvironmentVariable("TERM", "linux");
    23                 // if (client.requestPseudoTerminal("vt100", 120, 400, 0, 0,
    24                 // "")) {
    25                 if (session.startShell()) {
    26                     OutputStream writer = session.getOutputStream();
    27                     writer.write("echo $?\n".getBytes());
    28                     writer.flush();
    29                     writer.write("exit\n".getBytes());
    30                     writer.flush();
    31                     BufferedReader in = new BufferedReader(
    32                             new InputStreamReader(session.getInputStream()));
    33                     BufferedReader err = new BufferedReader(
    34                             new InputStreamReader(session
    35                                     .getStderrInputStream()));
    36                     String line;
    37                     while ((line = in.readLine()) != null) {
    38                         System.out.println(line);
    39                     }
    40                     System.out.println("------------------------");
    41                     while ((line = err.readLine()) != null) {
    42                         System.out.println(line);
    43                     }
    44                     if (session != null) {
    45                         session.close();
    46                     }
    47                 }
    48                 // }
    49             }
    50         } catch (IOException e) {
    51             e.printStackTrace();
    52         } finally {
    53         }
    54 
    55     }
    56 
    57 }

  • 相关阅读:
    K-Means算法总结
    C#设计模式(1)——单例模式
    sql 建表以及查询---复杂查询之成绩排名
    c# 折半查找法实现代码
    c# 合并两个有序数组
    C#中static void Main(string[] args)的含义
    C#中Main函数为什么要static
    C# 生成订单号的几种方式
    sql for xml path用法
    web端跨域调用webapi
  • 原文地址:https://www.cnblogs.com/ylqmf/p/2567904.html
Copyright © 2011-2022 走看看