1

Here's my predicament. I'm developing a perl telnet script that uses Expect to spawn a new telnet process and feed data (non-interactively) into it.

Everything works fine when I call the script from a terminal - however, if I start the script from cron I am unable to login to the remote device because the Window size negociation fails - as my client sends a window size of 0x0 (instead of 80x24).

Apparently telnet gets this window size from its master PTY - which isn't a TTY if it's called from cron. Most likely telnet is getting these settings using ioctl or some similar mechanism, because trying to override these settings by environment variables failed (ROWS=200 COLUMNS=80 telnet test).

I ran stty from cron and redirected the output to a file. Problem is stty complains when it's run from cron: /bin/stty: standard input: Invalid argument

Do you know any way to:
1) override the number of rows/columns telnet sends to the remote device
2) start a tty and start telnet inside that tty (from cron)

Thanks

Mad_Ady
  • 475
  • 1
  • 6
  • 14
  • update - I managed to solve my problem by using a workaround. The expect module has a function which copies the size of a TTY to the current "TTY" in which telnet runs. Problem was the default TTY was STDIN - which has no dimensions when run from cron. I forced it to get its dimensions from /dev/tty0 instead:
    #we're running from cron or smth
    $logger->debug("setting /dev/tty0 window size to 80x24:"); `/bin/stty -F /dev/tty0 columns 80 rows 24`;
    open TTY0, "/dev/tty0" or die "Can't open /dev/tty0: $!";
    $session->slave->clone_winsize_from(\*TTY0);
    – Mad_Ady Aug 12 '09 at 08:40
  • You've accepted an answer after 7.5 years! With it, you a record holder of this site! – peterh Mar 24 '17 at 16:38
  • 1
    :) Yeah. I stumbled on my question 7 years later and I had to agree with myself in the end. – Mad_Ady Mar 25 '17 at 19:02

3 Answers3

1

I found this post very useful as I ran into the same problem after migrating some Perl code away from using Net::Telnet to Expect (Expect has extremely useful debugging output in comparison).

However in my situation I had a /dev/tty0 that was pretty locked down (only root could read it) and I didn't want to change that:

crw--w----    1 root     tty        4,   0 Aug 30  2002 /dev/tty0

I looked at the Expect/IO::Pty/IO::Tty source and ended up hacking in the following - which essentially does the same thing (80 x 24) without needing to read from /dev/tty0.

use IO::Tty;
use IO::Tty::Constant;
my $winsize = pack('SSSS', 24, 80, 0, 0);
ioctl($exp->slave(), &IO::Tty::Constant::TIOCSWINSZ, $winsize);
1

update - I managed to solve my problem by using a workaround. The expect module has a function which copies the size of a TTY to the current "TTY" in which telnet runs. Problem was the default TTY was STDIN - which has no dimensions when run from cron. I forced it to get its dimensions from /dev/tty0 instead:

#we're running from cron or smth
$logger->debug("setting /dev/tty0 window size to 80x24:");
`/bin/stty -F /dev/tty0 columns 80 rows 24`;
open TTY0, "/dev/tty0" or die "Can't open /dev/tty0: $!";
$session->slave->clone_winsize_from(*TTY0);

Mad_Ady
  • 475
  • 1
  • 6
  • 14
0

In the start of the script:

set columns 80
set rows 24
pyhimys
  • 1,287
  • 10
  • 10