Executing the Unix commands remotely by java program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Executing the Unix commands remotely by java program

  1. #1
    Join Date
    May 2008
    Posts
    2

    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:

  2. #2
    Join Date
    May 2008
    Posts
    2
    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.

  3. #3
    Join Date
    May 2008
    Posts
    2
    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

  4. #4
    Join Date
    May 2008
    Posts
    2
    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);

  5. #5
    Join Date
    May 2007
    Posts
    843
    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.

  6. #6
    Join Date
    Dec 2008
    Posts
    1
    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();

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    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

  1. Pseudocode Problem
    By zobi316 in forum VB Classic
    Replies: 3
    Last Post: 03-10-2008, 08:05 AM
  2. Help! Simple Java Program.
    By SlickWilly440 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 10:17 PM
  3. Replies: 1
    Last Post: 01-03-2006, 07:45 AM
  4. operating system program in java
    By bina in forum Java
    Replies: 2
    Last Post: 09-18-2001, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center