jsch

Java JSch changing user on remote machine and execute command

狂风中的少年 提交于 2019-11-27 15:52:50
I'm trying to connect to a host, then change the user using "su - john" and then execute a command as john. Is it possible with using only JSch? The problem is that after I create a session and open the channel and execute the aforementioned command it should request password, but nothing happens. This is how I connect to the remote machine: String address = "myremote.computer.com"; JSch jsch = new JSch(); String user = "tom"; String host = address; String password = "l33tpaSSw0rd"; Session session = jsch.getSession( user, host, 22 ); java.util.Properties config = new java.util.Properties();

How to run multiple Unix commands in one shot

允我心安 提交于 2019-11-27 15:51:55
I am trying to execute multiple commands in one shot but to my surprise only the first command is getting executed and the rest are skipped. And the command is cleartool setview view1234 ; cleartool setactivity activity456 ; cd /vobs/app/src/epw/WEB-INF/scripts ; pwd And the output of the above command is You can now run 'clearquest' to start Rational ClearQuest. But instead I'm expecting to see the following 3 lines of output: You can now run 'clearquest' to start Rational ClearQuest. Set activity "activity456" in view "view1234". /vobs/app/src/epw/WEB-INF/scripts My search efforts yielded

Multiple commands using JSch

半世苍凉 提交于 2019-11-27 15:35:00
My requirement is as follow: I have to login to Unix box using my credentials and once login, I have to do sudo to different user. Once sudo is successful, I have to invoke shell in nohup. On completion of executions, close channel and session both. I tried the first step which is connect using sudo command, but I don't know how to invoke shell script after the sudo command. In the below code I am able to execute sudo command, but after getting sudo access how can I execute a shell in nohup with user masteruser . So that required files created my shell has owner as masteruser . public class

How do I transfer a file from one directory to another using Java SFTP Library JSch?

大兔子大兔子 提交于 2019-11-27 15:02:36
I need to program a file transfer using JSch library. I have a simple directory with two folders - In the SFTP_1 folder, I have a bitmap image. And the SFTP_2 folder is just an empty folder. My goal is to transfer the image using SFTP from SFTP_1 to SFTP_2 . Here is my code thus far : import com.jcraft.jsch.*; import java.awt.Desktop; import java.nio.channels.Channel; public class FileTransfer { public FileTransfer() { super(); } public static void main (String[] args) { FileTransfer fileTransfer = new FileTransfer(); JSch jsch = new JSch(); try { String host = "127.0.0.1"; int port = 22;

com.jcraft.jsch.JSchException: Auth cancel

强颜欢笑 提交于 2019-11-27 13:53:58
问题 I'm trying to write an Ant script to retrieve an URL via port tunnelling. It works great when I use a password (the names xxxx'd out for privacy): <project default="main"> <target name="main"> <sshsession host="xxxx" username="xxxx" password="xxxx"> <LocalTunnel lport="1080" rhost="xxxx" rport="80"/> <sequential> <get src="http://localhost:1080/xxxx" dest="/tmp/xxxx"/> </sequential> </sshsession> </target> </project> But it doesn't work when I use a keyfile, like this: <sshsession host="xxxx"

Sending commands to remote server through ssh by Java with JSch

扶醉桌前 提交于 2019-11-27 13:11:13
问题 I'm trying to set up a class so that I can ssh into a remote server (I have the IP, username, and password) and then send a command like "echo "test"" and then receive back the output (e.g., "test"). I'm using JSch to do this but I don't understand how to do it. import com.jcraft.jsch.*; public class ConnectSSH { public int execute (String command) { JSch jsch = new JSch(); String ip = "00.00.00.00; String user = "root"; String pass = "password"; int port = 22; try { Session session = jsch

What is the difference between the 'shell' channel and the 'exec' channel in JSch

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 12:41:18
I want to be able to send many consecutive command represented as strings within a Java application to a SSH server for execution. Should I use: Channel channel = session.openChannel("shell"); -or- Channel channel = session.openChannel("exec"); With shell channel the shell (on unix it's sh or bash or something like that, on Windows it's usually cmd.exe) is started and console is created (the same you see on the screen if you run them locally). You have a prompt which you can parse or use for detection of completion of command. With command channel a shell instance is started for each command

SFTP connection through Java asking for weird authentication

一笑奈何 提交于 2019-11-27 09:58:35
问题 So I'm writing a little program that needs to connect to a remote server through SFTP, pull down a file, and then processes the file. I came across JSch through some answers here and it looked perfect for the task. So far, easy to use and I've got it working, with one minor thing I'd like to fix. I'm using the following code to connect and pull the file down: JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("username", "127.0.0.1", 22); session.setConfig(

JSch SFTP security with session.setConfig(“StrictHostKeyChecking”, “no”);

天涯浪子 提交于 2019-11-27 09:32:24
I use JSch with private key to FTP file jsch.addIdentity(privatekeyfile); Session session = jsch.getSession( "user", "domain.com" ,22); session.setConfig("StrictHostKeyChecking", "no"); Line 3 is in question. Without this line, JSch does not work. My question is: Will line 3 make SFTP transfer insecure? Disabling the StrictHostKeyChecking option will make the connection less secure than having the option enabled, because it will let you connect to remote servers without verifying their SSH host keys. If the option is enabled, you will only be able to connect to servers which keys are known to

Use JSch sudo example and Channel.setPty for running sudo command on remote host

别来无恙 提交于 2019-11-27 08:59:39
I have used JSch Sudo example under following link: http://www.jcraft.com/jsch/examples/Sudo.java.html And changed it a bit and get rid of all the dialogs as I have to use it for EC2 instances using PuTTY. Now my code looks like this: import com.jcraft.jsch.*; import java.awt.*; import javax.swing.*; import java.io.*; public class sudo{ public static void main(String[] arg){ try{ JSch jsch=new JSch(); String host=null; if(arg.length>0){ host=arg[0]; } String privateKey = "my private key.pem"; jsch.addIdentity(privateKey, ""); Session session=jsch.getSession("ec2-user", "xx.xx.xx.xx", 22);