• 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 }

  • 相关阅读:
    安全机制
    Service
    ubuntu 16.4 安装配置IK6.3.2
    ubuntu openstack windows 镜像制作
    openstack RuntimeError: Unable to create a new session key. It is likely that the cache
    千帆过尽,野草依然
    shiro无法进入授权的方法org.crazycake.shiro.exception.PrincipalInstanceException: class java.util.HashMap must has getter for field: id
    绝望中的希望
    shrio中去掉 login;JSESSIONID
    method 'redisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError
  • 原文地址:https://www.cnblogs.com/ylqmf/p/2567904.html
Copyright © 2020-2023  润新知