Hi I am currently using the below code line to issue commands to the remote server
ChannelSftp sftp = (ChannelSftp)session.openChannel("sftp");
sftp.connect();
System.out.println("Current Directory: " + sftp.pwd()); <= This Line
But pwd is already be defined as a method in Class ChannelSftp, My Question is what if I want to issue some command or run any .sh file which is not a method in JSch?
Let say if I want to run : sudo /opt/bin/run.sh file.
OR is there any method which I can use to do the job
(Kindly note I have a Jump Server in between localhost and web Server. And web server can only be accessed via the jump server.So that is the reason I am not using exec or shell).
The ChannelSftp class is implementing the SFTP protocol, which is cabable of preforming FTP operations. Executing a random command is clearly not permitted via SFTP. What you need in this case is using a different class of the jsch lib: ChannelExec.
Example is here.
Related
I'm creating an Android extension intended to connect to a remote SSH host based on the well known JSch library, so that once the button is pressed, the class is instantiated this way, with no compilation error:
JSch jsch = new JSch();
...but, application crashes!
Looking at the JSch() constructor, there is nothing inside:
public JSch(){
// here there are several commented instructions that need to be
// used just at MAC operational systems (not my OS).
}
If I remove initiation, compiler complains that variable jsch is not initalized.
JSch jsch;
Once I'm compiling with Apache Ant, there is no debug resources that I could use to trace the runtime error.
All tests are being made either within threads or callback methods, giving the same result; I simply cannot initiate the jsch variable, this yields a runtime exception com.jcraft.jsch.JSch.
Does someone has some insights on how to solve that issue ?
I was able to install the Android SDK-tools and after capturing the Log via line command ADB logcat > file.txt, there were the following sentence many times:
java.lang.NoClassDefFoundError: com.jcraft.jsch.JSch
Which means that the extension file (.aix) compiled with apache command ant extensions did not embed the above class within it.
This explains the issue at all (application crashing instantiating above constructor - absent), and the next step is to discover how to encapsulate dependencies - surely in another thread, if I will not be able to do on my own.
I am trying to download a file from SFTP server to my local machine using JSch. It only downloads 16371 bytes of data regardless of the file size and ends the transfer. It does not throw any exception. If the file is smaller than 16371 bytes it is transfered sucesfully, but for any larger file the transfer results in a corrupted file.
Actually I managed to solve this problem. I replaced:
SftpProgressMonitor monitor = new MySftpProgressMonitor();
channelSftp.get(sourceFile, destFile, monitor);
with:
channelSftp.get(sourceFile, destFile);
After removing the progress monitor the transfer was sucesfull. I guess it is a bug inside Jsch.
I had the same problem with a special sftp server. My unique solution was use LFTP, wich is a linux command for automatize SFTP tasks. If you have a Linux environment it is very useful.
Example in PHP:
$command = set net:timeout 30; lcd $directorioDestino; cd /Usr/companies/cdrusr357901/CallCenterRecords/; mget {$prefijoArchivosAuris}*; bye
lftp -u {$this->user},{$this->password} -e '$command' sftp://{$this->host}
My application was using jsch 1.28 version previously and I was also
using JTA 2.0 for Telnet/SSH terminal everything was working fine until I
moved to the latest version of jsch 1.48, the problem I'm facing is that
I am able to open the SSH Terminal and connect as well but when I try to
execute some command from Keyboard on the Connection terminal it is not
all responding to keystrokes.
I moved to jsch 1.48 because with jsch 1.28 after trying connect via ssh i got error: "Algorithm negotiation fault"
Thanks
It works
I had to add: out_.flush(); after out_ = channel_.getOutputStream(); in the JTAJSch.java plugin file
I have tried the Shell.java example given in the link http://www.jcraft.com/jsch/examples/Shell.java.html. It works properly with the jsch 1.48.
Could you try it seperatelly from your project to find out the route cause.
I have a .bat file on a remote machine. I want to invoke it through http call. I dont want to make any changes on the remote machine. Is there a way to do it using java and http?
String command = "cmd /C start C:/Users/abc/Desktop/test.bat";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
The above works good to invoke a .bat file on local machine. I would not mind considering other ways too, but invoking it through http would be the first choice.
EDIT:
I am using paramiko to do this now. However,I am unable to run the remote commands on the command prompt.
ssh = paramiko.SSHClient()
print "Enter the IP address"
ip = raw_input("ip>")
print "Enter the username"
user = raw_input("username>")
print "Enter the password"
pwd = raw_input("password>")
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=user, password=pwd, allow_agent = False)
print "connection successfull"
i, o, e = ssh.exec_command("echo test") # example command
s = e.read()
if s: # an error occurred
raise RuntimeError, s
result = o.read()
print result
Somehow it says AllowDesktopAccess failed
You need a service on the remote machine, for example an http server that is configured to run this script on demand (eg via cgi) or an ssh server you can connect to to issue the command.
Since you're using windows (I assume) then PsExec may be the service you need.
http://technet.microsoft.com/en-us/sysinternals/bb897553
I need to run a batch file on a remote system from a local machine. Using the code below, I am getting the following error:
Path not correct
I have the IP address of the machine, and I have given the batch file as public share and share name is dsc.
The IP address of the machine is 16.181.37.28.
Here is my code. I know that the path is wrong. How can I give the exact path?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author padmaja
*/
import java.io.*;
class Test{
public static void main(String arg[]){
try{
String command = "cmd /C start 16.181.37.28/dsc/StartVisTsDataCenterMySql-log.bat";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
}catch (IOException e) {
e.printStackTrace();
}
}
}
Are you trying to run the script ON THE REMOTE machine or ON YOUR LOCAL machine? Your approach will read the file from the remote machine but run it on your local.
The usual way to get something running on a remote machine is having a process on the remote machine to run permanently and listen for requests. If a request arrives this process will start your batch file you would like to have run.
You're running on Windows but using Posix paths separators. Try "cmd /C \\\\16.181.37.28\\dsc\\StartVisTsDataCenterMySql-log.bat".
You can test whether it works by creating a service:
sc \\16.181.37.28 create StartVisTsDataCenterMySql-Log binPath= "cmd /c \\16.181.37.28\dsc\StartVisTsDataCenterMySql-log.bat"
Then the command to run it is:
"cmd /c sc \\16.181.37.28 stop StartVisTsDataCenterMySql-Log&sc \\16.181.37.28 start StartVisTsDataCenterMySql-Log"
You'll need to be connected to the share as an administrator (or have the credentials saved). Once you've confirmed that works, change to srvany, as you will get an error in the event log and the batch file will only be allowed to run for 30 seconds otherwise.
If that's not the right answer, perhaps you can elaborate on the real requirement, and give some information on whether reimplementing the batch file in Java is a realistic solution.