Search code examples
linuxbashubuntuudpxinetd

Pass Arguments to Bash Script when xinetd receive message via udp


i was wondering if its possible to pass args to a bash script when xinetd receive a command from a random host (in lan) via udp. To clearify: when i send e.g. hello from a random client to the xinetd server i want the xinetd server to pass the hello to my specified bash script. Same thing with world.

here is my xinetd service file:

service test
{
        socket_type = dgram
        protocol    = udp
        port        = 65534
        type        = UNLISTED
        wait        = yes
        user        = root
        server      = /root/sendmail
        server_args = **Received Message from UDP connection**
}

Kind regards and thank you very much!


Solution

  • You cannot receive the message inside your bash script, because there simply is no stream to receive on stdin.

    Using UDP, you are sending datagrams which are handled differently by xinetd.

    Receiving TCP Streams:

    xinetd establishes a connection with your source host and forks an instance of your given script. The stdin (standard input) get's connected to stream of the source host. Then you can read the input or do whatever you would do with a locally initiated run of your sendmail process.

    By the way: The stdout get's connected to your source hosts stdin as well and you can see answers or other messages, if you need bidirectional communication.

    Receiving UDP Datagrams:

    xinetd receives a datagram on your port and runs your script before even reading what it received. The (still unconnected) socket will be handed over to your script.

    You need to do the full handling of reading datagrams by yourself, which is not as trivial as reading and writing streams. Also, this usually needs something more than a simple bash script.

    (See recvfrom and sendto if you are interested in doing this)


    From my perspective I'd recommend you to just switch over to a TCP stream for this. This will also ensure that your commands will actually reach your host or throw an exception on your source host for futher handling.

    Edit: This is how your xinetd service would look like:

    service test
    {
            socket_type = stream
            protocol    = tcp
            port        = 65534
            user        = root
            server      = /root/sendmail
    }