176142998

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

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;
 }

 public void setServerPwd(String serverPwd) {
  this.serverPwd = serverPwd;
 }

 public int getServerPort() {
  return serverPort;
 }

 public void setServerPort(int serverPort) {
  this.serverPort = serverPort;
 }

 public String getFtpIp() {
  return ftpIp;
 }

 public void setFtpIp(String ftpIp) {
  this.ftpIp = "ftp " + ftpIp;
 }

 public String getFtpUser() {
  return ftpUser;
 }

 public void setFtpUser(String ftpUser) {
  this.ftpUser = ftpUser;
 }

 public String getFtpPwd() {
  return ftpPwd;
 }

 public void setFtpPwd(String ftpPwd) {
  this.ftpPwd = ftpPwd;
 }

 public String getFtpPort() {
  return ftpPort;
 }

 public void setFtpPort(String ftpPort) {
  this.ftpPort = ftpPort;
 }

}



package com.org.softwore.service;

public interface TelnetService {

 public boolean windowsTelnetDownLoad();

 public boolean windowsTelnetUpLoad();

 public boolean windowsTelnetUpGrade();
 
 public boolean windowsTelnetTomcat(String stats);
 
 public boolean windowsTelnetBackUpSoftware();
}



package com.org.softwore.service.impl;

import java.io.IOException;
import java.net.SocketException;
import java.util.TreeMap;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.org.softwore.domain.TelnetInfo;
import com.org.softwore.service.TelnetService;
import com.org.softwore.util.TelnetUtil;

@Transactional
@Service("telnetService")
public class TelnetServiceImpl implements TelnetService {

 /**
  * 上传升级问价到ftp服务器
  */
 @Override
 public boolean windowsTelnetUpLoad() {
  boolean reslut = false;

  TelnetInfo telnetInfo = new TelnetInfo();
  telnetInfo.setServerIp("127.0.0.1");
  telnetInfo.setServerUser("administrator");
  telnetInfo.setServerPwd("123456");
  telnetInfo.setServerPort(23);

  TreeMap<Integer, String> map = new TreeMap<Integer, String>();
  map.put(1, "ftp 10.39.62.45");
  map.put(2, "admin");
  map.put(3, "admintianyi");
  map.put(4, "bin");
  map.put(5, "mkdir test1");
  map.put(6, "cd /test1");
  map.put(7, "put c:\\upgrade\\appMarketWebApi.zip");
  map.put(8, "put c:\\upgrade\\T_RANKSWITCHTEMP.sql");
  map.put(9, "bye");
  telnetInfo.setMap(map);

  TelnetCmd(telnetInfo);
  return reslut;
 }
 
 /**
  * 下载升级程序和文件
  */
 @Override
 public boolean windowsTelnetDownLoad() {
  boolean reslut = false;

  TelnetInfo telnetInfo = new TelnetInfo();
  //telnet
  telnetInfo.setServerIp("127.0.0.1");
  telnetInfo.setServerUser("administrator");
  telnetInfo.setServerPwd("123456");
  telnetInfo.setServerPort(23);

  TreeMap<Integer, String> map = new TreeMap<Integer, String>();
  map.put(1, "cd c:\\");
  map.put(2, "rd test /s /q");
  map.put(3, "mkdir test");
  map.put(4, "ftp 10.39.62.45");
  map.put(5, "admin");
  map.put(6, "admintianyi");
  map.put(7, "bin");
  map.put(8, "lcd c:\\test");
  map.put(9, "cd test1");
  map.put(10, "get appMarketWebApi.zip");
  map.put(11, "get T_RANKSWITCHTEMP.sql");
  map.put(12, "bye");
  telnetInfo.setMap(map);

  TelnetCmd(telnetInfo);
  return reslut;
 }
 
 /**
  * 服务器开启和关闭
  */
 @Override
 public boolean windowsTelnetTomcat(String stats) {
  //安装tomcat7,使用服务来进行启动和停止
  boolean reslut = false;

  TelnetInfo telnetInfo = new TelnetInfo();
  telnetInfo.setServerIp("127.0.0.1");
  telnetInfo.setServerUser("administrator");
  telnetInfo.setServerPwd("123456");
  telnetInfo.setServerPort(23);

  // 需要执行的sql语句
  TreeMap<Integer, String> map = new TreeMap<Integer, String>();
  if(stats.equals("start")){
   map.put(1, "net start Tomcat7");
  }
  if(stats.equals("stop")){
   map.put(1, "net stop Tomcat7");
  }
  telnetInfo.setMap(map);

  TelnetCmd(telnetInfo);
  return reslut;
 }
 
 /**
  * 备份软件程序
  */
 @Override
 public boolean windowsTelnetBackUpSoftware(){
  boolean reslut = false;
  
  TelnetInfo telnetInfo = new TelnetInfo();
  telnetInfo.setServerIp("127.0.0.1");
  telnetInfo.setServerUser("administrator");
  telnetInfo.setServerPwd("123456");
  telnetInfo.setServerPort(23);
  
  // 需要执行的sql语句
  TreeMap<Integer, String> map = new TreeMap<Integer, String>();
  //执行sql脚本
  map.put(1, "cd c:\\");
  map.put(2, "rd testback /s /q");
  map.put(3, "mkdir testback");
  map.put(4, "cd c:\\testback");
  map.put(5, "mkdir appMarketWebApi");
  map.put(6, "xcopy \"c:\\Tomcat 7.0\\webapps\\appMarketWebApi\"  appMarketWebApi /s /y");
  telnetInfo.setMap(map);
  
  TelnetCmd(telnetInfo);
  
  return reslut;
  
 }
 
 
 /**
  * 执行sql脚本和升级程序
  */
 @Override
 public boolean windowsTelnetUpGrade() {
  boolean reslut = false;

  TelnetInfo telnetInfo = new TelnetInfo();
  telnetInfo.setServerIp("127.0.0.1");
  telnetInfo.setServerUser("administrator");
  telnetInfo.setServerPwd("123456");
  telnetInfo.setServerPort(23);

  // 需要执行的sql语句
  TreeMap<Integer, String> map = new TreeMap<Integer, String>();
  //执行sql脚本
  map.put(1, "sqlplus appstore/appstore@appstore226");
  map.put(2, "@c:\\test\\T_RANKSWITCHTEMP.sql");
  map.put(3, "exit");
  
  //执行升级程序
  map.put(5, "cd c:\\test");
  map.put(6, "unzip appMarketWebApi.zip");
  map.put(7, "xcopy appMarketWebApi \"c:\\Tomcat 7.0\\webapps\\appMarketWebApi\" /s /y");
  
  telnetInfo.setMap(map);

  TelnetCmd(telnetInfo);
  return reslut;
 }

 /**
  * 公共的调用执行telnet命令
  * @param telnetInfo
  * @return
  */
 public boolean TelnetCmd(TelnetInfo telnetInfo) {
  boolean reslut = false;
  if (telnetInfo != null) {
   TelnetUtil telnet = new TelnetUtil();
   try {
    telnet.TelnetCmd(telnetInfo);
    telnet.disconnect();
    reslut = true;
   } catch (SocketException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  return reslut;
 }
}


package com.org.softwore.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.org.softwore.service.TelnetService;

@Controller("telnetController")
public class TelnetController {

 @Autowired
 @Qualifier("telnetService")
 private TelnetService telnetService;

 /**
  * 上传升级问价到ftp服务器
  */
 public void windowsTelnetUpLoad() {
  telnetService.windowsTelnetUpLoad();
 }

 /**
  * 下载升级程序和文件
  */
 public void windowsTelnetDownLoad() {
  telnetService.windowsTelnetDownLoad();
 }

 /**
  * 备份软件程序
  */
 public void windowsTelnetBackUpSoftware() {
  telnetService.windowsTelnetBackUpSoftware();
 }

 /**
  * 服务关闭
  */
 public void windowsTelnetStopTomcat() {
  String stats = "stop";
  telnetService.windowsTelnetTomcat(stats);
 }

 /**
  * 执行sql脚本和升级程序
  */
 public void windowsTelnetUpGrade() {
  telnetService.windowsTelnetUpGrade();
 }

 /**
  * 服务开启
  */
 public void windowsTelnetStartTomcat() {
  String stats = "start";
  telnetService.windowsTelnetTomcat(stats);
 }
}



package com.org.softwore.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.org.softwore.controller.TelnetController;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/applicationContext.xml" })
public class TelnetUtilTest {

 @Autowired
 @Qualifier("telnetController")
 private TelnetController telnetController;

 /**
  * 上传升级问价到ftp服务器
  */
 @Test
 public void windowsTelnetUpLoad() {
  telnetController.windowsTelnetUpLoad();
 }

 /**
  * 下载升级程序和文件
  */
 @Test
 public void windowsTelnetDownLoad() {
  telnetController.windowsTelnetDownLoad();
 }

 /**
  * 备份软件程序
  */
 @Test
 public void windowsTelnetBackUpSoftware() {
  telnetController.windowsTelnetBackUpSoftware();
 }

 /**
  * 关闭服务
  */
 @Test
 public void windowsTelnetStopTomcat() {
  telnetController.windowsTelnetStopTomcat();
 }

 /**
  * 执行sql脚本和升级程序
  */
 @Test
 public void windowsTelnetUpGrade() {
  telnetController.windowsTelnetUpGrade();
 }

 /**
  * 启动服务
  */
 @Test
 public void windowsTelnetStartTomcat() {
  telnetController.windowsTelnetStartTomcat();
 }
}

posted on 2011-04-13 17:34 飞飞 阅读(2582) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: