java orion ssh scp 使用案例
来源:互联网 发布:淘宝读书官方版 编辑:程序博客网 时间:2024/04/30 19:24
首先得先下载orion-ssh2-214.jar包。
把jar包加入你的classpath然后就可以使用了。
SSH的使用方法如下:
/** * 执行远程的封存脚本 * @param host * @param username * @param password */ public static List<String> runRomoteScript(String host, String username, String password, String cmd) throws Exception { List<String> result = new ArrayList<String>(); Connection conn = new Connection(host); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) throw new RuntimeException("权限不够"); Session sess = conn.openSession(); sess.execCommand(cmd); InputStream stdout = new StreamGobbler(sess.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); while (true) { String line = br.readLine(); if (line == null) break; System.out.println(line); result.add(line); } sess.close(); conn.close(); return result; }
从远程获取文件:
/* * 命令成功执行 */ public static final String SUCCESS = "SUCCESS"; /** * 从远程服务器下载文件到本地文件夹 * @param host * @param username * @param password * @param romoteFileName * @param localDir */ public static String getFileFromRemote(String host, String username, String password, String romoteFileName, String localDir) { String msg = SSHUtil.SUCCESS; try { Connection conn = new Connection(host); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) return "权限不够!"; File inputFile = new File(localDir); if(!inputFile.exists()) //如果文件夹不存在,则新建文件夹 { inputFile.mkdirs(); } SCPClient scpClient = conn.createSCPClient(); scpClient.get(romoteFileName, localDir); conn.close(); } catch (IOException e) { return "出现了IO错误!"; } return msg; }以上代码经测试,运行正常,请放心使用。
SCPClient.put方法可以把文件发送到远程服务器。
- java orion ssh scp 使用案例
- SSH,SCP使用技巧
- 使用ssh、scp不需要密码
- 使用ssh命令实现scp
- Mac下使用ssh、scp
- Mac下使用ssh、scp
- Mac下使用ssh、scp
- scp、ssh无密码使用
- 【Linux】使用SSH和SCP
- 在webstorm使用scp/ssh
- Oracle Orion使用
- 使用Ant进行ssh和scp操作
- 使用scp、ssh等不用输入密码
- 使用Ant进行ssh和scp操作
- ssh安装 和 scp命令 使用
- 使用scp、ssh等不用输入密码
- 使用ssh、scp时不再输入密码
- 虚拟机中ssh和scp的使用
- JAVA内存泄露
- POJ1416--Shredding Company
- 8051寻址方式
- git常用命令
- 怎么安装口福科技产品
- java orion ssh scp 使用案例
- Linux卸载系统自带的httpd的方法
- 《30岁前每一天》 读书心得体会
- Android 开发环境搭建和基本的使用
- iOS 用cocos2d创建按钮
- 5、王爽《汇编语言》笔记_实验7
- Cocos2d-x-html5 之 HelloWorld 深入分析与调试
- 对话框及窗口背景色
- 关于链表的一些面试题