• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Remote command to server

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I am trying to connect to a server and execute commands remotely to that server via java code.
Unfortunately every code I could run (using JSch, since that's what google has mostly recommended  me) has resulted me in soooo many different types of errors for every tutorial.
Could anyone guide me for this task?
Let's sayI want to execute the command "ls -al". How could I connect to that server properly and execute the command?
Thank you in advance!
 
Saloon Keeper
Posts: 28102
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Darius Boteand
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, no... that's the terminal command...
 
Tim Holloway
Saloon Keeper
Posts: 28102
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Missed the "java code" part.

I guess I've used jsch unawares, since I have an Eclipse remote shell plugin and that's what it uses. Never saw a problem.

You can of course, simply do a Runtime.exec-style command execution providing you're not using Windows, where you'd have to install PuTTY and use its ssh client instead.

But if you can provide us with the sort of errors you're getting from jsch, we'll see if you can get that flying.

Although before we begin, I would recommend that you consider whether you actually want general SSH access or if you're controlling a specific function where some sort of more specialized service would be safer. SSH is very powerful and offers multiple opportunities for exploits. A lot of times it's sufficient for me to hack up a Node.js ReST server to manage the remote system work and use http(s) as my client command interface. Plus some remote services block SSH, but almost nobody blocks HTTP/HTTPS.
 
Darius Boteand
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Thank you for your help!
I just managed to make it run.
Only I don't know exactly how to make it work with inputs:
If I echo something or use a simple bash script, it would display an output.
On the other hand, as soon as I use an input, it doesn't display anything anymore.
How should I do this?

Here is the code I have used:

 
Darius Boteand
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind, found the solution!!!
But this is very weird honestly...
How is this making sense??? instead of ./ I was supposed to only use / ...
well anyways, thanks again for the help!
 
Tim Holloway
Saloon Keeper
Posts: 28102
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're referring to "./script/main_key", leading with a dot means that the directory/file script/main_key would be looked for under the remote system's current working directory. By default, this would the the remote userid's designated home directory, although a login script might cd to somewhere else.

Meaning that here, as is so often the case, the most reliable way for an automated process to locate a file is to give its full absolute pathname (beginning with "/"), rather than a relative path.
 
Darius Boteand
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I understand better now as for why using only / with its full path!
Thank you for your help!
 
no wonder he is so sad, he hasn't seen this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic