此程序需要ganymed-ssh2-build210.jar包。
下載地址:
http://www.ganymed.ethz.ch/ssh2/
為了調試方便,可以將\ganymed-ssh2-build210\src下的代碼直接拷貝到我們的工程里,
此源碼的好處就是沒有依賴很多其他的包,拷貝過來干干凈凈。
此程序的目的是執行遠程機器上的Shell腳本。
遠程機器IP:***.**.**.***
用戶名:sshapp
密碼:sshapp
登錄后用pwd命令,顯示當前目錄為:/sshapp.
在/sshapp/myshell/目錄下有myTest.sh文件,內容如下:
public
?
class
?RmtShellExecutor?
{
????
????
????
private
?Connection?conn;
????
????
private
?String?????ip;
????
????
private
?String?????usr;
????
????
private
?String?????psword;
????
private
?String?????charset?
=
?Charset.defaultCharset().toString();
????
private
?
static
?
final
?
int
?TIME_OUT?
=
?
1000
?
*
?
5
?
*
?
60
;
????
????
public
?RmtShellExecutor(ShellParam?param)?
{
????????
this
.ip?
=
?param.getIp();
????????
this
.usr?
=
?param.getUsername();
????????
this
.psword?
=
?param.getPassword();
????}
????
????
public
?RmtShellExecutor(String?ip,?String?usr,?String?ps)?
{
????????
this
.ip?
=
?ip;
????????
this
.usr?
=
?usr;
????????
this
.psword?
=
?ps;
????}
????
????
private
?
boolean
?login()?
throws
?IOException?
{
????????conn?
=
?
new
?Connection(ip);
????????conn.connect();
????????
return
?conn.authenticateWithPassword(usr,?psword);
????}
????
????
public
?
int
?exec(String?cmds)?
throws
?Exception?
{
????????InputStream?stdOut?
=
?
null
;
????????InputStream?stdErr?
=
?
null
;
????????String?outStr?
=
?
""
;
????????String?outErr?
=
?
""
;
????????
int
?ret?
=
?
-
1
;
????????
try
?
{
????????????
if
?(login())?
{
????????????????
//
?Open?a?new?{@link?Session}?on?this?connection
????????????????Session?session?
=
?conn.openSession();
????????????????
//
?Execute?a?command?on?the?remote?machine.
????????????????session.execCommand(cmds);
????????????????
????????????????stdOut?
=
?
new
?StreamGobbler(session.getStdout());
????????????????outStr?
=
?processStream(stdOut,?charset);
????????????????
????????????????stdErr?
=
?
new
?StreamGobbler(session.getStderr());
????????????????outErr?
=
?processStream(stdErr,?charset);
????????????????
????????????????session.waitForCondition(ChannelCondition.EXIT_STATUS,?TIME_OUT);
????????????????
????????????????System.out.println(
"
outStr=
"
?
+
?outStr);
????????????????System.out.println(
"
outErr=
"
?
+
?outErr);
????????????????
????????????????ret?
=
?session.getExitStatus();
????????????}
?
else
?
{
????????????????
throw
?
new
?AppException(
"
登錄遠程機器失敗
"
?
+
?ip);?
//
?自定義異常類?實現略
????????????}
????????}
?
finally
?
{
????????????
if
?(conn?
!=
?
null
)?
{
????????????????conn.close();
????????????}
????????????IOUtils.closeQuietly(stdOut);
????????????IOUtils.closeQuietly(stdErr);
????????}
????????
return
?ret;
????}
????
????
private
?String?processStream(InputStream?in,?String?charset)?
throws
?Exception?
{
????????
byte
[]?buf?
=
?
new
?
byte
[
1024
];
????????StringBuilder sb?
=
?
new
?StringBuilder();
????????
while
?(in.read(buf)?
!=
?
-
1
)?
{
????????????sb.append(
new
?String(buf,?charset));
????????}
????????
return
?sb.toString();
????}
????
public
?
static
?
void
?main(String?args[])?
throws
?Exception?
{
????????RmtShellExecutor?exe?
=
?
new
?RmtShellExecutor(
"
***.**.**.***
"
,?
"ssh
app
"
,?
"sshapp
"
);
????????
//
?執行myTest.sh?參數為java?Know?dummy
????????System.out.println(exe.exec(
"
sh?/webapp/myshell/myTest.sh?java?Know?dummy
"
));
//
????????exe.exec("uname?-a?&&?date?&&?uptime?&&?who");
????}
}
?
此方法通過遠程命令取得Exit Code/status。但并不是每個server設計時都會返回這個值,如果沒有則會返回null。
在調用getExitStatus時,要先調用WaitForCondition方法,通過ChannelCondition.java接口的定義可以看到每個條件的具體含義
而getExitStatus的返回值,可以認為是此次執行是否OK的標準。
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
