Basically, you need to know when the telnet/SSH session is prompting for input.
How to get around that problem?
Net::Telnet
Make the prompt something you know, see here:
Re: Net::Telent - How to get the prompt displayed after login using telnet?
Then send the input, see here:
Interactive input via telnet
With Net::Telnet, it's all a matter of using
cmd(),
waitfor() and
print()
For SSH, you might try taking a look at
Net::SSH::Expect
I haven't searched here but I bet there's plenty of examples to be found.