sduser1
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.
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.
You'll setup an ssh connection, then use either scp or rsync (recommended) to copy your files to your Linux box.
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.
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</group Id>
<artifactId>jsch</artifact Id>
<version>0.1.55</version>
</dependency>
https://mvnrepository.com/artifact/com.jcraft/jsch/0.1.55
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</group
<artifactId>jsch</artifact
<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.
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.
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 will post questions if I have any issue connecting with jsch. Thanks for your help
I'm with girionis on this one.
I could be wrong, but based on the question to me it sounds like he's trying to integrate SSH functionality
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
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.ChannelExe c;
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("StrictHostKeyC hecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(us er, host, 22);
session.setPassword(passwo rd);
session.setConfig(config);
session.connect();
System.out.println("Connec ted");
Channel channel=session.openChanne l("exec");
((ChannelExec)channel).set Command(co mmand2);
channel.setInputStream(nul l);
((ChannelExec)channel).set ErrStream( 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-s tatus: "+channel.getExitStatus()) ;
break;
}
try{Thread.sleep(1000);}ca tch(Except ion ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE") ;
}catch(Exception e){
e.printStackTrace();
}
}
}
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.ChannelExe
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("StrictHostKeyC
JSch jsch = new JSch();
Session session=jsch.getSession(us
session.setPassword(passwo
session.setConfig(config);
session.connect();
System.out.println("Connec
Channel channel=session.openChanne
((ChannelExec)channel).set
channel.setInputStream(nul
((ChannelExec)channel).set
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-s
break;
}
try{Thread.sleep(1000);}ca
}
channel.disconnect();
session.disconnect();
System.out.println("DONE")
}catch(Exception e){
e.printStackTrace();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.