導航:首頁 > 編程語言 > javassh實例

javassh實例

發布時間:2024-06-22 16:51:05

A. java ssh欏圭洰錛岀粡榪囦竴涓猘ction錛屾庝箞鑳借煩杞鍒扮櫨搴﹂栭〉鍛錛燂紵錛燂紵

action閲宺eturn "" 鐒跺悗鍦ㄩ厤緗鏂囦歡<result name="" type="redirect">鐧懼害緗戝潃</result>

B. 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

閱讀全文

與javassh實例相關的資料

熱點內容
還款解壓再質押 瀏覽:967
doc2pdf 瀏覽:389
Android提示為空 瀏覽:620
sql預編譯和執行的語法 瀏覽:108
復盛壓縮機選型軟體 瀏覽:744
android培訓大綱 瀏覽:58
docker鏡像內編譯 瀏覽:859
繳納社保app哪個好 瀏覽:414
哪個app跑步有錢 瀏覽:56
貨幣理論pdf 瀏覽:424
python35鍵盤輸入 瀏覽:287
安卓手機頭部自定義怎麼取消 瀏覽:26
python套利交易 瀏覽:790
java配置環境變數win8 瀏覽:903
輸解壓密碼時候壓縮文件損壞 瀏覽:403
xampp如何運行php項目 瀏覽:905
哪個app團購電影票最便宜 瀏覽:623
國外如何給信件加密 瀏覽:167
哪個看書app免費書全 瀏覽:40
用php寫一段自我介紹的代碼 瀏覽:662