-
Executing the Unix commands remotely by java program
Hi All,
I want to execute commands in putty remotely by java program.
Thru putty software,I am logging in to other machine and I want execute some commands(like unix commands) remotely by java program.
Please let me know how to achive this.
Thanks in advance
Regards
Prasad:
-
You can write a socket program to connect telnet on port 25, and you can run all the commands through that on the remote machine. You can build a UI if required.
-
Hi RameshBowrisett,
I am using putty to connect the remote machine. After getting connected thru remote machine ,I am logging using username and password thereafter I am executing the UNix commands. Please can U provide one example.
Thanks for the reply .
Thanks and regards,
Prasad
-
Here is the sample code, its very simple and quite easy.
Socket s = new Socket(RemoteServer, 25);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write(UnixCommand + "\n"); ==> Here you can enter uid and pwd and other unix commands.
out.flush();
String res = in.readLine(); ==> here you will read the response from remote machine
Hope this will help you to go ahead and start.
Ramesh
System.out.println("UnixCommand response "+ res);
-
Why you connect first before enter username and password ?
After login, you can invoke your program which is with UI, that program allow to execute Unix command.
-
I am also working on similar problem. Below is my code, but I am not able to see any output. Please help!!
Code:
String username = args[0].substring(0, args[0].indexOf("@"));
String hostname = args[0].substring(args[0].indexOf("@") + 1,args[0].length());
// Create a connection to server
Socket s = new Socket(hostname, 25);
// Create input and output streams to socket
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
wr.write(username + "\n");
wr.write(pwd + "\n");
wr.flush();
wr.write("/bin/sh -c ls -l");
wr.flush();
// Read response from socket
BufferedReader rd = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = rd.readLine();
while (line != null) {
System.out.println(line);
line = rd.readLine();
}
s.close();
-
Are you getting any errors or just no output?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
Similar Threads
-
By zobi316 in forum VB Classic
Replies: 3
Last Post: 03-10-2008, 07:05 AM
-
By SlickWilly440 in forum Java
Replies: 2
Last Post: 01-28-2006, 09:17 PM
-
By shiwalee in forum Java
Replies: 1
Last Post: 01-03-2006, 06:45 AM
-
Replies: 2
Last Post: 09-18-2001, 11:52 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks