Java operate file or system between different service

Source: Internet
Author: User

Imagine: Can I transfer files between different servers or execute operating system commands to operate the server?
Answer: Of course.
 
First, we will introduce how to access other services without a password. (This is the basis for subsequent operations)
 
 
To allow HostB to allow user User1 on HostA to perform operations, you must:
 
1. log in with User1 or su-User1 on HostA
2. Use the ssh-keygen command to generate the HostA key pair File
Ssh-keygen-t rsa
Directory:/home/User1/. ssh/
Id_rsa is a private key file.
Id_rsa.pub is a public key file.
3. modify attributes of a public key file
Chmod 755/home/User1/. ssh/id_rsa.pub
4. Add the public key content id_rsa.pub on HostA to HostB.
If there is no/home/User1/. ssh directory or authorized_keys on HostB, you need to create
Add the content of HostA's id_rsa.pub file to the/home/User1/. ssh/authorized_keys file.
5. Test: (you must log on to User1)
Scp 1.txt UserB @ HostB:/*****/xxxx/
 
If you can copy the 1.txt file from HostA to HostB without modifying the password, the configuration is OK.
 
 
Second, there are a lot of java-implemented ssh on the Internet, such as jsch. Here we will introduce two underlying operations, which are more concise and efficient.
 
1. runtime.getruntime(cmd.exe c ()
 
I think everyone is familiar with this. The Runtime is very powerful. It can be done by your operating system. For example, scp commands commonly used in linux system, cross-service operations
Runtime.getruntime(cmd.exe c ("scp-r/###/dir user @ HostB:/dir"); in this way, you can copy the files on your server to HostB. Powerful, of course, this function requires the help of the public key file configuration mentioned above. The specific parameters for using scp to copy file and directory are not described in detail.
 
 
2. The ganymed-ssh2-build210.jar package provides many slave Methods
(API: The sess.exe cCommand (command) of the http://www.ganymed.ethz.ch/ssh2/javadoc/index-all.html#_R_ connector; method can still complete the above scp function.
 
What they share is that they are all at the underlying Level of the operating system, simple, efficient, and very powerful. It's not just the scp function I introduced above, such as deleting a directory and all the directories and files under the directory? Rm-rf/###/drectory ......
 
Haha, you may have understood that as long as it is the command that your operating system can execute, it can be done for you here. Let me evaluate it in one sentence, that is: powerful!

Author "zzhangyongjie"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.