I want to execute command on remote system and want to get result of the same.
I tried following code:
package rough;
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 RemoteSSH {
public static void main(String[] args) {
String host = "\\\\10.209.110.114";
String user = "Administrator";
String password = "Admin123";
String command1 = "ipconfig";
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(command1);
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();
}
}
}
But I am getting following error:
com.jcraft.jsch.JSchException: java.lang.IllegalArgumentException: protocol = socket host = null
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at rough.RemoteSSH.main(RemoteSSH.java:23)
Caused by: java.lang.IllegalArgumentException: protocol = socket host = null
at sun.net.spi.DefaultProxySelector.select(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.jcraft.jsch.Util.createSocket(Util.java:252)
... 3 more
Any solution for it?
I have already tried with pstool also but not getting correct output.
After removing the \\\\ from the hostname, I'm getting:
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at rough.RemoteSSH.main(RemoteSSH.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
My wild guess is that you have no SSH server running on the target machine.
Note that Windows does not have any built-in SSH server.
Make sure you install some SSH server first.
See Is IIS SFTP natively supported by Windows Server 2012 R2?
Or use a different technology to run the command:
Best way to run remote commands on a Windows server from Java?
Related
i am using java JSCH library to connect to server-2 using jump server(server-1). Using the below code i am able to connect to jump server(server-1). using the same session its failing to connect to server2
getting the below respone
JSch jsch = new JSch();
jsch.setKnownHosts("C:/My Program Files/eclipse/workspace/StatusTracker/known_hosts.txt");
// jsch.setKnownHosts(knownHostLoc);
//Jump server connection session started
jumpServerSession = jsch.getSession(userid, jump server ip/hostname, 22);
jumpServerSession.setPassword(jump server password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
jumpServerSession.setConfig(config);
jumpServerSession.connect();
System.out.println("The session has been established to "+jump server userid+"#"+jump server name);
int assinged_port = jumpServerSession.setPortForwardingL(0, other server ip, 22);
System.out.println("portforwarding: "+
"localhost:"+assinged_port+" -> "+other server ip+":"+22);
//Main server connection session started
targetServerSession = jsch.getSession(fileBO.getServerUserId(), "127.0.0.1", assinged_port);
targetServerSession.setHostKeyAlias(other server ip);
targetServerSession.setPassword(other server password);
java.util.Properties config1 = new java.util.Properties();
config1.put("StrictHostKeyChecking", "no");
targetServerSession.setConfig(config1);
targetServerSession.connect();
channel = targetServerSession.openChannel("sftp");
channel = targetServerSession.openChannel("exec");
//command want to execute on dest server
((ChannelExec)channel).setCommand("pwd");
channel.setInputStream(null);
InputStream in11=channel.getInputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
byte[] tmp1=new byte[1024];
while(true){
while(in11.available()>0){
int i1=in11.read(tmp1, 0, 1024);
if(i1<0)break;
System.out.print(new String(tmp1, 0, i1));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
} catch (final JSchException e) {
LOGGER.error(e.getMessage());
}
Connected
portforwarding: localhost:64038 -> jumpServerSession64038
com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset
at com.jcraft.jsch.Session.connect(Session.java:565)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.test.appname.TestSCH.main(TestSCH.java:44)
I'm developing FTP program on JAVA. I'm using Apache Commons Net Library. My Codes are below.
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class ServerClass {
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
}
public static void main(String[] args) {
String server = "127.0.0.1";
int port = 80;
String user = "root";
String pass = "root";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
showServerReply(ftpClient);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("Operation failed. Server reply code: " + replyCode);
return;
}
boolean success = ftpClient.login(user, pass);
showServerReply(ftpClient);
if (!success) {
System.out.println("Could not login to the server");
return;
} else {
System.out.println("LOGGED IN SERVER");
}
} catch (IOException ex) {
System.out.println("Oops! Something wrong happened");
ex.printStackTrace();
}
}
}
But I can't connect my localhost. I want to login my localhost and see my file.
My errors are below.
Oops! Something wrong happened
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:188)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
at com.emrecanoztas.ftp.ServerClass.main(ServerClass.java:22)
can you help me anybody? Thanks!..
Questions:
Is there an FTP server running?
Is there a message in the FTP server log about the connect request?
Does the FTP server allow connections from localhost?
Is the FTP server listening on localhost or should you use the public IP/name of the computer? (check with netstat)
Try the settings below.
String server = "ftp.icm.edu.pl";
int port = 21;
String user = "anonymous";
String pass = "me#nowhere.com";
I'm trying to upload a file to webapps folder of tomcat in my local system through the following code, also do we require any installations to be done before using this code
public class DeployManager {
public static void main(String[] args) {
String SFTPHOST = "1.2.3.4";
int SFTPPORT = 22;
String SFTPUSER = "username";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "C:\\Program Files\\Apache Software Foundation\\Tomcat 8.0\\webapps\\";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("..");
channelSftp.cd(SFTPWORKINGDIR);
File f = new File("C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war");
channelSftp.put(new FileInputStream(f), f.getName());
} catch (Exception ex) {
ex.printStackTrace();
}
}}
but I'm getting the following error:
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at com.autohost.java.DeployManager.main(DeployManager.java:30)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.jcraft.jsch.Util.createSocket(Util.java:252)
... 3 more
You need to make sure sftp running on the machine with IP "10.74.64.102" . I guess its your local windows machine .
I am using a Java SFTP library Called JSch , and here is my code :
package client;
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;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class SFTPCode_2 {
public SFTPCode_2() {
super();
}
public static void main (String[] args) {
SFTPCode_2 fileTransfer = new SFTPCode_2();
try {
JSch jsch = new JSch();
String host = "127.0.0.1";
int port = 22;
String user = "username";
Session sessionRead = jsch.getSession(user, host, port);
sessionRead.connect();
Session sessionWrite = jsch.getSession(user, host, port);
sessionWrite.connect();
ChannelSftp channelRead = (ChannelSftp)sessionRead.openChannel("sftp");
channelRead.connect();
ChannelSftp channelWrite = (ChannelSftp)sessionWrite.openChannel("sftp");
channelWrite.connect();
PipedInputStream pin = new PipedInputStream(2048);
PipedOutputStream pout = new PipedOutputStream(pin);
/* channelRead.rename("C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_1/house.bmp",
"C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_2/house.bmp"); */
channelRead.get("C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_1/house.bmp", pout);
channelWrite.put(pin , "C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_2/house.bmp");
channelRead.disconnect();
channelWrite.disconnect();
sessionRead.disconnect();
sessionWrite.disconnect();
} catch( JSchException e) {
e.printStackTrace(); }
catch (SftpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am trying to copy a file from one directory (within my local machine) to another, using the JSch library .
But it give me this error :
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
at com.jcraft.jsch.Util.createSocket(Util.java:349)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at client.SFTPCode_2.main(SFTPCode_2.java:43)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at com.jcraft.jsch.Util.createSocket(Util.java:343)
... 3 more
Process exited with exit code 0.
I tried adding Cygwin ( from link here ) but it still didn't work.
It's not clear what line is throwing the exception. Try to surround each Jsch action with try/catch, like so:
Session sessionRead = null;
try {
sessionRead = jsch.getSession(user, host, port);
} catch (JSchException e) {
System.out.println("Issue getting session.");
e.printStackTrace();
}
try {
sessionRead.connect(); // do you need to set properties first?
} catch (JSchException e) {
System.out.println("Issue connecting to session.");
e.printStackTrace();
}
For me, when I use Jsch, I had to add session properties to make the connection eg, on sessionRead.connect() .
sessionRead.setPassword("password");
sessionRead.setConfig("StrictHostKeyChecking", "no");
sessionRead.connect();
i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3
i have followed this article for create my connection, and with official examples i've controlled article.
my java code here:
private void connect(String host, String user, String pwd) {
try{
ftp = new FTPSClient(false);
//ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host,22);//error is here
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}catch(Exception ex){
ex.printStackTrace();
}
}
I do not understand where I went wrong.
The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library
JSch jsch = new JSch();
Session session = jsch.getSession( user, host, port );
session.setConfig( "PreferredAuthentications", "password" );
session.setPassword( pass );
session.connect( FTP_TIMEOUT );
Channel channel = session.openChannel( "sftp" );
ChannelSftp sftp = ( ChannelSftp ) channel;
sftp.connect( FTP_TIMEOUT );
SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {
public static void main(String[] args) throws Exception {
String user = "username";
String host = "hostadress";
int port = 22;
String pass = "password";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
}
try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.
Change the port to 21 to connect to FTP server instead of SFTP(port 22)