導航:首頁 > 編程語言 > 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交換機相關的資料

熱點內容
滴滴號碼加密怎麼解除 瀏覽:844
模具編程的職責 瀏覽:941
華為ssh改加密演算法 瀏覽:147
文件夾空白合同 瀏覽:761
pythonwebpy開發 瀏覽:669
不是c編譯器的有 瀏覽:660
win10壓縮包下載 瀏覽:905
逆戰手機app怎麼樣 瀏覽:946
自嗨自我解壓圖片 瀏覽:395
電子書導入kindle哪個文件夾 瀏覽:418
pythontcpserver性能 瀏覽:544
linux文件夾改名 瀏覽:564
單片機開發板是什麼 瀏覽:851
阿里雲伺服器不能截屏 瀏覽:866
如何自己製作聯想伺服器 瀏覽:843
停車場規劃演算法 瀏覽:923
深蹲PDF 瀏覽:908
數據科學包python 瀏覽:849
程序員學習視頻網站 瀏覽:126
吃雞游戲如何安卓轉蘋果 瀏覽:188