jbush82 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to redirect all of the output of the script to /dev/tty9. Below is the script, and currently what happens is the script runs in the current terminal and writes 0s to the /dev/tty9 console. I'd like to get the color and everything outputted to that screen. Any help would be appreciated...


Main script:
#!/usr/bin/perl use Net::Ping; + # Module to check a remote host for reachability use Curses; + # Module to allow the use of system's curses $data_file="nms.systems2"; + # File which holds network node data open(DAT, $data_file) || die("COULD NOT OPEN SYSTEMS FILE: $!"); @raw_data=<DAT>; + # Array for holding node data close(DAT); $screen="/dev/tty9"; open(SAVEOUT, ">&STDOUT") || die("COULD NOT DUP STDOUT: $!"); open(STDOUT, ">", $screen) || die("COULD NOT REOPEN STDOUT ON $screen: + $!"); # Redirects standard output to $screen initscr(); + # Show header start_color(); init_pair(1, COLOR_CYAN, COLOR_BLACK); attron(COLOR_PAIR(1)); addstr(1, ($COLS -19) / 2, "Connectivity Status"); $y=0; + # Center line to split sides (comment out if not needed) while ($y != 20) { addstr(3+$y, 40, "|"); $y++; } attroff(COLOR_PAIR(1)); raw(); refresh(); foreach $system (@raw_data) { chop($system); ($ipaddr,$identity,$row,$col)=split(/\|/,$system); $p=Net::Ping->new("icmp"); if ($p->ping($ipaddr,1)) { start_color(); init_pair(2, COLOR_GREEN, COLOR_BLACK); + # If system is up, show output as green attron(COLOR_PAIR(2)); addstr($row, $col, "$identity"); + # Output information to specific location addstr($row+1, $col, " UP"); attroff(COLOR_PAIR(2)); } else { start_color(); init_pair(3, COLOR_RED, COLOR_BLACK); + # If system is down, show output as red attron(COLOR_PAIR(3)); addstr($row, $col, "$identity"); + # Output information to specific location addstr($row+1, $col, " DOWN"); attroff(COLOR_PAIR(3)); } refresh(); } endwin(); close(SAVEOUT); close(STDOUT);

nms.systems2 file:
127.0.0.1|gateway1|3|18 127.0.0.1|gateway2|3|54 127.0.0.1|system01|6|6 127.0.0.1|system02|6|18 127.0.0.1|system03|6|30 127.0.0.1|system11|6|42 127.0.0.1|system12|6|54 127.0.0.1|system13|6|66 127.0.0.1|system04|9|6 127.0.0.1|system05|9|18 127.0.0.1|system06|9|30 127.0.0.1|system14|9|42 127.0.0.1|system15|9|54 127.0.0.1|system16|9|66 127.0.0.1|system07|12|6 127.0.0.1|system08|12|18 127.0.0.1|system09|12|30 127.0.0.1|system17|12|42 127.0.0.1|system18|12|54 127.0.0.1|system19|12|66 127.0.0.1|system10|15|6 127.0.0.1|system20|15|42 127.0.0.1|system21|15|54 127.0.0.1|system22|15|66 127.0.0.1|system23|18|42 127.0.0.1|system24|18|54

Replies are listed 'Best First'.
Re: output script to /dev/tty9
by Fletch (Bishop) on Dec 27, 2006 at 12:29 UTC

    I think your problem is that Curses is going to write to STDOUT, so reopen that descriptor rather than opening a new one. If you need the original one save it beforehand.

    open( SAVEOUT, ">&STDOUT" ) or die "Can't dup STDOUT: $!\n"; open( STDOUT, ">", "/dev/tty9" ) or die "Can't reopen STDOUT on tty9: +$!\n";
      Thanks man, your solution seems to be working!
Re: output script to /dev/tty9
by jettero (Monsignor) on Dec 27, 2006 at 12:31 UTC

    That looks like it really aught to work... What doesn't work? I suspect your Curses functions aren't printing the colors to the TTY you selected?

    If so, have a look at Term::ANSIColor instead. It's probably set up better for what you're trying to do.

    -Paul

Re: output script to /dev/tty9
by Anonymous Monk on Dec 27, 2006 at 12:45 UTC
    select TTY;
Re: output script to /dev/tty9
by jbush82 (Novice) on Dec 27, 2006 at 18:08 UTC
    Okay, I've updated the code in my original post to reflect what I currently have, and now when I run the script I get the output in /dev/tty9, which is what I'm going for. However... I still have two problems.

    1.) When I run the script from crontab, the script does not run. It shows the following output in /var/log/syslog:
    Dec 27 11:59:01 penguin /USR/SBIN/CRON[5333]: (root) CMD (root^I/home/ +jason/Desktop/monitor_script/nms/nms.pl) Dec 27 11:59:01 penguin /USR/SBIN/CRON[5327]: (root) MAIL (mailed 25 b +ytes of output but got status 0x0001 ) Dec 27 11:59:01 penguin /USR/SBIN/CRON[5326]: (root) MAIL (mailed 24 b +ytes of output but got status 0x0001 ) j
    ** I just figured out a solution, I told it to sleep for 30 seconds, then execute itself again. Does anybody see any problems with this?

    2.) Each time the script is ran manually, the screen is cleared with initscr() function from ncurses. Does anybody know how to not have the screen clear? I want it to look as if the screen updates, rather than have it clear and have the systems reappear after they have a successful or failed ping.

    Any help is greatly appreciated. Thanks.