|
<
简介
序言: liunx号令是我们每个教编程的小火伴皆需求把握的一个课程,关于没有会liunx号令的小火伴来讲是一个很头痛的一件事,以是我们能够经由过程java毗邻centos效劳器停止liunx号令的操作,做一些可视化的界里协助没有会liunx号令的小火伴处理那些成绩,大要我们也能够经由过程可视化页里削减我们的操作。
java毗邻效劳器
Maven
- <dependency>
- <groupId>ch.ethz.ganymed</groupId>
- <artifactId>ganymed-ssh2</artifactId>
- <version>262</version>
- </dependency>
复造代码 java代码
- public class LiunxUtil {
- public static String execute(String ip,String username,String password, String cmd) {
- try {
- // 创立毗连
- Connection conn = new Connection(ip, 22);
- // 启动毗连
- conn.connect();
- // 考证用户暗码
- conn.authenticateWithPassword(username, password);
- Session session = conn.openSession();
- //施行号令
- session.execCommand(cmd);
- session = conn.openSession();
- //施行号令
- session.execCommand(cmd);
- InputStream stdout = new StreamGobbler(session.getStdout());
- BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
- StringBuffer buffer = new StringBuffer();
- String line;
- while ((line = br.readLine()) != null) {
- buffer.append(line + "\n");
- }
- String result = buffer.toString();
- System.out.println(result);
- session.close();
- conn.close();
- //假如出有非常,返回成果为号令施行成果,假如有非常,返回null
- return result;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
复造代码 施行代码
- public static void main(String[] args) {
- String execute = LiunxUtil.execute(host,userName,password,"ls");
- System.out.println(execute);
- }
复造代码 施行成果
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、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
|