Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

how to send input to stdin automatically?

by skyworld_chen (Acolyte)
on Dec 18, 2012 at 06:35 UTC ( #1009286=perlquestion: print w/ replies, xml ) Need Help??
skyworld_chen has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Is there a way to send the input to stdin directly? e.g when I use "cvs update", I always need to answer the password with ssh. If ignore security problem, is there a way I can write a script to run "cvs update" and answer password automatically? thanks.

Comment on how to send input to stdin automatically?
Re: how to send input to stdin automatically?
by tobyink (Abbot) on Dec 18, 2012 at 07:56 UTC

    Many programs that read passwords do not read them from STDIN - they read keystrokes from the terminal. If CVS is one of those, then sending data to its STDIN will not help you.

    But yes, you can run a program from Perl and pipe data to its STDIN...

    open my $pipe, '|-', 'some-program arg1 arg2'; print {$pipe} "some data\n"; close $pipe;

    If you also need to read from that program's STDOUT or STDERR, check out IPC::Open2, IPC::Open3 or System::Command.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Hi tobyink,

      thanks for your kind reply. You are right, I do need to send password to keyboard, not STDIN. I have tried code you suggested, but the linux still asks for the password. My code is as this:

      #!/usr/bin/perl open my $pipe,'|-', 'cvs update'; print {$pipe} "my_password\n"; close $pipe;

      did I miss something? thanks.

      ssh (as most programs asking for a password) does not read the password from stdin but from the process tty, so not opening a pipe neither using any of the modules you have named will work.

      The usual solution to automate password authentication is to use Expect.

Re: how to send input to stdin automatically?
by graff (Chancellor) on Dec 18, 2012 at 08:43 UTC
    I think you'll need to look at Expect (or one of its variants).
Re: how to send input to stdin automatically?
by Utilitarian (Vicar) on Dec 18, 2012 at 09:04 UTC
    As a general rule you should enable ssh key exchange on your cvs repository and then the question becomes moot.
    From your development machine:
    ssh-keygen -t dsa (accept the default for all queries) $ cat ~/.ssh/id_dsa.pub | ssh ${repository_host} "mkdir -p .ssh ; cat +>> .ssh/authorized_keys" (enter your password) $ ssh ${repository_host}echo (answer "yes") $ ssh ${repository_host} echo (Nothing should happen, you should just get your prompt back)
    You need to set the following environment variables (put them in your .profile or .bashrc) before you can run any cvs command:
    $ export CVS_RSH=ssh $ export CVSROOT=${username}@${repositry_host}:/path/to/cvsroot/
    Hope that helps

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      Hi all,

      I'm a new comer for Perl, both SSH and Expect seems difficult to me. I need some time to understand them. Thanks for all of your kind replies.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009286]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-12-25 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls