介紹
本文介紹如何通過Telnet協議連接到遠程Shell,執行腳本,並獲取執行結果;
相關文章:
《【Jsch】使用SSH協議連接到遠程Shell執行腳本》
http://www.cnblogs.com/ssslinppp/p/6244653.html
其他示例:
maven倉庫
使用Apache Commons-net通用庫;
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.4</version>
</dependency>
《Apache Commons Net示例》
http://commons.apache.org/proper/commons-net/
包括:
- FTP/FTPS
- FTP over HTTP (experimental)
- NNTP
- SMTP(S)
- POP3(S)
- IMAP(S)
- Telnet
- TFTP
- Finger
- Whois
- rexec/rcmd/rlogin
- Time (rdate) and Daytime
- Echo
- Discard
- NTP/SNTP
- Backgr
具體步驟
- 步驟1: 使用TelnetClient創建連接:connect();
- 步驟2: 設置Telnet屬性:如 回顯選項/SUPPRESS GO AHEAD/終端類型等;
- 步驟3: 獲取輸入/輸出流:getInputStream()/getOutputStream();
- 步驟4: 使用username和password進行登錄;
- 步驟5: 執行Shell腳本,獲取執行結果;
- 步驟6: 關閉資源:輸入/輸出流,TelnetClient連接等;
程序
步驟1~步驟6
執行具體腳本
測試程序
完整程序
package com.sssppp.Communication;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
public class TelentCommUtil {
/**
* 測試程序
*
* @param args
*/
public static void main(String[] args) {
String ip = "10.180.137.221";
int port = 23;
String localIp = null;
int localPort = 0;
int timeOut = 3000;
String userName = "xxxxx";
String password = "xxxxx";
String[] cmds = new String[] { "ifconfig | grep eth0\n",
"cat /etc/redhat-release\n" };
String[] result = null;
try {
result = execShellCmdByTelnet(ip, port, localIp, localPort, timeOut,
userName, password, cmds);
} catch (Exception e) {
e.printStackTrace();
}
if (result != null) {
for (String string : result) {
System.out.println(string);
System.out.println("-------------------");
}
}
}
/**
* 使用Telnet協議,連接到Linux Shell,執行腳本命令,並獲取結果
*
* @param dstIp
* @param dstPort
* @param localIp
* @param localPort
* @param timeOut
* @param userName
* @param password
* @param cmds
* @return
* @throws Exception
*/
public static String[] execShellCmdByTelnet(String dstIp, int dstPort,
String localIp, int localPort, int timeOut, String userName,
String password, String... cmds) throws Exception {
TelnetClient tc = new TelnetClient();
InputStream is = null;
OutputStream os = null;
try {
//設置:RFC 1091 TELNET終端類型選項
tc.addOptionHandler(new TerminalTypeOptionHandler("VT100", false,
false, true, false));
//設置:RFC 857 TELNET ECHO 回顯選項
tc.addOptionHandler(new EchoOptionHandler(true, false, true, false));
//設置:RFC 858 TELNET SUPPRESS GO AHEAD(抑制繼續進行)選項
tc.addOptionHandler(new SuppressGAOptionHandler(true, true, true,
true));
tc.setConnectTimeout(timeOut);
if (localIp == null) {
tc.connect(dstIp, dstPort);
} else {
tc.connect(InetAddress.getByName(dstIp), dstPort,
InetAddress.getByName(localIp), localPort);
}
is = tc.getInputStream();
os = tc.getOutputStream();
//輸入用戶名和密碼
if (sendCommand(is, os, "\n").contains("login:")) {
if (sendCommand(is, os, userName + "\n").contains("assword:")) {
if (sendCommand(is, os, password + "\n").contains(
"incorrect")) {
throw new Exception("Auth error");
}
}
}
String[] result = new String[cmds.length];
for (int i = 0; i < cmds.length; i++) {
result[i] = sendCommand(is, os, cmds[i]);
}
return result;
} catch (SocketTimeoutException e) {
throw new Exception("SocketTimeoutException error");
} catch (Exception e) {
throw e;
} finally {
try {
is.close();
} catch (Exception e) {
}
try {
os.close();
} catch (Exception e) {
}
try {
tc.disconnect();
} catch (IOException e) {
}
}
}
/**
* 執行Shell命令,並獲取執行結果
*
* @param is
* @param os
* @param cmd
* @return
* @throws IOException
*/
private static String sendCommand(InputStream is, OutputStream os,
String cmd) throws IOException {
os.write(cmd.getBytes());
os.flush();
StringBuffer sb = new StringBuffer();
int beat = 0;
while (true) {
if (beat > 3) {
break;
}
if (is.available() > 0) {
byte[] b = new byte[is.available()];
is.read(b);
sb.append(new String(b));
beat = 0;
} else {
if (sb.length() > 0) {
beat++;
}
try {
Thread.sleep(sb.toString().trim().length() == 0 ? 1000
: 300);
} catch (InterruptedException e) {
}
}
}
return sb.toString();
}
}
