Link to home
Start Free TrialLog in
Avatar of sduser1
sduser1Flag for United States of America

asked on

Need to know what maven dependency to use to connect to Linux box and run the batch commands

Hi,

I have java maven build project. I need to connect to a linux box and transfer few files from the local machine to a folder to linux and run the commands.

Please help me which maven dependency i can use and connect to the linux box. This is for writing cucumber automated tests using java.
Avatar of David Favor
David Favor
Flag of United States of America image

You'll setup an ssh connection, then use either scp or rsync (recommended) to copy your files to your Linux box.
Avatar of sduser1

ASKER

Can you tell me which maven dependency i can use which will help me make SSH connection to linux box?
There is no Maven dependency to run ssh.

All Linux Distros installed on machine + LXD will have sshd running.

You'll setup a keypair to access the machine. If you'll be doing this type of work repeatedly, you'll likely setup an empty passphrase ssh key, to avoid having to input your password repeatedly... or use a passphrase key you inject into ssh-agent.

If you're new to getting ssh to work, open another question as Maven != ssh, they have no relationship + are 2x completely different topic questions.
You need jsch (latest version is 0.1.55):

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

https://mvnrepository.com/artifact/com.jcraft/jsch/0.1.55
@girionis, I believe client is asking how to...

1) push a setup of Maven files from a local dev environment to a live runtime environment.

2) Not integrating ssh into Maven, push a set of Maven files to a server to deploy.

@sduser1, if you're asking about #1, you'll follow my comment. If you're asking about #2, you'll follow the girionis comment.
Avatar of sduser1

ASKER

Thanks to both of you. I am looking for jsch to add the maven dependency in pom file and connect to ssh.

 I will post questions if I have any issue connecting with jsch. Thanks for your help
I'm with girionis on this one.

which maven dependency i can use which will help me make SSH connection to linux box

I could be wrong, but based on the question to me it sounds like he's trying to integrate SSH functionality
Avatar of sduser1

ASKER

Using jsch I am able to connect to Linux server and run the batch command.

I need help in copying few files from my local file system (windows) to Linux server. Can you help me how to do that?

Below program runs the batch command on the Linux server using jsch. It works fine.  I need to copy few files before I run the batch command.

import java.io.InputStream;

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

public class ConnectSSH {

      
      public static void main(String[] args) {
            
            runSSH();
            
      }
      
      public static void runSSH() {
            
      
      //Below code is to connect to linux box with SSH
      
                  String host="HOST1";
                String user="TEST1";
                String password="PWD1";
                String command1="ls -ltr";
                String command2="cd /scripts;./test.sh";
                try{
                      
                      java.util.Properties config = new java.util.Properties();
                      config.put("StrictHostKeyChecking", "no");
                      JSch jsch = new JSch();
                      Session session=jsch.getSession(user, host, 22);
                      session.setPassword(password);
                      session.setConfig(config);
                      session.connect();
                      System.out.println("Connected");
                      
                      Channel channel=session.openChannel("exec");
                    ((ChannelExec)channel).setCommand(command2);
                    channel.setInputStream(null);
                    ((ChannelExec)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(tmp, 0, 1024);
                        if(i<0)break;
                        System.out.print(new String(tmp, 0, i));
                      }
                      if(channel.isClosed()){
                        System.out.println("exit-status: "+channel.getExitStatus());
                        break;
                      }
                      try{Thread.sleep(1000);}catch(Exception ee){}
                    }
                    channel.disconnect();
                    session.disconnect();
                    System.out.println("DONE");
                }catch(Exception e){
                      e.printStackTrace();
                }

            }
}
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial