Search code examples
linuxperltcltelnetperl-module

how to run command on telnet command prompt in perl script


i am executing telnet command in perl script as below.

$telnetOutput = `telnet localhost 4505`;
print "\n telnet command output: $telnetOutput \n";

$clients = `clients`;
print"\n $clients\n";

$clientNumber_or_anyOtherKey = `1`;
print "\n $clientNumber_or_anyOtherKey \n";

$pollers = `pollers`;
print "\n $pollers\n";`

but after running $telnetOutput = `telnet localhost 4505`; command, as we know it will open telnet command prompt but all other commands are still executing in same old command prmopt so it's saying clients or 1 or pollers are not recognized as internal or external commands.

can any 1 help me out pls? thanks in advanch


Solution

  • Communicating with external processes like telnet is more complicated than you might imagine, as you have to correctly handle buffering, waiting for input, and so on.

    The canonical way to approach this is to use Expect ( https://metacpan.org/module/RGIERSIG/Expect-1.21/Expect.pod ) if you really need full interaction.

    If you don't actually need interaction then a remote command runner like ssh or rsh (which you can call from perl of course) is sufficient.