日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

java實(shí)現(xiàn)linux連接操作

系統(tǒng) 2779 0

此程序需要ganymed-ssh2-build210.jar包。
下載地址: http://www.ganymed.ethz.ch/ssh2/
為了調(diào)試方便,可以將\ganymed-ssh2-build210\src下的代碼直接拷貝到我們的工程里,
此源碼的好處就是沒有依賴很多其他的包,拷貝過來干干凈凈。

此程序的目的是執(zhí)行遠(yuǎn)程機(jī)器上的Shell腳本。
遠(yuǎn)程機(jī)器IP:***.**.**.***
用戶名:sshapp
密碼:sshapp
登錄后用pwd命令,顯示當(dāng)前目錄為:/sshapp.
在/sshapp/myshell/目錄下有myTest.sh文件,內(nèi)容如下:

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( " 登錄遠(yuǎn)程機(jī)器失敗 " ? + ?ip);? // ?自定義異常類?實(shí)現(xiàn)略
????????????}

????????}
? 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 " );
????????
// ?執(zhí)行myTest.sh?參數(shù)為java?Know?dummy
????????System.out.println(exe.exec( " sh?/webapp/myshell/myTest.sh?java?Know?dummy " ));
// ????????exe.exec("uname?-a?&&?date?&&?uptime?&&?who");
????}

}

?

注:一般情況下shell腳本正常執(zhí)行完畢,getExitStatus方法返回0。
此方法通過遠(yuǎn)程命令取得Exit Code/status。但并不是每個server設(shè)計(jì)時都會返回這個值,如果沒有則會返回null。
在調(diào)用getExitStatus時,要先調(diào)用WaitForCondition方法,通過ChannelCondition.java接口的定義可以看到每個條件的具體含義
在實(shí)際應(yīng)用中,可以將outStr和outErr記錄到日志中,以便維護(hù)人員查看shell的執(zhí)行情況,
而getExitStatus的返回值,可以認(rèn)為是此次執(zhí)行是否OK的標(biāo)準(zhǔn)。

?

java實(shí)現(xiàn)linux連接操作


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 翁牛特旗| 舒城县| 六安市| 高青县| 社会| 北京市| 霍林郭勒市| 涞水县| 鲁甸县| 谷城县| 即墨市| 方山县| 津南区| 余江县| 昌黎县| 黎城县| 会昌县| 辉县市| 台北县| 桐庐县| 乐陵市| 运城市| 新闻| 宜兰县| 大理市| 昔阳县| 固安县| 泰宁县| 衡东县| 文成县| 奉新县| 余江县| 金平| 昭觉县| 绩溪县| 隆德县| 勐海县| 邵东县| 定边县| 封丘县| 毕节市|