日期:2014-05-16  浏览次数:21216 次

java实现telnet功能,待实现windows下远程多机自动化发布软件后台代码
java实现telnet功能,待实现windows下远程多机自动化发布软件后台代码

package com.org.softwore.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;

import org.apache.commons.net.telnet.TelnetClient;

import com.org.softwore.domain.TelnetInfo;

public class TelnetUtil {
?private TelnetClient telnet = new TelnetClient("VT220");
?private InputStream in;
?private PrintStream out;
?private String prompt = ">";
?String s;

?public void TelnetCmd(TelnetInfo telnetInfo) throws SocketException,
???IOException, InterruptedException {

??// 防火墙中高级的网络连接设置的本地连接取消选择
??// Connect to the specified server
??telnet.connect(telnetInfo.getServerIp(), telnetInfo.getServerPort());
??// Get input and output stream references
??in = telnet.getInputStream();
??out = new PrintStream(telnet.getOutputStream());

??// Login telnet
??readUntil("login: ");
??write(telnetInfo.getServerUser());
??readUntil("password: ");
??write(telnetInfo.getServerPwd());
??readUntil(prompt);

??// 执行命令
??if (telnetInfo.getMap() != null) {
???int a = 0;
???for (Integer id : telnetInfo.getMap().keySet()) {
????String temp = telnetInfo.getMap().get(id);
????write(temp);
????// 处理ftp登录,不需要执行readUntil(prompt)
????if (temp.startsWith("ftp")) {
?????a = 2;
????}
????if (a > 0) {
?????a = a - 1;
?????continue;
????} else {
?????readUntil(prompt);
????}

???}
??}
??System.out.println("\n");
?}

?public String readUntil(String pattern) throws IOException {
??char lastChar = pattern.charAt(pattern.length() - 1);
??StringBuffer sb = new StringBuffer();
??// boolean found = false;
??char ch = (char) in.read();

??while (true) {
???sb.append(ch);
???if (ch == lastChar) {
????if (sb.toString().endsWith(pattern)) {
?????// 处理编码,界面显示乱码问题
?????byte[] temp = sb.toString().getBytes("iso8859-1");
?????System.out.print(new String(temp, "GBK"));
?????return new String(temp, "GBK");
????}
???}
???ch = (char) in.read();
??}
?}

?public void write(String value) throws InterruptedException {
??out.println(value);
??out.flush();
??Thread.sleep(1000);
?}

?public void disconnect() throws IOException {
??telnet.disconnect();
?}
}


package com.org.softwore.domain;

import java.io.Serializable;
import java.util.TreeMap;

public class TelnetInfo implements Serializable {

?/**
? *?
? */
?private static final long serialVersionUID = -6592407260449335815L;
?private String serverIp;
?private String serverUser;
?private String serverPwd;
?private int serverPort;
?private String ftpIp;
?private String ftpUser;
?private String ftpPwd;
?private String ftpPort;
?private TreeMap<Integer, String> map;

?public TreeMap<Integer, String> getMap() {
??return map;
?}

?public void setMap(TreeMap<Integer, String> map) {
??this.map = map;
?}

?public String getServerIp() {
??return serverIp;
?}

?public void setServerIp(String serverIp) {
??this.serverIp = serverIp;
?}

?public String getServerUser() {
??return serverUser;
?}

?public void setServerUser(String serverUser) {
??this.serverUser = serverUser;
?}

?public String getServerPwd() {
??return serverPwd;
?