jsch

JSch:纯JAVA实现远程执行SSH2主机的SHELL命令

淺唱寂寞╮ 提交于 2019-11-29 18:22:43
上篇文章我编写了利用JSch实现SFTP的文件上传和下载 http://my.oschina.net/hetiangui/blog/137357 ,在本篇文章中,我将描述如何利用JSch实现执行远程SSH2主机的SHELL命令,不说了,直接上代码和详细的代码说明: /** * 利用JSch包实现远程主机SHELL命令执行 * @param ip 主机IP * @param user 主机登陆用户名 * @param psw 主机登陆密码 * @param port 主机ssh2登陆端口,如果取默认值,传-1 * @param privateKey 密钥文件路径 * @param passphrase 密钥的密码 */ public static void sshShell(String ip, String user, String psw ,int port ,String privateKey ,String passphrase) throws Exception{ Session session = null; Channel channel = null; JSch jsch = new JSch(); //设置密钥和密码 if (privateKey != null && !"".equals(privateKey)) { if (passphrase != null

JSch issue - Cannot retrieve full command output [closed]

泪湿孤枕 提交于 2019-11-29 16:32:55
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . I am using JSch to connect to SSH and execute commands. One of the commands gives out a big output. In terminal if you execute the command you have to press enter to see the entire output. Using JSch I am not able to retrieve the entire output. When I login using an interactive terminal, the

Finding file size and last modified of SFTP oldest file using Java

无人久伴 提交于 2019-11-29 15:38:24
I'm using JSch to get files from an SFTP server, but I'm trying to figure out a way to only get the oldest file, and to make sure that it is not currently being written to. The way I imagine myself doing this is first finding which file in the specified remote folder is oldest. I would then check the file size, wait x seconds (probably about 10, just to be safe) and then check it again. If the file size has not changed, I download the file and process it. However, I have no idea how to do this! If anybody knows how to do this, or knows of something else that supports SFTP that has this built

Executing sudo using SSH “exec” channel in JSch

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 13:04:00
I am using file in which I passed below commands: hostname pwd pbrun su - fclaim whoami cd .. pwd Ad the Java code below: for (String command1 : commands) { Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command1); 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.println(new String(tmp, 0, i)); } if(channel.isClosed()){ break; } } channel.setInputStream(null); channel.disconnect(); } But I'm getting this output: some hostname /home/imam missing

USERAUTH fail using JGit to access git repo securely for PullCommand()

孤街浪徒 提交于 2019-11-29 12:54:26
I am trying to do a git pull using JGit's API with the following code public class gitHubTest { JSch jsch = new JSch(); // Defining the private key file location explicitly String userHome = System.getProperty("user.home"); String privateKey = userHome + "/.ssh/id_rsa"; String knownHostsFile = userHome + "/.ssh/known_hosts"; Repository localRepo = new FileRepository("/LocalPath/.git"); public gitHubTest() throws Exception { jsch.setConfig("StrictHostKeyChecking", "no"); jsch.setKnownHosts(knownHostsFile); jsch.addIdentity(privateKey); System.out.println("privateKey :" + privateKey); Git git =

Executing multiple bash commands using a Java JSch program after sudo login

烂漫一生 提交于 2019-11-29 12:50:14
I am trying to execute multiple bash commands through a Java program which connects to a SSH using JSch. But after sudo login, I am unable to execute any bash commands. From what I have read, after sudo login, we enter into a sub-shell. I wish to use a single channel. I am clueless as to what to do next. ChannelExec chnlex=(ChannelExec) session.openChannel("exec"); InputStream in = chnlex.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(in)); chnlex.setCommand("sudo -u appbatch -H /opt/apptalk/local/bin/start_shell.sh -c <<exit"); chnlex.connect(); System.out

JSch multiple tunnels/jumphosts

廉价感情. 提交于 2019-11-29 12:46:29
I'm not sure if this is caused by using a private key instead of password for the port forwarding but here's what I'm trying to do I need to forward local port 3308 all the way to the my SQL DB at 3306. I can run things like this all together in terminal on my local ssh -L 3308:localhost:3307 username@jumpbox "ssh -L 3307:mysqlDB:3306 username@server" Or run the first part on my local and then the second part on the jumpbox. Both works fine and I can connect to my localhost:3308. The problem comes when I start using JSch. Here is my code JSch jsch = new JSch(); jsch.addIdentity("~/.ssh/id_rsa"

JSch SCP file transfer using “exec” channel

[亡魂溺海] 提交于 2019-11-29 11:56:55
I'm very new to the SCP protocol and JSch. I have to transfer a file fro a remote device via SCP to Android. The server side developers refused to tell be anything about their device except for the file location, and the root account which can be used to access it with SCP. Here are the steps I tried. Confirm that using JSch, my Android client can establish connection with the server. [complete] Confirm that using JSch, and the ChannelExec object, I can send the ls command and read its output. [complete] Confirm that using JSch, and the ChannelSFTP object, I can transfer a file from the device

JSch to add private key from a string

有些话、适合烂在心里 提交于 2019-11-29 11:17:10
I have the contents of the key pair file for SFTP as a string. I need to use JSch to add the contents, but addIdentity only accepts a file path. Is there anyway I can do this? I see that the KeyPair class has a method - KeyPair load(JSch jsch, byte[] bytes, byte[] bytes1) I'm not sure what this does. There is an addIdentity overload that takes the key from a buffer : public class JSch { ... public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase) See also Java SFTP client that takes private key as a string . For an example of implementation, see JSch: addIdentity

Using connection pool with JSCH

老子叫甜甜 提交于 2019-11-29 10:42:49
I'm using JSCH for file upload ever sftp. In it's current state each thread opens and closes connection when needed. If it possible to use connection pooling with JSCH in order to avoid overhead caused by large number of connection opening and closing? Here is a example of function called from inside of thread public static void file_upload(String filename) throws IOException { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("user", "server_name", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("super_secre_password"); session.connect(