`
zzhangyongjie
  • 浏览: 9314 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java operate file or system between different service

阅读更多

 

设想:能不能在不同服务器之间文件传输,或是执行操作系统的命令的方式来操作服务器?

答案:当然可以

 

首先,在这里介绍一下不用密码怎么去访问别的service。(这是后续操作的基础)

 

 

要让 HostB 允许HostA 上的用户User1 进行操作,需要:

 

1. HostA 上以 User1 登陆,或 su - User1

2. 用ssh-keygen 命令,来生成HostA 的密钥对文件

ssh-keygen -t rsa 

生成目录:/home/User1/.ssh/

id_rsa 是私钥文件

id_rsa.pub 是公钥文件

3. 修改公钥文件属性

chmod 755 /home/User1/.ssh/id_rsa.pub

4. 将 HostA 上的公钥内容 id_rsa.pub 添加到 HostB 上  

如果HostB上没有  /home/User1/.ssh 目录或authorized_keys 则需要新建

将HostA 的 id_rsa.pub 文件内容,添加到 /home/User1/.ssh/authorized_keys 文件中

5. 测试:(注意需要以 User1 登陆)

scp  1.txt UserB@HostB:/****/xxxx/

 

在不需要密码的情况下能把1.txt文件从HostA  copy  至HostB,那么,该配置设置ok。

 

补充:有可能出现按照上述设置,任然不可以的现象,做如下操作:

在HostA上

chmod 700 .ssh

chmod 600 ./ssh/authorized_keys

 

 

其次,网上有很多java实现的ssh,比如jsch等。我们在这里介绍两种底层的操作,更加简明、高效

 

1. Runtime.getRuntime().exec()

 

我想大家对这个不陌生吧,Runtime这个东西很强大,只要是你的操作系统能够完成的,它都能完成。比如linux系统中我们常用的scp命令,跨服务操作

Runtime.getRuntime().exec("scp -r /###/dir  user@HostB:/dir") ;这样就可以把你的服务器上的文件copy至HostB上。强大吧,当然该功能需要借助于上述说的公钥文件的配置。至于采用scp来copy file和directory的具体参数就不多说了。

 

举例:

(1) 用Runtime.getRuntime().exec 打开文件(windows) Runtime.getRuntime().exec("cmd.exe /c start E://1.xlsx") ;

 

(2) Runtime.getRuntime().exec执行结果:

 

Process prs =  Runtime.getRuntime().exec("cmd.exe /c start E://test//1.xlsx") ; 

prs.waitFor() ;

int i = prs.exitValue() ;// i 即为执行结果

prs.destroy() ;

 

当然,在用完该方法后,最好Process prs =  Runtime.getRuntime().exec("") ;  ......  prs.destroy() ;

 

 

 

2. ganymed-ssh2-build210.jar包提供了很多从方法

(API:http://www.ganymed.ethz.ch/ssh2/javadoc/index-all.html#_R_)它的sess.execCommand(command) ;方法依旧能完成上述scp的功能。

 

这两者的共同之处在于它们都是在执行操作系统底层的东西,简洁、高效、非常强大。不仅仅是我在上面介绍的scp的功能,比如删除目录以及该目录下所有的目录&文件呢?rm -rf /###/drectory ......

 

哈哈,大家也许看明白了,只要是你的操作系统所能执行的命令,这里都可以帮你完成,要让我用一句话评价,那就是:强大!

 

 

 

1
0
分享到:
评论

相关推荐

    微软内部资料-SQL性能优化2

    Five different types of pages represent the system’s working set: system cache; paged pool; pageable code and data in the kernel; page-able code and data in device drivers; and system mapped views. ...

    UE(官方下载)

    You can use these functions to insert a file into the current file, delete the active file, send the file through email, or insert a string into the file at every specified increment HTML preview ...

    Universal-USB-Installer

    this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these ...

    联想旭日420AXP系统芯片组驱动

    package is designed to operate with. 2. One of the following operating systems must be fully installed and running on the system before installing this software: Microsoft Windows* Server 2003 ...

    Chucks_VBA_Reference_rev17.docx

    Instructions for determining if a workbook exists in Excel 2003, which might involve checking the file system or using specific VBA techniques. **Determine if Workbook Exists – Excel 2007 (Page 23)*...

    Diskeeper 2008 v12.0.759.0

    percent or two of free space left available or a file in millions of fragments, Diskeeper can restore lost performance and save the applications reliant on those files from major reliability ...

    WizFlow网页编辑

    is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked ...

    hibernate-shards.jar

    is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo. Sometimes it makes sense to have pointer (or better, scoped_ptr) members ...

    ramin 2.2 for xp

    Service pack 4 or higher is required. ---------------------------------------------------------------------- Installation. ---------------------------------------------------------------------- Two...

    The Art of Assembly Language Programming

    The Division Instructions: DIV, IDIV, and AAD 6.6 - Logical, Shift, Rotate and Bit Instructions 6.6.1 - The Logical Instructions: AND, OR, XOR, and NOT 6.6.2 - The Shift Instructions: SHL/...

Global site tag (gtag.js) - Google Analytics