「java登录ssh」java登录页面代码

博主:adminadmin 2022-11-27 09:01:06 55

今天给各位分享java登录ssh的知识,其中也会对java登录页面代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何使用java通过ssh的方式登录远程服务器执行命令并返回结果

让用户输入要执行的命令,接收端会认为它是密码,应该会提示密码错误之类的吧?获取这个信息,提醒用户输入密码。

如何使用java通过ssh的方式登录远程服务器执行命令

mina。apache。org/sshd-project/如何使用java通过ssh的方式登录远程服务器执行命令

java ssh登陆交换机执行命令

第一步下载java扩展包

第二步:解压文件把 jar包,拷贝到java的库目录下,我的是ubuntu14(/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext )

cp ganymed-ssh2-build210.jar /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext

第三步: 把默认账户修改成直接的账户密码

yang@yang:~/Downloads/java/ganymed-ssh2-build210/examples$ cat Basic.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

import ch.ethz.ssh2.StreamGobbler;

public class Basic

{

public static void main(String[] args)

{

String hostname = "127.0.0.1";

String username = "user";

String password = "password";

try

{

Connection conn = new Connection(hostname);

conn.connect();

boolean isAuthenticated = conn.authenticateWithPassword(username, password);

if (isAuthenticated == false)

throw new IOException("Authentication failed.");

Session sess = conn.openSession();

sess.execCommand("uname -a date uptime who");

System.out.println("Here is some information about the remote host:");

InputStream stdout = new StreamGobbler(sess.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true)

{

String line = br.readLine();

if (line == null)

break;

System.out.println(line);

}

System.out.println("ExitCode: " + sess.getExitStatus());

sess.close();

conn.close();

}

catch (IOException e)

{

e.printStackTrace(System.err);

System.exit(2);

}

}

}

第四步: 编译 javac Basic.java java Basic

Here is some information about the remote host:

Linux yang 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

2014年 05月 03日 星期六 17:26:00 CST

17:26:00 up 6:09, 4 users, load average: 0.15, 0.21, 0.23

yang :0 2014-05-03 11:17 (:0)

yang pts/2 2014-05-03 16:34 (:0)

yang pts/26 2014-05-03 17:13 (:0)

yang pts/27 2014-05-03 17:24 (:0)

ExitCode: 0

关于java登录ssh和java登录页面代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。