sshj
ssh, scp and sftp for java
I having a strange issue with sshj (am using sshj v0.6.0) for which I would need some help from someone.
Authentication with public key works fine on some machines but doesnt work fine on other machines and I see the below error.
The only difference that I could make out was that the UNIX ID in question viz coonradt seems to have the below listed configuration setup under ~/.ssh/config only on the box on which the below errors are being triggered
Host *
Protocol 1,2
FallBackToRsh no
ForwardAgent yes
ForwardX11 yes
PasswordAuthentication yes
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
NoHostAuthenticationForLocalhost yes
StrictHostKeyChecking no
KeepAlive yes
From the above config file I learnt that the ID in question is supposed to make use of Protocol 1,2 and I suspect that this might have something to do with my failures (I am not very sure about it, but this is just a hunch)
For all other UNIX IDs for which this works fine, I dont have any such config file.
PS : I cannot alter the config of the UNIX ID "coonradt" since this ID is being used by the central hudson servers.
Would appreciate if someone could please help me suggest as to what might be wrong here
Following is the error that I am seeing :
Oct 24, 2011 2:30:37 AM net.schmizz.sshj.DefaultConfig initCipherFactories
WARNING: Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.TransportImpl init
INFO: Client identity string: SSH-2.0-SSHJ_0_6_0
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.TransportImpl init
INFO: Server identity string: SSH-1.99-OpenSSH_4.3
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.KeyExchanger sendKexInit
INFO: Sending SSH_MSG_KEXINIT
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.KeyExchanger handle
INFO: Received SSH_MSG_KEXINIT
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.kex.AbstractDHG init
INFO: Sending SSH_MSG_KEXDH_INIT
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.KeyExchanger handle
INFO: Received kex followup data
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.kex.AbstractDHG next
INFO: Received SSH_MSG_KEXDH_REPLY
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.TransportImpl die
SEVERE: Dying because - net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify `ssh-rsa` host key with fingerprint `ca:0b:b3:7f:53:5a:e3:bc:bf:44:63:d8:2d:26:c0:41` for `mymachine.domain.com` on port 22
Oct 24, 2011 2:30:38 AM net.schmizz.concurrent.Promise tryRetrieve
SEVERE: <<kex done>> woke to: net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify `ssh-rsa` host key with fingerprint `ca:0b:b3:7f:53:5a:e3:bc:bf:44:63:d8:2d:26:c0:41` for `mymachine.domain.com` on port 22
Oct 24, 2011 2:30:38 AM net.schmizz.sshj.transport.TransportImpl setService
INFO: Setting active service to null-service
Oct 24, 2011 2:30:38 AM com.test.jaws.execution.ssh.impl.SSHJClientImpl$ExceptionHandler handleSevereCondition
SEVERE: mymachine.domain.com is not added to your /x/home/coonradt/.ssh/known_hosts file.
Throwable occurred: net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify `ssh-rsa` host key with fingerprint `ca:0b:b3:7f:53:5a:e3:bc:bf:44:63:d8:2d:26:c0:41` for `mymachine.domain.com` on port 22
at net.schmizz.sshj.transport.KeyExchanger.verifyHost(KeyExchanger.java:222)
at net.schmizz.sshj.transport.KeyExchanger.handle(KeyExchanger.java:373)
at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:477)
at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:127)
at net.schmizz.sshj.transport.Decoder.received(Decoder.java:195)
at net.schmizz.sshj.transport.Reader.run(Reader.java:72)
Source: (StackOverflow)
I want to create a Java application to connect to my remote Linux server with ssh. Can someone send me some docs or tutorials to begin with sshj in eclipse (I'm working in a Windows environment).
Source: (StackOverflow)
I have a shell script (bash) that executes a sqlplus command, and it works perfectly when I run it from a terminal (or SSH clients like PuTTY or Xshell), what means I can read its output. Now I created a web interface where the user can start this script, but the execution fails because this same sqlplus seems not to be executed. Have a look at the following simplified examples I created:
Test shell script (test.sh):
echo 1
sqlplus -s SIEBEL/SIEBEL@SBLQA02 @select.sql
echo 2
select.sql:
select 'a' from dual;
exit;
Output generated in a terminal:
1
'
-
a
2
Output generated on a web client (so far I tried to use Ganymed SSH-2 for Java and sshj, producing the same result):
1
2
As you can see, only the simple echo commands have generated output. What happened to the sqlplus output? Am missing anything?
Thanks in advance for any help!
Source: (StackOverflow)
I am trying to do ssh into other machine using SSHJ. PFA code below (excluded try/catch/finally blocks).
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
final SSHClient sshClient = new SSHClient();
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
sshClient.connect("test-hostname");
sshClient.authPublickey("test-user", private_key_path);
Session session = sshClient.startSession();
Session.Command cmd = session.exec(TEST_SSH_COMMAND);
cmd.join(5, TimeUnit.SECONDS);
if(cmd.getExitStatus() == 0) {
System.out.println("Success");
}
When I try to execute the above program I am getting following error
[reader] n.s.sshj.transport.TransportImpl - Dying because -net.schmizz.sshj.transport.TransportException: Unable to reach a settlement: [] and [aes128-ctr, aes192-ctr, aes256-ctr, arcfour256, arcfour128, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, aes192-cbc, aes256-cbc, arcfour, rijndael-cbc@lysator.liu.se]
2014-07-01 20:45:09,021 INFO [reader] n.s.sshj.transport.TransportImpl - Disconnected - UNKNOWN
2014-07-01 20:45:09,023 ERROR [pool-3-thread-1] net.schmizz.concurrent.Promise - <<kex done>> woke to: net.schmizz.sshj.transport.TransportException: Unable to reach a settlement: [] and [aes128-ctr, aes192-ctr, aes256-ctr, arcfour256, arcfour128, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, aes192-cbc, aes256-cbc, arcfour, rijndael-cbc@lysator.liu.se]
2014-07-01 20:45:09,024 INFO [pool-3-thread-1] n.s.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION
Can someone help me to debug the issue.
Thanks.
Source: (StackOverflow)
How would you go about getting sshj installed, considering that I'm using Eclipse on ubuntu? I haven't done anything with Java in 2 years and the only thing I sort of remember is how to add a jar file to a project, but in the case of sshj, I only have .java files.
Source: (StackOverflow)
I am using the sshj library for java.
I need to open an ssh session on a remote server, execute a command which could run for many minutes, and then continue on executing in my java code. Once the command running on the remote server is finished, i would like the ssh session to close, and the sshclient to disconnect.
I have seen the following code example:
public class Exec {
public static void main(String... args) throws IOException {
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
}
}
}
Basically I don't want to wait for the command to finish (no cmd.join
call) and I need the session.close()
and ssh.disconnect()
to be called automatically once the command has exited. Is this possible?
Source: (StackOverflow)
I am trying to execute a sudo command on my Amazon EC2 machine using the SSHJ library (https://github.com/shikhar/sshj). Unfortunately, I am not getting any response from the server. I know for sure that the other non-sudo commands get executed flawlessly. Here is some sample code.
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
sshClient.connect(host, 22);
if (privateKeyFile != null) {
// authenticate using private key file.
PKCS8KeyFile keyFile = new PKCS8KeyFile();
keyFile.init(privateKeyFile);
sshClient.authPublickey(user, keyFile);
} else {
// Authenticate using password.
sshClient.authPassword(user, password);
}
// Start a new session
session = sshClient.startSession();
session.allocatePTY("vt220", 80,24,0,0,Collections.<PTYMode, Integer>emptyMap());
Command cmd = null;
String response = null;
try (Session session = sshClient.startSession()) {
cmd = session.exec("sudo service riak start");
response = IOUtils.readFully(cmd.getInputStream()).toString();
cmd.join(timeout, timeUnit);
} finally {
if (cmd != null) {
cmd.close();
}
}
Source: (StackOverflow)
I'm using SSHJ and ExpectIt to send multiple commands to a virtual machine hosted on Amazon's EC2. I get no errors, but ExpectIt executes only the first command and forgets the rest. Can you find out what am I doing wrong here?
{
SSHClient ssh=new SSHClient();
Session session;
Shell shell;
session=ssh.startSession();
session.allocateDefaultPTY();
shell=session.startShell();
Expect expect = new ExpectBuilder()
.withOutput(shell.getOutputStream())
.withInputs(shell.getInputStream(), shell.getErrorStream())
.build();
expect.sendLine("sudo useradd "+uname+" -g sftponly -m -d /home/"+uname); //Only this command is getting executed
expect.sendLine("sudo passwd "+uname);
expect.sendLine(pwd);
statusbar.setText("Assigning access key to user...");
expect.sendLine("sudo mkdir /home/"+uname+"/.ssh");
expect.sendLine("sudo touch /home/"+uname+"/.ssh/authorized_keys");
expect.sendLine("sudo echo "+pemfile+">/home/"+uname+"/.ssh/authorized_keys");
statusbar.setText("Providing permissions to user...");
expect.sendLine("sudo chown root /home/"+uname);
expect.sendLine("sudo chmod go-w /home/"+uname);
expect.sendLine("sudo mkdir /home/"+uname+"/"+uname);
expect.sendLine("sudo chmod ug+rwX /home/"+uname);
expect.sendLine("sudo chmod 700 /home/"+uname+"/.ssh");
expect.sendLine("sudo chmod 600 /home/"+uname+"/.ssh/authorized_keys");
expect.sendLine("sudo chmod 755 /home/"+uname);
statusicon.setForeground(Color.green);
statusbar.setText("User created!");
expect.close();
}
NOTE: Some sensitive (but irrelevant to this question) code has been omitted from this code.
Source: (StackOverflow)
I'm on a Windows machine, and I need to write some Java code that connects to a remote Solaris machine by using SSH and can then execute commands on it and get their output, but I'm having some trouble authenticating. (I'm using SSHJ 0.8.1)
Here's the code for connecting and starting a session:
public void init(String address, int portNumber, String hostKeyVerifier) throws IOException {
client = new SSHClient();
client.getConnection().setTimeout(300);
client.addHostKeyVerifier(hostKeyVerifier);
client.connect(address, portNumber);
client.authPassword("user", "password");
session = client.startSession();
}
I'd expect this to result in a working connection, since trying to connect to instances of Apache Karaf with the same code (albeit with a different host key verifier, address, port and credentials) works. Instead, this is the output I get:
16:27:58.029 [main] INFO n.schmizz.sshj.common.SecurityUtils - BouncyCastle not registered, using the default JCE provider
16:27:58.159 [main] INFO n.s.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0_8_1_SNAPSHOT
16:27:58.159 [main] INFO n.s.sshj.transport.TransportImpl - Server identity string: SSH-2.0-Sun_SSH_2.2
16:27:58.159 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<kex done>> to `null`
16:27:58.159 [main] DEBUG n.s.sshj.transport.KeyExchanger - Sending SSH_MSG_KEXINIT
16:27:58.169 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<kexinit sent>> to `SOME`
16:27:58.169 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<kex done>>
16:27:58.189 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Received SSH_MSG_KEXINIT
16:27:58.189 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Negotiated algorithms: [ kex=diffie-hellman-group1-sha1; sig=ssh-rsa; c2sCipher=aes128-ctr; s2cCipher=aes128-ctr; c2sMAC=hmac-sha1; s2cMAC=hmac-sha1; c2sComp=none; s2cComp=none ]
16:27:58.209 [reader] DEBUG net.schmizz.sshj.transport.kex.DHG1 - Sending SSH_MSG_KEXDH_INIT
16:27:58.239 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Received kex followup data
16:27:58.239 [reader] DEBUG net.schmizz.sshj.transport.kex.DHG1 - Received SSH_MSG_KEXDH_REPLY
16:27:58.249 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Trying to verify host key with net.schmizz.sshj.SSHClient$1@135bdd3
16:27:58.249 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Sending SSH_MSG_NEWKEYS
16:27:58.249 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Received SSH_MSG_NEWKEYS
16:27:58.249 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<kexinit sent>> to `null`
16:27:58.249 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<kex done>> to `SOME`
16:27:58.249 [main] DEBUG net.schmizz.sshj.SSHClient - Key exchange took 0.09 seconds
16:27:58.249 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<service accept>> to `null`
16:27:58.249 [main] DEBUG n.s.sshj.transport.TransportImpl - Sending SSH_MSG_SERVICE_REQUEST for ssh-userauth
16:27:58.249 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<service accept>>
16:27:58.309 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<service accept>> to `SOME`
16:27:58.309 [main] DEBUG n.s.sshj.transport.TransportImpl - Setting active service to ssh-userauth
16:27:58.309 [main] DEBUG n.schmizz.sshj.userauth.UserAuthImpl - Trying `password` auth...
16:27:58.309 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `null`
16:27:58.309 [main] DEBUG n.s.s.userauth.method.AuthPassword - Requesting password for [AccountResource] kc@10.82.82.6
16:27:58.309 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<authenticated>>
16:28:28.320 [main] DEBUG n.schmizz.sshj.userauth.UserAuthImpl - `password` auth failed
16:28:28.320 [main] DEBUG n.schmizz.sshj.userauth.UserAuthImpl - Saving for later - net.schmizz.sshj.userauth.UserAuthException: Timeout expired
16:28:28.320 [main] DEBUG n.schmizz.sshj.userauth.UserAuthImpl - Trying `keyboard-interactive` auth...
16:28:28.321 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `null`
16:28:28.321 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<authenticated>>
16:28:43.363 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `SOME`
16:28:43.363 [main] DEBUG n.schmizz.sshj.userauth.UserAuthImpl - `keyboard-interactive` auth successful
16:28:43.365 [main] DEBUG n.s.sshj.transport.TransportImpl - Setting active service to ssh-connection
16:28:43.366 [main] DEBUG n.s.sshj.connection.ConnectionImpl - Attaching `session` channel (#0)
16:28:43.366 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<chan#0 / open>>
16:29:28.387 [reader] DEBUG n.s.sshj.transport.TransportImpl - Received SSH_MSG_UNIMPLEMENTED #5
16:29:28.397 [reader] ERROR n.s.sshj.transport.TransportImpl - Dying because - net.schmizz.sshj.common.SSHException: [PROTOCOL_ERROR] Unexpected: SSH_MSG_UNIMPLEMENTED
16:29:28.397 [reader] INFO n.s.sshj.transport.TransportImpl - Disconnected - PROTOCOL_ERROR
16:29:28.398 [reader] DEBUG n.s.sshj.transport.KeyExchanger - Got notified of net.schmizz.sshj.common.SSHException: [PROTOCOL_ERROR] Unexpected: SSH_MSG_UNIMPLEMENTED
16:29:28.398 [reader] DEBUG n.s.sshj.connection.ConnectionImpl - Notified of net.schmizz.sshj.common.SSHException: [PROTOCOL_ERROR] Unexpected: SSH_MSG_UNIMPLEMENTED
16:29:28.398 [reader] DEBUG n.s.s.c.c.direct.SessionChannel - Channel #0 got notified of net.schmizz.sshj.common.SSHException: [PROTOCOL_ERROR] Unexpected: SSH_MSG_UNIMPLEMENTED
16:29:28.398 [reader] DEBUG n.s.sshj.connection.ConnectionImpl - Forgetting `session` channel (#0)
16:29:28.398 [main] ERROR net.schmizz.concurrent.Promise - <<chan#0 / open>> woke to: net.schmizz.sshj.connection.ConnectionException: Unexpected: SSH_MSG_UNIMPLEMENTED
16:29:28.398 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<chan#0 / close>> to `SOME`
16:29:28.398 [reader] DEBUG n.s.sshj.transport.TransportImpl - Setting active service to null-service
16:29:28.398 [reader] DEBUG n.s.sshj.transport.TransportImpl - Sending SSH_MSG_DISCONNECT: reason=[PROTOCOL_ERROR], msg=[Unexpected: SSH_MSG_UNIMPLEMENTED]
16:29:28.398 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<transport close>> to `SOME`
16:29:28.398 [reader] DEBUG net.schmizz.sshj.transport.Reader - Stopping
Also, this is the stack trace I get in Eclipse:
net.schmizz.sshj.connection.ConnectionException: Unexpected: SSH_MSG_UNIMPLEMENTED
at net.schmizz.sshj.connection.ConnectionException$1.chain(ConnectionException.java:32)
at net.schmizz.sshj.connection.ConnectionException$1.chain(ConnectionException.java:26)
at net.schmizz.concurrent.Promise.deliverError(Promise.java:95)
at net.schmizz.concurrent.Event.deliverError(Event.java:72)
at net.schmizz.concurrent.ErrorDeliveryUtil.alertEvents(ErrorDeliveryUtil.java:34)
at net.schmizz.sshj.connection.channel.AbstractChannel.notifyError(AbstractChannel.java:241)
at net.schmizz.sshj.connection.channel.direct.SessionChannel.notifyError(SessionChannel.java:249)
at net.schmizz.sshj.common.ErrorNotifiable$Util.alertAll(ErrorNotifiable.java:35)
at net.schmizz.sshj.connection.ConnectionImpl.notifyError(ConnectionImpl.java:250)
at net.schmizz.sshj.transport.TransportImpl.die(TransportImpl.java:578)
at net.schmizz.sshj.transport.Reader.run(Reader.java:79)
Caused by: net.schmizz.sshj.common.SSHException: [PROTOCOL_ERROR] Unexpected: SSH_MSG_UNIMPLEMENTED
at net.schmizz.sshj.AbstractService.notifyUnimplemented(AbstractService.java:66)
at net.schmizz.sshj.transport.TransportImpl.gotUnimplemented(TransportImpl.java:555)
at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:490)
at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:127)
at net.schmizz.sshj.transport.Decoder.received(Decoder.java:195)
at net.schmizz.sshj.transport.Reader.run(Reader.java:72)
PuTTY is capable of connecting to that same address and port, using the same credentials, with no issues, though it does take some time. What should I do to get a working connection with Java here?
Source: (StackOverflow)
I created an instance in the Google Compute Engine and I need connect using SSHJ, but I'm not getting. I can connect successfully using the AWS:
public String exec(String host, String keyPair, String script) throws IOException{
File keyPairFile = new File(keyPair);
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(host);
PKCS8KeyFile keyFile = new PKCS8KeyFile();
keyFile.init(keyPairFile);
ssh.auth(USER_DEFAULT, new AuthPublickey(keyFile));
Session session = null;
try {
logger.info("Conectando via ssh " + host + "...");
session = ssh.startSession();
final Command command = session.exec(script);
String response = IOUtils.readFully(command.getInputStream()).toString();
command.join(10, TimeUnit.SECONDS);
return response;
} finally {
session.close();
ssh.disconnect();
ssh.close();
}
}
Google Compute Engine to do?
I created a public key and a private key using puttygen.Then I accessed the console of my Google Compute Engine and imported the public key. That way I could access with putty using the private key.
When I try to access with SSHJ using the exec method defined above, the error occurs:
Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods
at net.schmizz.sshj.userauth.UserAuthImpl.authenticate(UserAuthImpl.java:114)
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:205)
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:190)
at br.com.clouddeploy.service.SSHConnect.exec(SSHConnect.java:31)
at br.com.clouddeploy.main.TestConecte.main(TestConecte.java:17)
Caused by: net.schmizz.sshj.userauth.UserAuthException: Problem getting public key from PKCS8KeyFile{resource=[PrivateKeyFileResource] E:\Essencial\SkyDrive\MESTRADO\Disciplinas\2014 e 2015\EDD\Pesquisa\google-teste.ppk}
at net.schmizz.sshj.userauth.method.KeyedAuthMethod.putPubKey(KeyedAuthMethod.java:46)
at net.schmizz.sshj.userauth.method.AuthPublickey.buildReq(AuthPublickey.java:62)
at net.schmizz.sshj.userauth.method.AuthPublickey.buildReq(AuthPublickey.java:81)
at net.schmizz.sshj.userauth.method.AbstractAuthMethod.request(AbstractAuthMethod.java:63)
at net.schmizz.sshj.userauth.UserAuthImpl.authenticate(UserAuthImpl.java:92)
... 4 more
Caused by: java.io.IOException: Could not read key pair from: [PrivateKeyFileResource] E:\Essencial\SkyDrive\MESTRADO\Disciplinas\2014 e 2015\EDD\Pesquisa\google-teste.ppk
at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.readKeyPair(PKCS8KeyFile.java:145)
at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.getPublic(PKCS8KeyFile.java:72)
at net.schmizz.sshj.userauth.method.KeyedAuthMethod.putPubKey(KeyedAuthMethod.java:44)
... 8 more
Any suggestion?
Source: (StackOverflow)
I am trying to create a tunnel to use a service behind a firewall, that supports SSH. I wanted a complete solution in java, but I cannot seem to get it to work. I found this github snip and based on that I created the following code to keep a background thread giving me the tunnel:
// property on surrounding class
// static final SSHClient sshclient = new SSHClient();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
String host = "10.0.3.96";
sshclient.useCompression();
sshclient.addHostKeyVerifier("30:68:2a:20:21:9f:c8:e8:ac:b4:a7:fc:2d:a7:d0:26");
sshclient.connect(host);
sshclient.authPassword("messy", "messy");
if (!sshclient.isAuthenticated()) {
throw new RuntimeException(String.format("Unable to authenticate against '%s'", host));
}
Forward forward = new Forward(8111);
InetSocketAddress addr = new InetSocketAddress("google.com", 80);
SocketForwardingConnectListener listener = new SocketForwardingConnectListener(addr);
sshclient.getRemotePortForwarder().bind(forward, listener);
sshclient.getTransport().setHeartbeatInterval(30);
HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8111").openConnection();
con.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while( (line = reader.readLine()) != null) {
System.out.println(line);
}
sshclient.getTransport().join();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (sshclient != null && sshclient.isConnected()) {
sshclient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.setDaemon(true);
thread.start();
The problem is that if I connect to a remote SSH server like '10.0.3.96' it does not work. If I use the local SSH server on my localhost it will work. I have been going thru different configurations with any luck and tried debugging, but I cannot grasp what is going on inside the SSHj package.
Now it does not have to be a SSHj solution, but that would be preferred since other parts of the code are fully implemented and using SSHj and I do not want to mix two SSH packages in one project.
Thanks for any help.
Source: (StackOverflow)
I'm trying to open an SSH client session from my Android app. Trying to connect to a device on the local network (a Raspberry Pi). I'm using the SSHJ library version 0.10.0. It fails on the ssh.connect()
call, with a TransportException
which is ultimately caused by a NoSuchAlgorithmException
. Refer exception tree below.
SSHClient ssh = new SSHClient(new AndroidConfig());
Session session = null;
try {
//ssh.loadKnownHosts();
// Exception thrown on this line
ssh.connect("192.168.1.109", 22);
// Doesn't reach below
ssh.authPassword("user", "password");
session = ssh.startSession();
}
catch (net.schmizz.sshj.transport.TransportException ex) {
;
}
Exception tree:
net.schmizz.sshj.transport.TransportException
net.schmizz.sshj.common.SSHException
net.schmizz.sshj.common.SSHRuntimeException
java.security.GeneralSecurityException: java.security.NoSuchAlgorithmException: KeyFactory ECDSA implementation not found
java.security.NoSuchAlgorithmException: KeyFactory ECDSA implementation not found
Other system info:
SSHJ library : v0.10.0
Android device : Galaxy Note 3 running Android 4.4.2
I used the maven dependency support in Android Studio to bring in the SSHJ JAR and it pulled in the following three libraries in addition to the SSHJ v0.10.0 jar...
bouncy castle...
bcpkix-jdk15on-1.50.jar
bcprov-jdk15on-1.50.jar
logging....
slf4j-api-1.7.7.jar
Don't have a clue where to start with this exception ... any suggestions appreciated! Thanks.
UPDATE: 31-Oct-2014
As suggested by LeeDavidPainter, I included the SpongyCastle 1.51.0 JAR and added this line at the top:
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
I'm now getting a different exception on the same line:
net.schmizz.sshj.transport.TransportException
net.schmizz.sshj.common.SSHException
net.schmizz.sshj.common.SSHRuntimeException
java.security.GeneralSecurityException: java.security.spec.InvalidKeySpecException: key spec not recognised
java.security.spec.InvalidKeySpecException: key spec not recognised
Also note I tried the following line as well, with the same result:
Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
I have another app on my phone which is basically doing exactly what I want to achieve - its called RaspberryPiController - it connects to your RPi over SSH with username and password auth. This works fine, so it would seem its not a network issue.
Source: (StackOverflow)
I want to connect to an Amazon EC2 terminal via JAVA API and perform sudo operations. I ended up using SSHJ library because I found its interface very simple and easy to use. The nice thing is that I can even execute sudo operations via this library. Here is some sample code:
// Start a new session
session = sshClient.startSession();
session.allocatePTY("vt220", 80,24,0,0,Collections.emptyMap());
Command cmd = null;
String response = null;
// your allocating a new session there
try (Session session = sshClient.startSession()) {
cmd = session.exec("sudo service riak start");
response = IOUtils.readFully(cmd.getInputStream()).toString();
cmd.join(timeout, timeUnit);
} finally {
if (cmd != null)
cmd.close();
}
However, the response, I received back had control characters and wanted to convert them into plain text.
Starting riak: [60G[[0;32m OK [0;39
Source: (StackOverflow)
I have been using sshj libraries
<dependency>
<groupId>net.schmizz</groupId>
<artifactId>sshj</artifactId>
<version>0.3.1</version>
</dependency>
Following was my code using 0.3.1 which worked fine for doing uploading of files supporting wildcard patterns.
SSHClient client = null;
SCPUploadClient uploader = null;
try {
client = getClient();
uploader = client.newSCPFileTransfer().newSCPUploadClient();
uploader.setFileFilter(new WildcardFileFilter(wildCardPattern));
//determine the remote directory
File f = new File(localDirLocation);
String dir = remoteDirLocation + f.getName();
uploader.copy(localDirLocation, remoteDirLocation);
} catch (IOException e) {
//processing exceptions here
} finally {
disconnectClient(client);
}
But now the code gives me a lot of compilation errors when I tried moving over to 0.5.0.
I would like to understand how do I go about setting fileFilters when I want to do uploading and downloading of files from local to remote machine and vice versa
Can someone please help me with this ?
Source: (StackOverflow)