0

I want to run bash script and python script from PERL server-client script. Connection works and if send command via client gets right answer, for example "DATE" gets me current date in client terminal. I want to run bash script in client terminal window taping "SCRIPT" but now it's run on server terminal window. Can I fix it and run script in client terminal window? Maybe solution is simple but now I have no idea. Thank you in advance for help.

My piece of code how I try to run my script


   foreach ($buf) {
            /^SCRIPT$/ and
                    print ($sock system("/bin/bash /path/to/my/bash_script")),  
                   last;
       /^DATE$/  and
                   print($sock
                         scalar(localtime),
                         "\n"),
                   last;
       print $sock "NO OPTION\n";
   }

EDIT:

In reference to the comment i change this line:

print ($sock system("/bin/bash /path/to/my/bash_script"))

to this:

print ($sock `/bin/bash /path/to/my/bash_script`)

This works perfectly for python script. For bash script that is interactive this not works correct. This is bash simple game using tput to color game area.

Interactivity - controls in game:

    local key=$(cat -v)
    local dy=0
    local dr=0

    if [ "$key" == "w" ]; then
        dy=$((dy-2))
    fi

    if [ "$key" == "s" ]; then
        dy=$((dy+2))
    fi


    if [ "$key" == "^[[A" ]; then
        dr=$((dr-2))
    fi

    if [ "$key" == "^[[B" ]; then
        dr=$((dr+2))
    fi

And main loop is something like that:

while [key "e" for exit not down] then
    interactivity()
    draw_game_area()
    increase_iteration++
    sleep 0.1
done;
4
  • How do you connect to the server? Is it SSH protocol? What OS is on client and server side? I have read you question a few times and still have difficulty to understand what you try to do. You can connect to remote server for example over SSH protocol and issue the commands. Or you can write a perl script which runs locally but allows to establish remote connection and then you can issue command to the script server and you communicate with server (commands executed remotely), issue *local and now all commands will run locally. Commented Dec 6, 2019 at 10:45
  • Server and client is on linux localhost. It is PERL script with IO::Socket::INET sockets and runing on the same machine on localhost. I run server.pl next run client.pl on client terminal window taping "DATE" get answer from server in client terminal window (current date), but when I taping "SCRIPT" script run on server terminal window ( I want to run this bash script on client terminal window ) some like tellnet, conversation with machine. So i don't know it is possible on localhost. Commented Dec 6, 2019 at 10:55
  • Lets clarify -- you have two perl scripts one named server.pl and other client.pl run side by side on same computer, and you want them communicate with each other over INET socket (similar to chat programs) and which would allow run programs on remote side (in this case remote is same computer but in case of two computers, server and client would be on different machines). Well, do not forget about some security aspect of this communication (not all commands should be allowed to run remotely). Commented Dec 6, 2019 at 11:07
  • In this case you need look into 'capturing' remote 'STDOUT' and redirect it to 'other' side of the communication. Yes, it can be done but some extra code is involved. I can not read your expertise level but please allow me to refer to following chapter -- docstore.mik.ua/orelly/perl4/prog/ch16_05.htm Commented Dec 6, 2019 at 11:15

2 Answers 2

1
print ($sock system("/bin/bash /path/to/my/bash_script"))

sends the return of system() to the client, but that isn't the output of the script. To capture the output of the script and return it to the client, use backticks instead:

print($sock `/bin/bash /path/to/my/bash_script`), last;
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, thanks a lot it's exactly what I mean. But bash script(this is simple pong game using tput and script is interactive) load very very slow and after starting have a huge lags. Python script works perfectly. Where could be problemm with this? Is the interactivity the cause of this lags?
ah, if it is interactive, you have to do a whole lot more than just capture the output and return it to the client when the script is complete. can you come up with a simple bash script that demonstrates what you want to do and add it to the question (or maybe at this point open a new question)?
I understand I have to after game start redirect my keyboard input (turn off sending lines to server) and after game over back sending commands to server, but for now I can't locate problem. If it is much more complicated to achive this I will stay running script on the server.
0

There are no standard solution for there. Client must support running system commands remotely functionality -- i.e. become a command-server yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.