Java uses Ganymed-ssh2-build210.jar to implement the LINUXSCP command. __linux

Source: Internet
Author: User

1, establish the connection

Connection con = new Connection (IP, PORT); You can not enter a port number

Connection

Con.connect (); This step must not forget

User name password for remote server

boolean isauthed = Con.authenticatewithpassword (Remoteuser,remotepass);

Establish an SCP client

Scpclient scpclient = Con.createscpclient ();

Server-side files are downloaded to the local directory

Scpclient.get ("/home/test/11.txt", "c:/");

Upload the local file to the server-side directory

Scp.put ("C:/12.txt", "/home/test");

To establish a session, only one Linux command can be executed within a session

Session session = NULL;

Session = Con.opensession ();

Use sessions to manipulate remote servers

For example: Delete files in a remote directory

Session.execcommand ("Rm-f". Concat (RemotePath). Concat (Qrcodefilemode));

Displays information after the command is executed

InputStream stdout = new Streamgobbler (Session.getstdout ());

BufferedReader br = new BufferedReader (new InputStreamReader (stdout));

while (true) {

String line = Br.readline ();

if (line = = null) {

Log.info ("Remote server return information: null");

Break

}

Log.info ("Remote Server return information:" + line);

}

Get launch status

System.out.println ("ExitCode:" + session.getexitstatus ());

Session.close ();

Con.close ();


PS: It is regrettable that the entire folder cannot be uploaded.

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.