Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

SSH leaving terminal confused issue

by wulvrine (Friar)
on Apr 24, 2007 at 13:58 UTC ( [id://611740]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monks,
I have a couple of questions in the following code. Basically the code tries to figure out if the user is authorized for SSH without password. So, it forks off a simple command and waits 2 seconds. If it responds within 2 seconds the user is authorized. Otherwise it prints that the user is no longer authorized.
The problem I am having here is after completion of an unauthorized user (where the password prompt is displayed), the terminal no longer works right (pressing enter just redisplays prompt on same line, typing doesnt show up in window etc.) until a 'reset' command is given. What am I missing to either cause the terminal to exit properly, or to reset it (without losing information on the screen) when finished?
A second, less vital question, is how do I keep any text being displayed from the SSH'd term. I have tried saving off STDOUT/STDERR, rdirecting them, and putting them back at the end. This doesn't keep the password prompt from appearing, and doesn't help with messing up the terminal, as I was hoping for 2 birds with one stone.
#! /usr/bin/perl # Libraries use Carp; use strict; use warnings; my $user="MyUserName"; my $host="MyHost.MyDomain.MyCom"; my $pid = 0; #check to see if we are already authorized on system #try to ssh in without password, if finished within #2 seconds, we are authorized, otherwise we are sitting #at password prompt and need to do something eval { local $SIG{'ALRM'} = sub { die "alarm\n" }; $pid = fork; if ($pid) { # Parent alarm 2; waitpid $pid, 0; alarm 0; } else { # Child $SIG{'CHLD'} = 'IGNORE'; #simple command, ignore output just see if runs exec("ssh -f -n -l $user $host uptime"); } }; if ($@) { if ($@ ne "alarm\n") { croak "Unexpected error connecting to '$host'\n"; } kill 9, $pid; print "\nTFD>> <0> Cannot login to '$host'\n"; exit 0; } print "TFD>> <1> Successful login to '$host'\n"; print "done\n"; exit 1;

Note::Why am I not using Net::SSH::Perl? Because Net::SSH::Perl Doesn't seem to work properly on many of my test systems (For example, it stalls for minutes per command on my Fedora Core 6 based machines. This is unusable for something done every 5 seconds.) Creating authorized_keys would allow my test programs to SSH without password, which allows them to perform SSH commands without using faulty Net::SSH::Perl module

Thanks!

s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Replies are listed 'Best First'.
Re: SSH leaving terminal confused issue
by sgifford (Prior) on Apr 24, 2007 at 14:34 UTC
    ssh is probably opening /dev/tty, which is why adjusting filehandles beforehand doesn't help. One standard solution to the "program is opening terminal" problem is to use Expect, which will create a new terminal for the program to read from. This is a good general solution to the problem.

    Another possibility is to use OpenSSH's BatchMode option. The docs say:

    If set to “yes”, passphrase/password querying will be disabled. This option is useful in scripts and other batch jobs where no user is present to supply the password. The argument must be “yes” or “no”. The default is “no”.
    which sounds like what you want. To use it from the command line, you would say something like:
    ssh -o BatchMode=yes ...
Re: SSH leaving terminal confused issue
by Moron (Curate) on Apr 24, 2007 at 14:32 UTC
    ( -f "~$user/.ssh/identity" ) or die "access denied\n";
    __________________________________________________________________________________

    ^M Free your mind!

Re: SSH leaving terminal confused issue
by wulvrine (Friar) on Apr 24, 2007 at 15:51 UTC
    Moron Thanks for the thought! I think that presumes I am already on the remote system to check for that file. No?

    sgifford GREAT! That worked like a charm and dropped the need for me to even have to deal with the forking situation. Thank you VERY much !!

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|
      OIC
      use File::Remote; { my $secure = new File::Remote (rsh => "/usr/local/bin/ssh", rcp => "/usr/local/bin/scp" ); $secure -> open( \*SSHID, "$host:~$user/.ssh/identity") ? ( $secure -> close( \*SSHID ) || die($!) ) : die "access denied\n"; }
      updated for clarity and to heed a warning about filehandle syntax of open in File::Remote
      __________________________________________________________________________________

      ^M Free your mind!

        Why did you change without notice? It changed just after I upvoted on it. For a very slight moment, I though that my voting somehome changed it.

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: SSH leaving terminal confused issue
by Bro. Doug (Monk) on Apr 25, 2007 at 03:48 UTC
    In regards to your second question (How To Keep Text From Being Displayed), I like to make system calls to stty. It's quick and dirty.
    #turn off the terminal's echo `stty -echo` ;

    If you do this sort of thing, don't forget to turn it back on. Just to be extra safe, consider doing this, too:
    # turn echo back on, an extra check before we exit END { `stty echo` }

    It's kind of a mess if the program exits and echo is still off.

    Hope it helps. Peace monks,
    Bro. Doug :wq
Re: SSH leaving terminal confused issue
by cengineer (Pilgrim) on Apr 25, 2007 at 15:41 UTC
    Try installing Math::BigInt::GMP . This is supposed to help with the connection stalling if you decide to use Net::SSH::Perl
Re: SSH leaving terminal confused issue
by wulvrine (Friar) on May 14, 2007 at 15:02 UTC
    First all, THANKS so much for helping.
    My apologies for being so late on response, hit very busy time in life and I wasn't able to log on for a while.
    Moron Will do, and will try that, mucho appreciated.
    Bro. Doug Thanks for the idea! I think that may just do it.
    cengineer I could not find Math::BigInt::GMP using yumm for the red hat enterprise linux updater. There was a Math:GMP or something already installed though.
    Thanks again!

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://611740]
Approved by Joost
Front-paged by liverpole
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-24 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found