Shell Script in netcat listener to talk with client












1














I have this script on my netcat server which asks for name and some other information:



*echo "Tell me your name"                                                    
read $ln
echo "I got '$ln'"
echo "Tell me something more"
while read ln; do
echo "I got '$ln'"
echo "Tell me something more"
done*


When a client connects to this server, I want the script to communicate with the client directly.
On server end I do : while true; do nc -l -p port-no | ./My-script-file ; done



The while loop is just so that the server continues listening even after one client has closed the connection and nothing else. But somehow, I cannot get the queries to appear on client side.



On client side I do: nc server-ip port-no



I want the lines "Tell me your name", "I got..." and "Tell me something else" to appear on client screen and the input from client end to be fed into the script.



I have also tried options like --exec, -e and --sh-exec and the errors I am getting are something like



nc: invalid option -- '-'
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
[-m minttl] [-O length] [-P proxy_username] [-p source_port]
[-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
[-X proxy_protocol] [-x proxy_address[:port]] [destination] [port]









share|improve this question





























    1














    I have this script on my netcat server which asks for name and some other information:



    *echo "Tell me your name"                                                    
    read $ln
    echo "I got '$ln'"
    echo "Tell me something more"
    while read ln; do
    echo "I got '$ln'"
    echo "Tell me something more"
    done*


    When a client connects to this server, I want the script to communicate with the client directly.
    On server end I do : while true; do nc -l -p port-no | ./My-script-file ; done



    The while loop is just so that the server continues listening even after one client has closed the connection and nothing else. But somehow, I cannot get the queries to appear on client side.



    On client side I do: nc server-ip port-no



    I want the lines "Tell me your name", "I got..." and "Tell me something else" to appear on client screen and the input from client end to be fed into the script.



    I have also tried options like --exec, -e and --sh-exec and the errors I am getting are something like



    nc: invalid option -- '-'
    usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
    [-m minttl] [-O length] [-P proxy_username] [-p source_port]
    [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
    [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port]









    share|improve this question



























      1












      1








      1







      I have this script on my netcat server which asks for name and some other information:



      *echo "Tell me your name"                                                    
      read $ln
      echo "I got '$ln'"
      echo "Tell me something more"
      while read ln; do
      echo "I got '$ln'"
      echo "Tell me something more"
      done*


      When a client connects to this server, I want the script to communicate with the client directly.
      On server end I do : while true; do nc -l -p port-no | ./My-script-file ; done



      The while loop is just so that the server continues listening even after one client has closed the connection and nothing else. But somehow, I cannot get the queries to appear on client side.



      On client side I do: nc server-ip port-no



      I want the lines "Tell me your name", "I got..." and "Tell me something else" to appear on client screen and the input from client end to be fed into the script.



      I have also tried options like --exec, -e and --sh-exec and the errors I am getting are something like



      nc: invalid option -- '-'
      usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
      [-m minttl] [-O length] [-P proxy_username] [-p source_port]
      [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
      [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port]









      share|improve this question















      I have this script on my netcat server which asks for name and some other information:



      *echo "Tell me your name"                                                    
      read $ln
      echo "I got '$ln'"
      echo "Tell me something more"
      while read ln; do
      echo "I got '$ln'"
      echo "Tell me something more"
      done*


      When a client connects to this server, I want the script to communicate with the client directly.
      On server end I do : while true; do nc -l -p port-no | ./My-script-file ; done



      The while loop is just so that the server continues listening even after one client has closed the connection and nothing else. But somehow, I cannot get the queries to appear on client side.



      On client side I do: nc server-ip port-no



      I want the lines "Tell me your name", "I got..." and "Tell me something else" to appear on client screen and the input from client end to be fed into the script.



      I have also tried options like --exec, -e and --sh-exec and the errors I am getting are something like



      nc: invalid option -- '-'
      usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
      [-m minttl] [-O length] [-P proxy_username] [-p source_port]
      [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
      [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port]






      linux shell-script shell netcat nc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 21 '18 at 19:43









      GAD3R

      25.5k1750107




      25.5k1750107










      asked Dec 21 '18 at 18:33









      Shritama Sengupta

      368




      368






















          2 Answers
          2






          active

          oldest

          votes


















          3














          It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful command to the connected machine, these commands are considered as dangerous.

          As a work around I just uninstalled Open-BSD netcat and installed traditional-netcat server following the solution in the site https://stackoverflow.com/questions/10065993/how-to-switch-to-netcat-traditional-in-ubuntu. This gave me access to the -exec command.
          The command that I run on server is : nc -l -p port-number -e File-Script-to-execute






          share|improve this answer































            1














            Ok, so here I've set up a small example:



            #!/bin/bash

            while read -p 'Tell me your name: ' ln;
            do
            echo "I got $ln";
            done


            So you save the script make it executable and run it as follows:



            On the server



            nc -l -p 4444 -k --sh-exec ./yourscript


            Here -k does keep the connection open so no need for the loop you got there.



            On the client



            nc hostname 4444
            foo bar


            This should do the job.






            share|improve this answer





















            • I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
              – Shritama Sengupta
              Dec 21 '18 at 18:53












            • @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
              – Valentin Bajrami
              Dec 21 '18 at 18:56










            • I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
              – Shritama Sengupta
              Dec 21 '18 at 19:00












            • @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
              – Valentin Bajrami
              Dec 21 '18 at 19:26










            • I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
              – Shritama Sengupta
              Dec 21 '18 at 19:29











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490371%2fshell-script-in-netcat-listener-to-talk-with-client%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful command to the connected machine, these commands are considered as dangerous.

            As a work around I just uninstalled Open-BSD netcat and installed traditional-netcat server following the solution in the site https://stackoverflow.com/questions/10065993/how-to-switch-to-netcat-traditional-in-ubuntu. This gave me access to the -exec command.
            The command that I run on server is : nc -l -p port-number -e File-Script-to-execute






            share|improve this answer




























              3














              It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful command to the connected machine, these commands are considered as dangerous.

              As a work around I just uninstalled Open-BSD netcat and installed traditional-netcat server following the solution in the site https://stackoverflow.com/questions/10065993/how-to-switch-to-netcat-traditional-in-ubuntu. This gave me access to the -exec command.
              The command that I run on server is : nc -l -p port-number -e File-Script-to-execute






              share|improve this answer


























                3












                3








                3






                It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful command to the connected machine, these commands are considered as dangerous.

                As a work around I just uninstalled Open-BSD netcat and installed traditional-netcat server following the solution in the site https://stackoverflow.com/questions/10065993/how-to-switch-to-netcat-traditional-in-ubuntu. This gave me access to the -exec command.
                The command that I run on server is : nc -l -p port-number -e File-Script-to-execute






                share|improve this answer














                It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful command to the connected machine, these commands are considered as dangerous.

                As a work around I just uninstalled Open-BSD netcat and installed traditional-netcat server following the solution in the site https://stackoverflow.com/questions/10065993/how-to-switch-to-netcat-traditional-in-ubuntu. This gave me access to the -exec command.
                The command that I run on server is : nc -l -p port-number -e File-Script-to-execute







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 22 '18 at 11:45









                Rui F Ribeiro

                39.1k1479130




                39.1k1479130










                answered Dec 21 '18 at 19:56









                Shritama Sengupta

                368




                368

























                    1














                    Ok, so here I've set up a small example:



                    #!/bin/bash

                    while read -p 'Tell me your name: ' ln;
                    do
                    echo "I got $ln";
                    done


                    So you save the script make it executable and run it as follows:



                    On the server



                    nc -l -p 4444 -k --sh-exec ./yourscript


                    Here -k does keep the connection open so no need for the loop you got there.



                    On the client



                    nc hostname 4444
                    foo bar


                    This should do the job.






                    share|improve this answer





















                    • I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
                      – Shritama Sengupta
                      Dec 21 '18 at 18:53












                    • @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
                      – Valentin Bajrami
                      Dec 21 '18 at 18:56










                    • I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
                      – Shritama Sengupta
                      Dec 21 '18 at 19:00












                    • @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
                      – Valentin Bajrami
                      Dec 21 '18 at 19:26










                    • I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
                      – Shritama Sengupta
                      Dec 21 '18 at 19:29
















                    1














                    Ok, so here I've set up a small example:



                    #!/bin/bash

                    while read -p 'Tell me your name: ' ln;
                    do
                    echo "I got $ln";
                    done


                    So you save the script make it executable and run it as follows:



                    On the server



                    nc -l -p 4444 -k --sh-exec ./yourscript


                    Here -k does keep the connection open so no need for the loop you got there.



                    On the client



                    nc hostname 4444
                    foo bar


                    This should do the job.






                    share|improve this answer





















                    • I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
                      – Shritama Sengupta
                      Dec 21 '18 at 18:53












                    • @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
                      – Valentin Bajrami
                      Dec 21 '18 at 18:56










                    • I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
                      – Shritama Sengupta
                      Dec 21 '18 at 19:00












                    • @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
                      – Valentin Bajrami
                      Dec 21 '18 at 19:26










                    • I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
                      – Shritama Sengupta
                      Dec 21 '18 at 19:29














                    1












                    1








                    1






                    Ok, so here I've set up a small example:



                    #!/bin/bash

                    while read -p 'Tell me your name: ' ln;
                    do
                    echo "I got $ln";
                    done


                    So you save the script make it executable and run it as follows:



                    On the server



                    nc -l -p 4444 -k --sh-exec ./yourscript


                    Here -k does keep the connection open so no need for the loop you got there.



                    On the client



                    nc hostname 4444
                    foo bar


                    This should do the job.






                    share|improve this answer












                    Ok, so here I've set up a small example:



                    #!/bin/bash

                    while read -p 'Tell me your name: ' ln;
                    do
                    echo "I got $ln";
                    done


                    So you save the script make it executable and run it as follows:



                    On the server



                    nc -l -p 4444 -k --sh-exec ./yourscript


                    Here -k does keep the connection open so no need for the loop you got there.



                    On the client



                    nc hostname 4444
                    foo bar


                    This should do the job.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 21 '18 at 18:49









                    Valentin Bajrami

                    5,90611627




                    5,90611627












                    • I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
                      – Shritama Sengupta
                      Dec 21 '18 at 18:53












                    • @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
                      – Valentin Bajrami
                      Dec 21 '18 at 18:56










                    • I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
                      – Shritama Sengupta
                      Dec 21 '18 at 19:00












                    • @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
                      – Valentin Bajrami
                      Dec 21 '18 at 19:26










                    • I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
                      – Shritama Sengupta
                      Dec 21 '18 at 19:29


















                    • I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
                      – Shritama Sengupta
                      Dec 21 '18 at 18:53












                    • @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
                      – Valentin Bajrami
                      Dec 21 '18 at 18:56










                    • I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
                      – Shritama Sengupta
                      Dec 21 '18 at 19:00












                    • @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
                      – Valentin Bajrami
                      Dec 21 '18 at 19:26










                    • I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
                      – Shritama Sengupta
                      Dec 21 '18 at 19:29
















                    I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
                    – Shritama Sengupta
                    Dec 21 '18 at 18:53






                    I have tried using the --exec command before and it didn't work. I tried your command and this is what I got: root@kali:~/Desktop# nc -l -p 4444 -k --sh-exec ./Test-Script nc: invalid option -- '-' usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [destination] [port] root@kali:~/Desktop#
                    – Shritama Sengupta
                    Dec 21 '18 at 18:53














                    @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
                    – Valentin Bajrami
                    Dec 21 '18 at 18:56




                    @ShritamaSengupta what version of netcat are you using? Does nc --help show you the option --sh-exec ?
                    – Valentin Bajrami
                    Dec 21 '18 at 18:56












                    I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
                    – Shritama Sengupta
                    Dec 21 '18 at 19:00






                    I got this:nc -h OpenBSD netcat (Debian patchlevel 1.195-1)
                    – Shritama Sengupta
                    Dec 21 '18 at 19:00














                    @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
                    – Valentin Bajrami
                    Dec 21 '18 at 19:26




                    @ShritamaSengupta your nc doesn't support the exec flag the. There is this answer here but it uses named pipes superuser.com/questions/691008/…
                    – Valentin Bajrami
                    Dec 21 '18 at 19:26












                    I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
                    – Shritama Sengupta
                    Dec 21 '18 at 19:29




                    I just installed the traditional-netcat instead and accessed the -exec command. This seemed as the easier way to achieve my task. Thank you for your hint though.
                    – Shritama Sengupta
                    Dec 21 '18 at 19:29


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490371%2fshell-script-in-netcat-listener-to-talk-with-client%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Morgemoulin

                    Scott Moir

                    Souastre