導航:首頁 > 編程語言 > java交換機

java交換機

發布時間:2023-07-06 08:53:39

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交換機相關的資料

熱點內容
如何將文件夾的內容發到微信 瀏覽:892
安卓手機emoji扁平怎麼辦 瀏覽:208
歐版三星s20怎麼升級安卓11 瀏覽:104
我的世界伺服器如何拉黑房子 瀏覽:480
linux伺服器網路設置 瀏覽:106
在線編譯代碼怎麼做 瀏覽:205
社會研究方法教程pdf 瀏覽:696
cad修改伺服器地址 瀏覽:788
新手伺服器裝什麼系統好 瀏覽:128
androidxmlui 瀏覽:713
星辰之戀在哪個app播 瀏覽:465
手機卡伺服器出錯誤是怎麼回事 瀏覽:536
cg編譯器怎麼設置 瀏覽:418
單片機教學視頻哪個好 瀏覽:598
編譯器錯誤了怎麼辦 瀏覽:77
看摩托車用什麼app好 瀏覽:405
pdf轉換excel在線轉換 瀏覽:361
php多客服 瀏覽:746
語言編譯程序如何分類 瀏覽:377
pdf下載哪個 瀏覽:77