Jsch - Sending command over SSH connection

Jagson

I want to send a simple command with a java application over my windows computer to activate a python script on my raspberry pi which should control the GPIO ports of my raspberry Pi.

I don't get really into Jsch, my SSH connection already works but not sending commands.

Here is my actual code:

package sshtest;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.jcraft.jsch.*; 
import java.io.InputStream;

public class SSHTest {


public static void main(String[] args) {
    String host="DELETED";
    String user="pi";
    String password="DELETED";
    String command =("cd Desktop");
    String command2 =("sudo python IO2.py all high");
    try{

        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        Session session=jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        System.out.println("Connected");

        Channel channel = session.openChannel("exec");
        InputStream is = new ByteArrayInputStream(command.getBytes());
        channel.setInputStream(is);
        InputStream iss = new ByteArrayInputStream(command2.getBytes());
        channel.setInputStream(iss);





        session.disconnect();

    }catch(Exception e){
        e.printStackTrace();
    }
}
}
chemikadze

You should use ChannelExec.setCommand(java.lang.String) to start it, and wait while Channel.isClosed() returns false. BTW, JSch has an example for this: http://www.jcraft.com/jsch/examples/Exec.java.html.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jsch - Sending command over SSH connection

From Dev

execute command over ssh connection

From Dev

How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

From Dev

How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

From Dev

Sending data over a connection

From Dev

Reverse Tunnel over JSCH (SSH) and HTTPS

From Dev

SSH connection using JSCH, UnknownHostException on Android

From Dev

JSch create a embed ssh connection but ServerAliveInterval not works

From Dev

Sequelize connection over ssh

From Dev

Need to run remote command over SSH with a pause before sending remote command

From Dev

Reverse SCP over SSH connection

From Dev

JSch SSH connection throws NPE when initializing reverse tunnel

From Dev

Open putty ssh connection over SOCKS5 proxy via command line

From Dev

Android: Sending multiple messages over a sslsocket connection

From Dev

Python: sending key press events over SSH

From Dev

Sending keypresses to remote X session over SSH

From Dev

sending an http request over ssh server

From Dev

What is the protocol for sending files over SSH, in code?

From Dev

Different output of command over ssh

From Java

Execute remote ssh command "which java" (JSch java)

From Dev

Running telnet command on remote SSH session using JSch

From Dev

JSch: Run command after Multi-Level ssh

From Dev

Ssh and run command upon connection

From Dev

Running ssh command and keeping connection

From Dev

Ssh and run command upon connection

From Dev

Execute command on successful SSH connection

From Dev

Sending files over Samba with command line

From Dev

Forward Internet connection over ssh to remote computer

From Dev

Transfer a file over a unstable SSH connection

Related Related

  1. 1

    Jsch - Sending command over SSH connection

  2. 2

    execute command over ssh connection

  3. 3

    How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

  4. 4

    How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

  5. 5

    Sending data over a connection

  6. 6

    Reverse Tunnel over JSCH (SSH) and HTTPS

  7. 7

    SSH connection using JSCH, UnknownHostException on Android

  8. 8

    JSch create a embed ssh connection but ServerAliveInterval not works

  9. 9

    Sequelize connection over ssh

  10. 10

    Need to run remote command over SSH with a pause before sending remote command

  11. 11

    Reverse SCP over SSH connection

  12. 12

    JSch SSH connection throws NPE when initializing reverse tunnel

  13. 13

    Open putty ssh connection over SOCKS5 proxy via command line

  14. 14

    Android: Sending multiple messages over a sslsocket connection

  15. 15

    Python: sending key press events over SSH

  16. 16

    Sending keypresses to remote X session over SSH

  17. 17

    sending an http request over ssh server

  18. 18

    What is the protocol for sending files over SSH, in code?

  19. 19

    Different output of command over ssh

  20. 20

    Execute remote ssh command "which java" (JSch java)

  21. 21

    Running telnet command on remote SSH session using JSch

  22. 22

    JSch: Run command after Multi-Level ssh

  23. 23

    Ssh and run command upon connection

  24. 24

    Running ssh command and keeping connection

  25. 25

    Ssh and run command upon connection

  26. 26

    Execute command on successful SSH connection

  27. 27

    Sending files over Samba with command line

  28. 28

    Forward Internet connection over ssh to remote computer

  29. 29

    Transfer a file over a unstable SSH connection

HotTag

Archive