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

Folks,

I am trying to use Net::SSH::Perl to do a non-interactive login to a system and run quite a few commands. The output of those commands would go to individual files. I cant seem to get it to log in noninteractivly. It fails on the ssh->login line below. I dont want to use a password or a passphrase. Any ideas what I am doing wrong? I am quite stumped.. I have ssh (openssh 3.5p1) currently setup so that from the normal command line, I can just type "ssh hostname" and access the system without typing a password or passphrase (I am well aware of the security implications). Its using rsa public keys I believe since I have a id_rsa and id_rsa.pub files in my home directory on the host I am running this from. I am running this as my normal userid not root.

Thanks,
Crayola
#!/usr/bin/perl # use Text::CSV; use Net::SSH::Perl; # Make sure this is the right filename for the command file! $CSVFILE = "collector-commands"; # Make sure this is the right filename for the host file! $HOSTFILE = "collector-hostlist"; # Read the csv file into an MD array my $csv = Text::CSV->new; open (CSVDATA, $CSVFILE) or die "Cant open commandfile\n"; while (<CSVDATA>) { next if /^#/; # skip comments next if /^\s*$/; # skip empty lines if ($csv->parse($_)) { my @field = $csv->fields; push @csv_array, [ @field ]; } } open (HOSTLIST, $HOSTFILE) or die "Cant open hostlist\n"; while ($host = <HOSTLIST>) { chomp $host; my $ssh = Net::SSH::Perl->new($host, debug => 1, protocol => 2); $ssh->login("myusername"); for $i ( 0 .. $#csv_array ) { $row = $csv_array[$i]; my($out, $err) = $ssh->cmd("$row->[0]"); # eventually we will pump all the output to a database # but for now.. it goes to individual files open (OUTFILE,">$host-$row->[1]"); print OUTFILE $out; close (OUTFILE); $out = ""; } }

Replies are listed 'Best First'.
Re: Net::SSH::Perl problems
by zby (Vicar) on May 28, 2003 at 14:21 UTC
    And what is the error message?
      Opps.. sorry forgot the error message. Here is the output of the Net::SSH::Perl debug data when the script runs. Any ideas?
      [mcunning@localhost mcunning]$ ./collector.pl > mike localhost.localdomain: Reading configuration data /home/mcunning/.ssh/config localhost.localdomain: Reading configuration data /etc/ssh_config localhost.localdomain: Connecting to dumbo-temp, port 22. localhost.localdomain: Remote protocol version 1.99, remote software version OpenSSH_3.5p1 localhost.localdomain: Net::SSH::Perl Version 1.23, protocol version 2 +.0. localhost.localdomain: No compat match: OpenSSH_3.5p1. localhost.localdomain: Connection established. localhost.localdomain: Sent key-exchange init (KEXINIT), wait response +. localhost.localdomain: Algorithms, c->s: 3des-cbc hmac-sha1 none localhost.localdomain: Algorithms, s->c: 3des-cbc hmac-sha1 none localhost.localdomain: Entering Diffie-Hellman Group 1 key exchange. localhost.localdomain: Sent DH public key, waiting for reply. localhost.localdomain: Received host key, type 'ssh-dss'. localhost.localdomain: Host 'dumbo-temp' is known and matches the host + key. localhost.localdomain: Computing shared secret key. localhost.localdomain: Verifying server signature. localhost.localdomain: Waiting for NEWKEYS message. localhost.localdomain: Enabling incoming encryption/MAC/compression. localhost.localdomain: Send NEWKEYS, enable outgoing encryption/MAC/compression. localhost.localdomain: Sending request for user-authentication service +. localhost.localdomain: Service accepted: ssh-userauth. localhost.localdomain: Trying empty user-authentication request. localhost.localdomain: Authentication methods that can continue: publickey,password,keyboard-interactive. localhost.localdomain: Next method to try is publickey. localhost.localdomain: Next method to try is password. localhost.localdomain: Trying password authentication. localhost.localdomain: Will not query passphrase in batch mode. localhost.localdomain: Authentication methods that can continue: publickey,password,keyboard-interactive. localhost.localdomain: Next method to try is publickey. localhost.localdomain: Next method to try is password. localhost.localdomain: Trying password authentication. localhost.localdomain: Will not query passphrase in batch mode. localhost.localdomain: Authentication methods that can continue: publickey,password,keyboard-interactive. localhost.localdomain: Next method to try is publickey. localhost.localdomain: Next method to try is password. localhost.localdomain: Trying password authentication. localhost.localdomain: Will not query passphrase in batch mode. localhost.localdomain: Authentication methods that can continue: publickey,password,keyboard-interactive. localhost.localdomain: Next method to try is publickey. localhost.localdomain: Next method to try is password. Permission denied at ./collector.pl line 34
        the documentation says:
        * identity_files ... If you don't provide this, RSA authentication defaults to using $ENV{HOME}/.ssh/identity, and DSA authentication defaults to $ENV{HOME}/.ssh/id_dsa.

        So if your identity is in id_rsa.pub maybe you should let the object know that during instantiation:

        my $ssh = Net::SSH::Perl->new( $host, identity_files => ["$ENV{HOME}/.ssh/id_rsa.pub"], );
        Well it looks like it pays to read the authors ToDO file. for Net::ssh::Perl. It only supports dsa public key authentication not rsa. As soon as I setup a dsa key it worked great. Thanks all for your help. Crayola
Re: Net::SSH::Perl problems
by KPeter0314 (Deacon) on May 28, 2003 at 14:52 UTC
    Looks like your client is knocking at the door but getting refused.

    What kind of access to the remote system do you have?

    If you have root access, you might be able to run 'sshd -vV' and then retry your program. You will see all the messages from the server end. I've found those error messages tend to say more than the client when troubleshooting.

    Note: You will have to kill the currently running sshd to start a new sshd.

    -Kurt

      I do have root access.. I will look into increaseing the logging level of the ssh server. I know a normal ssh and non interactive ssh works fine. Everything is using Openssh v 3.5p1. Thanks, Crayola