jsch

Running command after sudo login

…衆ロ難τιáo~ 提交于 2019-11-28 12:56:05
I'm trying to execute below code to execute sudo commands but I do not know how execute commands after sudo login String[] commands = {"sudo su - myname;","id"}; JSch jsch = new JSch(); String username = "myuser"; com.jcraft.jsch.Session session = jsch.getSession(username,"hostname", 22); session.setPassword("my@123"); session.connect(); Channel channel=session.openChannel("exec"); for(int a=0;a<=commands.length;a++){ ((ChannelExec)channel).setCommand("sudo su - myname;"); ((ChannelExec)channel).setErrStream(System.err); ((ChannelExec) channel).setPty(true); channel.connect(); System.out

JSch: UnknownHostKey exception even when the hostkey fingerprint is present in the known_hosts file

感情迁移 提交于 2019-11-28 12:39:10
There are two questions about this exception already: JSchException: UnknownHostKey and com.jcraft.jsch.JSchException: UnknownHostKey I am using a Windows machine and trying to connect to a VM created with Vagrant running Ubuntu. Here is my code: public static void main(String[] args) { String host = "localhost"; String username = "vagrant"; int port = 2200; String privateKey = "C:\\keys\\openSSH_pair1\\open_ssh_private"; JSch js = new JSch(); try { js.addIdentity(privateKey, "pass"); js.setKnownHosts("C:\\Users\\user\\.ssh\\known_hosts"); Session session = js.getSession(username, host, port);

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

拟墨画扇 提交于 2019-11-28 07:04:02
问题 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

JSch: How to keep the session alive and up

送分小仙女□ 提交于 2019-11-28 06:33:01
I'm writing Java GUI program for static route management using SSH. My code is as follows: import com.jcraft.jsch.*; import java.io.*; public class Konsep { String status; static String username; static String hostname; String inputcommand; String output; static Session session; JSch jsch = new JSch(); public String status(String stringstatus) { stringstatus = status; return stringstatus; } public String InputCommand(String inputcommandstatus) { inputcommandstatus = inputcommand; return inputcommandstatus; } public void connect(String usernamelokal, String hostnamelokal, String password, int

Funny Shell Output: [01;32mtestfile.txt[00m instead of testfile.txt

℡╲_俬逩灬. 提交于 2019-11-28 06:28:02
问题 Question: I'm getting "funny" character output from commands I send when using a ChannelShell and I'm wondering how to get just the regular output you see in a PuTTY session. By "funny" I mean I should see this: testfile.txt And I'm seeing this: [01;32mtestfile.txt[00m This is similar to the problem in this question except the answer does not satisfy my need. The answer there is to call setPty(false) on the ChannelShell which removes the "pseudo-terminal" entirely, but I need to have the

JSch SCP file transfer using “exec” channel

笑着哭i 提交于 2019-11-28 06:26:56
问题 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

JSch to add private key from a string

廉价感情. 提交于 2019-11-28 04:48:18
问题 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. 回答1: 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

Using connection pool with JSCH

空扰寡人 提交于 2019-11-28 03:54:59
问题 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

How can I specify the path of a JAR in an ant buildfile?

非 Y 不嫁゛ 提交于 2019-11-28 03:26:39
问题 I am executing lot of scp and sshexec and other remote commands from an ant build script. These commands don't work if jsch.jar isn't in the ant lib directory. To make it work, I copied the JAR into the ant lib directory, but this is not a good solution, as anyone else wanting to run the script would have to do the same thing. To run the ant target from Teamcity, we will have to explicitly set the path of the lib file. Is there a way I can specify the path of the JAR in the ant build XML

jsch ChannelExec run a .sh script with nohup “lose” some commands

别说谁变了你拦得住时间么 提交于 2019-11-28 02:04:48
问题 I hava a .sh script which glues many other scripts, called by jsch ChannelExec from a windows application. Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand("/foo/bar/foobar.sh"); channel.connect(); if I run the command like "nohup /foo/bar/foobar.sh >> /path/to/foo.log &", all the long term jobs(database operations, file processing etc.) seems to get lost. checking the log file, only find those echo stuffs(before and after a long term operation, calculate