Connect to UNIX


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Connect to UNIX

  1. #1
    VAX Guest

    Connect to UNIX


    Could anyone tell me how can I write a Java program to connect to UNIX and
    run a command?

    Thank you.

  2. #2
    Cirrus Guest

    Re: Connect to UNIX


    Implment a mini version of telent ... its pretty easy.

    Cheers!!
    - Cirrus.


    "VAX" <dunga0@yahoo.com> wrote:
    >
    >Could anyone tell me how can I write a Java program to connect to UNIX and
    >run a command?
    >
    >Thank you.



  3. #3
    PoorMan Guest

    Re: Connect to UNIX

    Hi Cirrus, do u hv more info. or url about this?

    Thanks!

    PoorMan

    "Cirrus" <cirrus25@yahoo.com> wrote in message
    news:3b583cf8$1@news.devx.com...
    >
    > Implment a mini version of telent ... its pretty easy.
    >
    > Cheers!!
    > - Cirrus.
    >
    >
    > "VAX" <dunga0@yahoo.com> wrote:
    > >
    > >Could anyone tell me how can I write a Java program to connect to UNIX

    and
    > >run a command?
    > >
    > >Thank you.

    >




  4. #4
    flairn Guest

    Re: Connect to UNIX


    "VAX" <dunga0@yahoo.com> wrote:
    >
    >Could anyone tell me how can I write a Java program to connect to UNIX and
    >run a command?
    >
    >Thank you.


    You could create your own server-side java program on the Unix side (to capture
    commands from a client-side java program) to relay commands to Unix, capture
    the response, and send it back to the client-side program.

    You could communicate between the two sides using "DatagramSocket"/"DatagramPacket"
    classes and methods, among others.

    You might have to create a temp file on the Unix side to capture the output/response
    from the commands you issue so you can read it and send it back to the client-side
    program...e.g.,



    public void send(String s)
    {

    String ss = null;
    try
    {
    ss = "grep -l "
    + s
    + " /etc/a*";

    System.out.println( "\nSending packet containing: " + ss + "\n"
    );
    }


    byte data[] = ss.getBytes();
    InetAddress mfAddress = InetAddress.getByName("xxx.xxx.xxx.xxx");
    sendPacket = new DatagramPacket( data, data.length, mfAddress, 6969);
    socket.send( sendPacket );

    }
    }


    public void waitForPackets()
    {
    while ( true )
    {
    byte data[] = new byte[ 500 ];
    receivePacket = new DatagramPacket( data, data.length );
    socket.receive( receivePacket );

    String x = new String (receivePacket.getData(), 0, receivePacket.getLength()
    );
    System.out.println("\nline from Server program: " + x);
    }
    }





  5. #5
    flairn Guest

    Re: Connect to UNIX


    "VAX" <dunga0@yahoo.com> wrote:
    >
    >Could anyone tell me how can I write a Java program to connect to UNIX and
    >run a command?
    >
    >Thank you.


    You could create your own server-side java program on the Unix side (to capture
    commands from a client-side java program) to relay commands to Unix, capture
    the response, and send it back to the client-side program.

    e.g.,

    *****partial client-side code example*****
    DatagramPacket sendPacket, receivePacket;
    DatagramSocket socket;
    -
    -
    -
    //send command to Unix server-side program

    String ss = "grep -l apple /etc/*";

    System.out.println( "\nSending packet containing: " + ss + "\n" );

    byte data[] = ss.getBytes();
    InetAddress mfAddress = InetAddress.getByName("xxx.xxx.xxx.xxx");
    sendPacket = new DatagramPacket( data, data.length, mfAddress, 6969);
    socket.send( sendPacket );


    //wait for response
    while ( true )
    {
    byte data[] = new byte[ 200 ];
    receivePacket = new DatagramPacket( data, data.length );
    socket.receive( receivePacket );
    String x = new String (receivePacket.getData(), 0, receivePacket.getLength()
    );
    System.out.println("\nresponse from server-side program: " + x);
    }


    *****partial server-side code example*****
    private DatagramPacket sendPacket, receivePacket;
    private DatagramSocket socket;
    -
    -
    -
    while ( true )
    {

    byte data[] = new byte[ 200];
    receivePacket = new DatagramPacket( data, data.length );
    socket.receive( receivePacket );
    String sCommand = new String(receivePacket.getData(), 0, receivePacket.getLength());

    System.out.println("\ncommand received from client-side program: " + sCommand);

    //execute shell command received
    Runtime rt = Runtime.getRuntime();

    String[] sCommand2 = null;
    sCommand2 = new String[3];
    sCommand2[0] = "/usr/bin/sh";
    sCommand2[1] = "-c";
    sCommand2[2] = (sCommand);
    Process p = rt.exec(sCommand2);


    //notify client that command was executed...

    String sCommandExecuted = ("\nCommandExecuted: "
    + sCommand2[0]
    + sCommand2[1]
    + sCommand2[2]
    );
    sendPacket = new DatagramPacket(sCommandExecuted.getBytes(),
    sCommandExecuted.length(),
    receivePacket.getAddress(),
    receivePacket.getPort() );
    socket.send( sendPacket );
    }



  6. #6
    Join Date
    Oct 2006
    Posts
    1

    Connect to Unix

    Could you please elaborate the source code for connecting the unix box through java program.

    Is there any way of connecting the unix box directly instead of client script(java class running in the windows) calling the Server script(java class placed at unix environment)?

    please get back to me as soon as possible

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