Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to send input to stdin automatically?

by skyworld_chen (Acolyte)
on Dec 18, 2012 at 06:35 UTC ( [id://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?

Replies are listed 'Best First'.
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.

Re: how to send input to stdin automatically?
by tobyink (Canon) 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'
      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.

      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.

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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found