Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Java and SSH

0 Replies, 3373 Views

In The Name OF Allah
Al-Salam Alekum

Hello guys, Today I'm going to explain using JSch library in Java to connect with SSH server, executing commands and downloading files. I hope ye all like it...

Start your IDE I'm using NetBeans. Add JSch library Download it from here

In this example we will connect to our server(VPS) through SSH and run a command (cd /var/www && ls).

PHP Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ssh;

import java.io.InputStream;
import java.util.Properties;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class 
SSHConnectionJava {
 
   public static void main(String[] args) {

    
//Host IP    
 
       String host "1.1.1.1";
    
//Host User
 
       String user "root";
    
//Host Password
 
       String password "12345678";
 
               
        try 
{
 
           Properties config = new Properties();
 
           config.put("StrictHostKeyChecking""no");
 
           JSch jsch = new JSch();
 
           // Create a JSch session to connect to the server
 
           Session session jsch.getSession(userhost22);
 
           session.setPassword(password);
 
           session.setConfig(config);
 
           // Establish the connection
 
           session.connect();
 
           System.out.println("Connected...");
 
           ChannelExec channel = (ChannelExecsession.openChannel("exec");
 
           //Executing a command
 
           channel.setCommand("cd /var/www/ && ls");
 
           channel.setErrStream(System.err);
 
           
            InputStream in 
channel.getInputStream();
 
           channel.connect();
 
           byte[] tmp = new byte[1024];
 
           while (true) {
 
               while (in.available() > 0) {
 
                   int i in.read(tmp01024);
 
                   if (0) {
 
                       break;
 
                   }
 
                   System.out.print(new String(tmp0i));
 
               }
 
               if (channel.isClosed()) {
 
                   System.out.println("Exit Status: "
 
                       channel.getExitStatus());
 
                       break;
 
                   }
 
                   Thread.sleep(1000);
 
           }
 
                       
            channel
.disconnect();
 
           session.disconnect();
 
           System.out.println("DONE!!!");
    } catch (
Exception e) {
 
           e.printStackTrace();
 
           }
    }
 
   


As you see in this image, it shows us html as a commands result.

   

Now all depend on you for Backing up both your website files and SQL database using two commands instead of the one above:

Code:
zip -r /home/redsecurity.zip /var/www/ && mysqldump --all-databases > /home/all_databases.sql

This above will generate a zip files which include our files and the second command will generate a SQL file which include the whole databases data. Note: && separate the two commands.

Okay Let's download this files above: 

PHP Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ssh;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
/**
 * 
 * @author Kurdy
 *
 */
public class DownloadFileSFTP {
 
 
   public static void main(String[] argsthrows Exception {

 
       JSch jsch = new JSch();
 
       Session session null;
 
       try {
 
           //Server User, IP and Port
 
           session jsch.getSession("root""1.1.1.1"22);
 
           session.setConfig("StrictHostKeyChecking""no");
 
           //Server Password here...
 
           session.setPassword("12345678");
 
           session.connect();
 
           
            Channel channel 
session.openChannel("sftp");
 
           channel.connect();
 
           //creating a FTP channel between you and the server
 
           ChannelSftp sftpChannel = (ChannelSftpchannel;
 
           //Downlaoding redsecurity.zip file to partiton D
 
           sftpChannel.get("/home/redsecurity.zip""D:\\redsecurity.zip"); 
 
           //Downloading all_databses.sql file to partition D
 
           sftpChannel.get("/home/all_databases.sql""D:\\all_databases.sql");
 
           System.out.println("Downloading Finished");
 
           sftpChannel.exit();
 
           session.disconnect();
 
       } catch (JSchException e) {
 
           e.printStackTrace();  
        
} catch (SftpException e) {
 
           e.printStackTrace();
 
       }

 
  }



I'm done if you need anything feel free PMing me or posting here down...
Thank you for reading ......


Wa Salam Alekum
Rs
* Thankful to Allah *
Kurdy

Possibly Related Threads…
Thread Author Replies Views Last Post
Question What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling? Mr.Kurd 0 5,157 03-20-2020, 08:49 PM
Last Post: Mr.Kurd
Question Why Java Strings are immutable in nature? Mr.Kurd 0 2,590 03-20-2020, 08:47 PM
Last Post: Mr.Kurd
Question What is constructor chaining in Java? Mr.Kurd 0 2,546 03-20-2020, 08:43 PM
Last Post: Mr.Kurd
Question What is the difference between equals() and == in Java? Mr.Kurd 0 2,571 03-20-2020, 08:40 PM
Last Post: Mr.Kurd
  What public static void main(String args[]) stand for in Java? Mr.Kurd 0 2,622 03-20-2020, 08:38 PM
Last Post: Mr.Kurd
Question Why multiple inheritance is not supported in java? Mr.Kurd 0 2,564 03-20-2020, 08:34 PM
Last Post: Mr.Kurd
  Tutorial Taking Screenshot in Java Mr.Kurd 0 2,651 03-09-2020, 05:38 PM
Last Post: Mr.Kurd
  10 java skills HeavensReject 1 3,352 01-12-2020, 05:31 AM
Last Post: Mr.Kurd
Wink Tutorial Downloading video from Youtube using JAVA Mr.Kurd 0 5,288 02-06-2019, 06:33 PM
Last Post: Mr.Kurd
  Tutorial Writing to a word file in Java Mr.Kurd 0 3,043 11-30-2018, 08:43 AM
Last Post: Mr.Kurd



Users browsing this thread: 1 Guest(s)