'Perl: How can I use Telnet to run two different programs simultaneously on a remote Linux machine

I want to write a Perl script to execute two different programs (ptp4l, phc2sys) simultaneously on another thread of the same kernel that will continuously print its output (similar to ping) until I kill it with ctrl+c, through Net::Telnet on a remote Linux machine.

Possible solutions I found:

a) Using & behind the two programs/commands, example:

$tel->print("ptp4l ... & phc2sys ... &");

$tel->waitfor(Match => ...); # Two programs produce different output and I don't know how to process them separately.

b) Using threads, but I am not sure how to do this for two different processes with different output. So far, the examples I have seen are multiple threads created to execute the same command/process.

c) Using GNU parallel, but this is not available to me and I am not allowed to install it.

If I were to run the two programs sequentially, it would look like this:

$tel->print("ptp4l ...");
$tel->waitfor(Match => ...); # wait for the keyword that shows the program is executed successfully.

$tel->print("phc2sys ... "); # this will produce wrong results as this program need to run with ptp4l simultaneously on another thread of the same kernel.
$tel->waitfor(Match => ...);

How can I achieve my objective above? What is the right or best way to do this?



Solution 1:[1]

Does it have to be telnet, or just be able to connect to other machines. Although you may be in a restrictive environment, others who look at these answers may not be.

If the problem is the duplicity

I don't know what's driving your problem, but I think I'd be inclined to simple fork once for each thing I want to do. Although Perl has this thing called "threads", not many people use it. Perl is more inline with the classic Unix way of doing things.

If you don't need to share information or make decisions based on the outputs (and you haven't said that you do), then do the dumb thing of forking.

Telnet is kinda old

My latest machines don't even have telnet. However, I don't know your particular setup or constraints, but other people may benefit from this.

You can execute commands through ssh. This is just dumb command execution:

% ssh some_host command

That command could even be another ssh. I once worked on a project that had a chain of five such jumps because the access to machines was limited to particular hosts (so you had to get to the right host to get to the one you wanted):

% ssh some host 'ssh other_host command'

If you want to do something not so dumb, Expect is a great tool to interact command-line interfaces. It knows how to read the output, "expect" certain patterns, and based on that, send input. For example, this connects to a machine, expects to see the prompt with my name in it, and when it does, send the ls as if I had typed it myself (the interact is there just to give the program time to show you the output instead of stopping immediately):

%  expect -c 'spawn ssh m1; expect "brian" { send "ls\r" }; interact'

Although Expect is a Tcl tool, there's also Perl's Expect module.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brian d foy