教你用页面操作liunx下令

代码 代码 1456 人阅读 | 0 人回复

<
简介

序言: liunx号令是我们每个教编程的小火伴皆需求把握的一个课程,关于没有会liunx号令的小火伴来讲是一个很头痛的一件事,以是我们能够经由过程java毗邻centos效劳器停止liunx号令的操作,做一些可视化的界里协助没有会liunx号令的小火伴处理那些成绩,大要我们也能够经由过程可视化页里削减我们的操作。
java毗邻效劳器

Maven
  1. <dependency>
  2.     <groupId>ch.ethz.ganymed</groupId>
  3.     <artifactId>ganymed-ssh2</artifactId>
  4.     <version>262</version>
  5. </dependency>
复造代码
java代码
  1. public class LiunxUtil {
  2.     public static String execute(String ip,String username,String password, String cmd) {
  3.         try {
  4.             // 创立毗连
  5.             Connection conn = new Connection(ip, 22);
  6.             // 启动毗连
  7.             conn.connect();
  8.             // 考证用户暗码
  9.             conn.authenticateWithPassword(username, password);
  10.             Session session = conn.openSession();
  11.             //施行号令
  12.             session.execCommand(cmd);
  13.             session = conn.openSession();
  14.             //施行号令
  15.             session.execCommand(cmd);
  16.             InputStream stdout = new StreamGobbler(session.getStdout());
  17.             BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
  18.             StringBuffer buffer = new StringBuffer();
  19.             String line;
  20.             while ((line = br.readLine()) != null) {
  21.                 buffer.append(line + "\n");
  22.             }
  23.             String result = buffer.toString();
  24.             System.out.println(result);
  25.             session.close();
  26.             conn.close();
  27.             //假如出有非常,返回成果为号令施行成果,假如有非常,返回null
  28.             return result;
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.             return null;
  32.         }
  33.     }
  34. }
复造代码
施行代码
  1. public static void main(String[] args) {
  2.         String execute = LiunxUtil.execute(host,userName,password,"ls");
  3.         System.out.println(execute);
  4.     }
复造代码
施行成果
094644aiy9y1gyc29oyooy.jpg

centos效劳器的文件上传战下载

东西包
[code]public class FtpUtil {    private static final Logger loggerMonitor = LoggerFactory.getLogger("monitor");    /**     * FTPClient工具     **/    private static ChannelSftp ftpClient = null;    /**     *     */    private static Session sshSession = null;    /**     * 毗邻效劳器     *     * @param host     * @param port     * @param userName     * @param password     * @return     * @throws Exception     */    public static ChannelSftp getConnect(String host, String port, String userName, String password)            throws Exception {        try {            JSch jsch = new JSch();            // 获得sshSession            sshSession = jsch.getSession(userName, host, Integer.parseInt(port));            // 增加s密码            sshSession.setPassword(password);            Properties sshConfig = new Properties();            sshConfig.put("StrictHostKeyChecking", "no");            sshSession.setConfig(sshConfig);            // 开启sshSession链接            sshSession.connect();            // 获得sftp通讲            ftpClient = (ChannelSftp) sshSession.openChannel("sftp");            // 开启            ftpClient.connect();            loggerMonitor.debug("success ..........");        } catch (Exception e) {            e.printStackTrace();            throw new Exception("毗邻sftp效劳器非常。。。。。。。。");        }        return ftpClient;    }    /**     * 下载文件     *     * @param ftp_path    效劳器文件途径     * @param save_path   下载保存途径     * @param oldFileName 效劳器上文件名     * @param newFileName 保存后新文件名     * @throws Exception     */    public static void download(String ftp_path, String save_path, String oldFileName, String newFileName)            throws Exception {        FileOutputStream fos = null;        try {            ftpClient.cd(ftp_path);            File file = new File(save_path);            if (!file.exists()) {                file.mkdirs();            }            String saveFile = save_path + newFileName;            File file1 = new File(saveFile);            fos = new FileOutputStream(file1);            ftpClient.get(oldFileName, fos);        } catch (Exception e) {            loggerMonitor.error("下载文件非常............", e.getMessage());            throw new Exception("download file error............");        } finally {            if (fos != null) {                try {                    fos.close();                } catch (Exception e) {                    e.printStackTrace();                    throw new Exception("close stream error..........");                }            }        }    }    /**     * 上传     *     * @param upload_path 上传文件途径     * @param ftp_path    效劳器保存途径     * @param newFileName 新文件名     * @throws Exception     */    public static void uploadFile(String upload_path, String ftp_path, String newFileName) throws Exception {        FileInputStream fis = null;        try {            fis = new FileInputStream(new File(upload_path));            ftpClient.cd(ftp_path);            ftpClient.put(fis, newFileName);        } catch (Exception e) {            e.printStackTrace();            throw new Exception("Upload file error.");        } finally {            if (fis != null) {                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                    throw new Exception("close stream error.");                }            }        }    }    /**     * 封闭     *     * @throws Exception     */    public static void close() throws Exception {        loggerMonitor.debug("close............");        try {            ftpClient.disconnect();            sshSession.disconnect();        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();            throw new Exception("close stream error.");        }    }    /**     * 遍历文件夹里的文件     * @file 文件夹途径     * @fileList 存储文件名的汇合     */    public static ArrayList isDirectory(File file, ArrayList fileList) {        if(file.exists()){            if (file.isFile()) {                int start = file.getAbsolutePath().lastIndexOf("\\")+1;                int end = file.getAbsolutePath().length();                fileList.add(file.getAbsolutePath().substring(start,end));            }else{                File[] list = file.listFiles();                if (list.length == 0) {                    System.out.println(file.getAbsolutePath() + " is null");                } else {                    for (int i = 0; i <span class="token operator">
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则