XZC.Log
BlogJava
首頁(yè)
新隨筆
聯(lián)系
聚合
管理
隨筆-314 評(píng)論-209 文章-0 trackbacks-0
Java通過Telnet連接執(zhí)行shell腳本
/**
* commons-net-2.0.jar是工程依賴包
*/
package
telnet2;
import
java.io.InputStream;
import
java.io.PrintStream;
import
org.apache.commons.net.telnet.TelnetClient;
/**
* @descript NetTelenet.java
* @author sinclair
* @date Jun 10, 2010
*/
public
class
NetTelnet {
private
TelnetClient telnet =
new
TelnetClient();
private
InputStream in;
private
PrintStream out;
private
char
prompt =
'$'
;
// 普通用戶結(jié)束
public
NetTelnet( String ip,
int
port, String user, String password ) {
try
{
telnet.connect( ip, port );
in = telnet.getInputStream();
out =
new
PrintStream( telnet.getOutputStream() );
// 根據(jù)root用戶設(shè)置結(jié)束符
this
.prompt = user.equals(
"root"
) ?
'#'
:
'$'
;
login( user, password );
}
catch
( Exception e ) {
e.printStackTrace();
}
}
/**
* 登錄
*
* @param user
* @param password
*/
public
void
login( String user, String password ) {
readUntil(
"login:"
);
write( user );
readUntil(
"Password:"
);
write( password );
readUntil( prompt +
" "
);
}
/**
* 讀取分析結(jié)果
*
* @param pattern
* @return
*/
public
String readUntil( String pattern ) {
try
{
char
lastChar = pattern.charAt( pattern.length() -
1
);
StringBuffer sb =
new
StringBuffer();
char
ch = (
char
) in.read();
while
(
true
) {
sb.append( ch );
if
(ch == lastChar) {
if
(sb.toString().endsWith( pattern )) {
return
sb.toString();
}
}
ch = (
char
) in.read();
}
}
catch
( Exception e ) {
e.printStackTrace();
}
return
null
;
}
/**
* 寫操作
*
* @param value
*/
public
void
write( String value ) {
try
{
out.println( value );
out.flush();
}
catch
( Exception e ) {
e.printStackTrace();
}
}
/**
* 向目標(biāo)發(fā)送命令字符串
*
* @param command
* @return
*/
public
String sendCommand( String command ) {
try
{
write( command );
return
readUntil( prompt +
" "
);
}
catch
( Exception e ) {
e.printStackTrace();
}
return
null
;
}
/**
* 關(guān)閉連接
*/
public
void
disconnect() {
try
{
telnet.disconnect();
}
catch
( Exception e ) {
e.printStackTrace();
}
}
public
static
void
main( String[] args ) {
try
{
System.out.println(
"啟動(dòng)Telnet..."
);
String ip =
"192.168.0.11"
;
int
port =
23
;
String user =
"user"
;
String password =
"111111"
;
NetTelnet telnet =
new
NetTelnet( ip, port, user, password );
telnet.sendCommand(
"export LANG=en"
);
String r1 = telnet.sendCommand(
"cd /home/project/"
);
String r2 = telnet.sendCommand(
"pwd"
);
String r3 = telnet.sendCommand(
"sh a.sh"
);
System.out.println(
"顯示結(jié)果"
);
System.out.println( r1 );
System.out.println( r2 );
System.out.println( r3 );
telnet.disconnect();
}
catch
( Exception e ) {
e.printStackTrace();
}
}
}
posted on 2010-09-29 15:56
xzc
閱讀(4438)
評(píng)論(0)
編輯
收藏
所屬分類:
Java
、
linux/unix
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
Kerberos簡(jiǎn)介
快使用阿里云的maven倉(cāng)庫(kù)
JAVA對(duì)于數(shù)字證書的常用操作方法
DER 和 PEM 格式
深入理解 Java中的 流 (Stream)
JAVA 命令參數(shù)詳解:-D
靜態(tài)html文件js讀取url參數(shù)
Java Cookie 中文亂碼解決方法
web.xml中l(wèi)oad-on-startup的作用
java keytool 使用總結(jié)(轉(zhuǎn))
<
2010年9月
>
日
一
二
三
四
五
六
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(12)
給我留言
查看公開留言
查看私人留言
隨筆分類
BPM(1)
Design(5)
Eclipse(2)
hadoop(34)
Hibernate(1)
Java(55)
linux/unix(70)
Log4j(5)
Maven(4)
mysql(4)
Oracle(73)
Other(10)
python(3)
Spring(12)
Struts(1)
SybaseIQ(3)
TDD(2)
UML(2)
Web(12)
Xdoclet(8)
XML(17)
隨筆檔案
2018年11月 (3)
2018年9月 (1)
2018年5月 (1)
2018年3月 (2)
2017年10月 (3)
2017年9月 (4)
2017年8月 (3)
2017年7月 (3)
2017年5月 (4)
2017年4月 (6)
2016年9月 (2)
2016年4月 (1)
2016年3月 (3)
2016年2月 (2)
2015年12月 (7)
2015年11月 (7)
2015年10月 (4)
2015年9月 (10)
2015年8月 (1)
2015年7月 (1)
2015年6月 (1)
2015年4月 (1)
2015年2月 (3)
2015年1月 (5)
2014年12月 (3)
2014年9月 (2)
2013年10月 (1)
2013年2月 (1)
2012年8月 (2)
2012年7月 (1)
2012年6月 (1)
2012年3月 (3)
2011年12月 (2)
2011年11月 (1)
2011年10月 (1)
2011年9月 (2)
2011年8月 (4)
2011年7月 (6)
2011年6月 (4)
2011年5月 (2)
2011年4月 (4)
2011年3月 (8)
2011年1月 (3)
2010年12月 (9)
2010年11月 (3)
2010年10月 (1)
2010年9月 (5)
2010年8月 (6)
2010年7月 (6)
2010年6月 (2)
2010年4月 (7)
2010年3月 (9)
2010年2月 (1)
2010年1月 (2)
2009年12月 (3)
2009年10月 (1)
2009年9月 (3)
2009年8月 (5)
2009年6月 (4)
2009年1月 (1)
2008年11月 (7)
2008年10月 (2)
2008年9月 (1)
2008年6月 (7)
2008年5月 (7)
2007年12月 (1)
2007年11月 (4)
2007年10月 (3)
2007年9月 (3)
2007年7月 (1)
2007年6月 (2)
2007年3月 (1)
2007年2月 (1)
2006年12月 (5)
2006年11月 (23)
2006年10月 (18)
2006年9月 (16)
2006年8月 (11)
2006年6月 (3)
2006年4月 (1)
收藏夾
xzc(12)
搜索
最新評(píng)論
1.?re: Hive中reduce個(gè)數(shù)設(shè)定
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--xzc
2.?re: shell時(shí)間處理、加減、以及時(shí)間差
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--xzc
3.?re: Shell字符串比較相等、不相等方法小結(jié)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--xzc
4.?re: shell判斷文件是否存在
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--xzc
5.?re: curl模擬http發(fā)送get或post接口測(cè)試
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--xzc
閱讀排行榜
1.?web.xml中l(wèi)oad-on-startup的作用(149955)
2.?Oracle中start with...connect by子句的用法 (49723)
3.?數(shù)據(jù)庫(kù)設(shè)計(jì)準(zhǔn)則(第一、第二、第三范式說明)(44871)
4.?Oracle閃回查詢恢復(fù)delete刪除數(shù)據(jù)(27804)
5.?shell 判斷字符串是否存在包含關(guān)系(26442)
評(píng)論排行榜
1.?web.xml中l(wèi)oad-on-startup的作用(22)
2.?數(shù)據(jù)庫(kù)設(shè)計(jì)準(zhǔn)則(第一、第二、第三范式說明)(17)
3.?rowid 刪除重復(fù)記錄!!! (8)
4.?oracle日期處理完全版(8)
5.?Oracle SQL 內(nèi)置函數(shù)大全(6)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 xzc
主站蜘蛛池模板:
大同市
|
滨海县
|
上饶县
|
镇赉县
|
拜城县
|
永昌县
|
吉林市
|
崇左市
|
彭阳县
|
托克托县
|
平阳县
|
济宁市
|
婺源县
|
佛学
|
天祝
|
宿州市
|
五原县
|
乌海市
|
吴堡县
|
宾阳县
|
南投市
|
哈巴河县
|
镶黄旗
|
招远市
|
龙海市
|
三亚市
|
克什克腾旗
|
永仁县
|
桓台县
|
怀安县
|
平度市
|
徐水县
|
浏阳市
|
满城县
|
青河县
|
五峰
|
旌德县
|
宁乡县
|
江油市
|
岑溪市
|
玛曲县
|