Perl: Net::telnet prompt question

1. Purpose - to have a perl program telnet into a centOS system, issue many commands and capture output for sorting, comparing later. Had this working 10 years ago and haven't done much since so I've forgot a lot... however I have my old code and perl book and want to get back into it.
First step isn't quite working....print output from a command. Telnet log-in works, print doesn't.

$ts = new Net::Telnet (Timeout => 4, Prompt => '/\>/');        #matches for log-in but
$ts->open  ("$boxip");
$ts->login ($username, $passwd);
print "\n                   Sucess! We are in !!!!\n\n\n\n";
@lines = $ts->cmd ("who");                                             # does > not match?
print @lines;
print "\n exiting";
exit;

2. The code above does not print any output for any command issued, yet I can see that the command(s) was issued  by looking into the .bashrc_history file. I'm thinking it may have something to do with the prompt matching exactly and I do not have the knowledge to fiddle with this. (http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm)
For log-in I seem to be able to use " >"  , and this works ...however the prompt may actually be something more. I guess it's really the username/hostname/current directory
 Below is what it looks like after hitting return a couple of times and then issuing the "who" command. Also notice when I issue a "cd" command the path name changes (as part of the prompt??) and some sort of "ls" ? command displays the size and contents of the new directory.
Can I program around this when using $ts->cmd? or do I have to turn off the fancy prompting? I'd rather work with the fancy prompt but if I can't  how do I change it to be something simple that will work in a perl program that needs to capture output from many, many commands in different directories??  I'm not too familiar with changing shell prompts and fiddling with the 4 files that seem to effect it (attached .profile .bashrc /etc/profile /etcbashrc).
I did change the prompt by issuing a PS1=">" and it works until I change a directory and it goes back to the fancy prompt

To Summarize: I would like to use $ts->cmd with the fancy prompt else I'd like a good way to change the prompt for testing and put it back when done.
=============================================================================
Network Sentry
root@qa223:/root
>

Network Sentry
root@qa223:/root
>

Network Sentry
root@qa223:/root
> who
root     tty1         2011-11-14 11:16
root     pts/0        2011-12-29 08:58 (192.168.10.147)

Network Sentry
root@qa223:/root
>
Network Sentry
root@qa223:/root
> cd bkup1227
/root/bkup1227
total 76K
drwxrwxr-x 5 4.0K Dec 23 03:08 templates
-rw-r--r-- 1  67K Dec 28 11:36 filedef.tar

Network Sentry
root@qa223:/root/bkup1227
>
=============================================================================
Network Sentry
root@qa223:/root
> echo $PS1
\n\[\033[1;32m\]Network Sentry \[\033[0m\]\n\[\033[1;31m\]\u@\h:/root\n> \[\033[0m\]

Network Sentry
root@qa223:/etc/sysconfig/network-scripts
> $BASH_VERSION
-bash: 3.2.25(1)-release: command not found

=============================================================================
Output from the code above:
c:\Perl\brad>sanchk1.pl


                   Sucess! We are in !!!!

 exiting
c:\Perl\>

profile-bashrc.zip
LinkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
You would be better off using the Net::SSh::PERL
http://search.cpan.org/~turnstep/Net-SSH-Perl-1.34/lib/Net/SSH/Perl.pm

This provides for a secure communication as best practices is to disable telnet access to a system.

Not sure what you would like to do with the prompt?
Are you using the prompt as a determinant whether the remote system is ready for input?

You could alter the prompt depending on the shell, bash, PS1.
0
LinkAuthor Commented:
I'll try the Net:SSh......and would actually rather us it as I will not have to enable telnet, but as I am in a testing environment behind firewalls there is zero reason to worry about security, it's just not a concern for writing a script to test code in a lab.

I'm not sure what I want to do with the prompt either....what I want to know is why the code doesn't print out the result of issuing the "who" command and I thought it may have to do with the prompt. My guess is I'll have the same issue using Net::Ssh, so I'll try it a little later and post results. Thanks.
0
arnoldCommented:
Not sure, the cmd() should work without regard to the type of prompt.  I think it should handle most standard variations of prompts.

you can try instead of printing the @lines
print "There were $#lines returned\n"
foreach $entry (@lines) {
print "$entry\n";
}

see if there are other control characters that wind their way in running a command, alternatively change the shell on the user you are testing from bash to plain sh and see whether that makes a difference.
send one of the commands "TERM=vt100; export TERM" and see if that makes a difference.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

LinkAuthor Commented:
The methods login() and cmd() use the prompt setting in the object to determine when a login or remote command is complete. Those methods will fail with a time-out if you don't set the prompt correctly. I just hve the feeling that if my prompt was a simple ">" or "#" or similar it would work

Maybe you can try, here is the complete program...it should work according to http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

I'll try this program on my home pc loaded with Suse Linux and report back, Thanks for your comments

####    main routine
##

$| = 1;                                   # force print to flush immediatley

## command structure definitions

### default test parameter
$username       = "root";                                                   # username
$passwd         = "pleasechangeme";                              # password
$boxip         = "192.168.0.5";                                         # shelf controller
$ver            =  "1st draft";

$ts = new Net::Telnet (Timeout => 4, Prompt => '/\>/');        #Alternative Prompt => '/[\$%#>] $/');
$ts->open  ("$boxip");
$ts->login ($username, $passwd);
print "\n                   Sucess! We are in !!!!\n\n\n\n";
@lines = $ts->cmd ("who");                            # show up in bash_history but result do not print
print @lines;
print "\n exiting";
exit;
0
arnoldCommented:
Oh, I see what you mean. Your PS1 includes colors.

http://jamiedubs.com/ps1-collection-customize-your-bash-prompt

by default command prompt (cmd_prompt) is any of the following characters:
/[\$%#>] $/
which means any one of the 4 characters followed by a space

Yours adds color and multiple lines, but your Prompt definition is missing the space
Prompt => '/\> /'
which should work.
you could setup a .bashrc with an initial question
What prompt do you want?> 1 interactive user 2 script
when 1 PS1 will remain defined as is, if 2 PS1='> '

The following discussion has an example that might help.

http://answers.yahoo.com/question/index?qid=20111202005113AAsxMt9
0
LinkAuthor Commented:
Playing around with another system that has a simple prompt....but still can not print out the results
from $ts->cmd....I think I need to close this question and open one which states the problem in a more simple manner as I have way too much info here and before I even get to addressing the prompt, I need to get basic telnet command output to return to my perl program
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LinkAuthor Commented:
Reopen question to simplify and break down into manageable pieces
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.