javassh登陆交换机执行命令
答案:1 悬赏:20 手机版
解决时间 2021-12-19 05:38
- 提问者网友:孤山下
- 2021-12-18 21:58
有人写过java ssh登陆交换机操作命令吗,不是telent也不是ssh下发linux指令 是ssh登陆交换机执行命令
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-12-18 22:04
第一步下载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
第二步:解压文件把 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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯