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

熱點內容
java繼承類實例 瀏覽:286
疫情期間程序員的工資 瀏覽:27
最好的python編譯器 瀏覽:187
安卓手機如何調分屏 瀏覽:729
安卓系統藍牙耳機如何用 瀏覽:719
為什麼微信不能給appstore充值 瀏覽:495
程序員的保護動物 瀏覽:274
程序員遇到問題去哪個網站 瀏覽:531
安卓手機空格鍵連續輸入怎麼取消 瀏覽:520
壓縮空氣管道流量計 瀏覽:564
ug編程高級教程 瀏覽:177
什麼叫做伺服器已滿 瀏覽:37
暑假哪有教演算法的 瀏覽:136
密碼學的根基是加密 瀏覽:662
stata方差檢驗命令 瀏覽:337
解壓後文件夾里的內容丟失 瀏覽:715
解壓無敵視頻 瀏覽:690
什麼是伺服器辨認不了 瀏覽:129
java如何調用類方法 瀏覽:483
管理孩子的app叫什麼 瀏覽:546