使用java代码批量创建CRT会话

工作中,有时候需要管理上百台服务器或者交换机,需要在CRT上创建session,需要配置相同的logon Actions.
手动创建太费事,可以先创建一个模板,然后进行复制文件,然后修改IP即可.
以下代码就实现了这个过程.

package com.dyz.crtcopy;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 根据给定的IP列表信息,自动创建CRT文件
 *
 * @author dingyz
 */
public class AutoCreateCrtSession {
    /**
     * 创建文件的目的目录
     */
    private String path;

    /**
     * 参考的模板文件名称
     * 模板文件包含一些共性信息,包括账号和密码.以及登录后自动执行的操作
     *
     * @param args
     */
    private String templateName;
    /**
     * IP列表路径
     */
    private String ipListPath;

    /**
     * 存储模板文件内容的缓冲区
     *
     * @param args
     */
    private StringBuffer buffer = new StringBuffer();
    /**
     * 用于存储IP列表
     */
    private List<String> ipList = new ArrayList<>();
    /**
     * 要替换的关键字
     */
    private String ip;

    /**
     * 构造方法
     *
     * @param path         路径
     * @param templateName 模板
     */
    public AutoCreateCrtSession(String path, String templateName, String ipListPath) {
        this.path = path;
        this.templateName = templateName;
        this.ipListPath = ipListPath;
        init();
    }

    /**
     * 初始化
     * 1.将需要创建的IP列表加载到list
     * 2.将模板内容写到buffer中
     * 3.提取buffer中IP的关键字,创建其他文件的时候,进行替换
     */
    public void init() {
        File ipListFile = new File(ipListPath);
        BufferedReader br = null;
        try {
            //读取IP信息,到list中
            br = new BufferedReader(new FileReader(ipListFile));
            String line;
            while (null != (line = br.readLine())) {
                ipList.add(line);
            }
            //读取模板内容
            br = new BufferedReader(new FileReader(path + "/" + templateName));
            while (null != (line = br.readLine())) {
                if (line.startsWith("S:\"Hostname\"=")) {
                    ip = line;
                }
                buffer.append(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }

    /**
     * 从模板拷贝文件
     * 文件名为IP,后缀为ini
     *
     * @param args
     */
    public void copyFile() throws IOException {
        for (String s : ipList) {
            //创建一个临时的buffer,因为要对buffer进行内容替换,每次都要用到一样的buffer
            StringBuffer bufferTemp = new StringBuffer(buffer.toString());
            //创建文件名
            File file = new File(path + "/" + s + ".ini");
            if (!file.exists()) {
                file.createNewFile();
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            String result = "S:\"Hostname\"=" + s;
            bufferTemp.replace(bufferTemp.indexOf(ip),
                    bufferTemp.indexOf(ip) + ip.length(), result);
            bw.write(bufferTemp.toString());
            bw.flush();
            bw.close();
        }
    }

    public static void main(String[] args) {
        String ipListPath = "C:\\Users\\thinkpad T440S\\Desktop\\iplist.txt";
        String path = "D:\\dyz\\VanDyke Software\\VanDyke Software\\Config\\Sessions\\NBU安装";
        String template = "37.ini";
        AutoCreateCrtSession autoCreateCrtSession = new AutoCreateCrtSession(path, template, ipListPath);
        try {
            autoCreateCrtSession.copyFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43354959/article/details/90200943
crt