I needed a script to copy a file out to a dmz (using scp), so I wrote this snippet w/ expect. It took a bit of monkeying with it to get it right (especially the part when it gets a "prompt" back from the system), but it works well. (it is functional, just not pretty). "I am more of a butcher than a hacker" Joe
sub copyover { $scp=Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath} +/$file"); $scp->expect(30,"ssword: ") || die "Never got password prompt from + $dest:$!\n"; print $scp 'password' . "\n"; $scp->expect(30,"-re",'$\s') || die "Never got prompt from parent +system:$!\n"; $scp->soft_close(); return; }

Replies are listed 'Best First'.
Re: expect w/scp
by tadman (Prior) on Mar 23, 2001 at 06:14 UTC
    It's an interesting use of Expect, but ssh can be configured to log in without the password prompt, which may be more appealing from a security standpoint because you get key-level authentication instead of a simple password.

    Of course, OpenSSH and SSH act differently with regards to configuration. Look in the man page for references to "authorized_keys" for more info.

    This is a little off-topic, but here is how SSH-SSH (for lack of a better term) operates.

    With SSH2, on server A, you would have a file like:
    ~/.ssh2/identification ---------------------- IdKey id_dsa_1024_a
    On server B, where you want to connect with no password:
    ~/.ssh2/authorization ---------------------- Key id_dsa_1024_a.pub
    You would copy the "id_dsa_1024_a.pub" key from server A over to server B and put it in ~/.ssh2/

    ObPerl:
    Just for kicks, here's a Quick Hack that I put together to schlep keys around from one box to several others.
    #!/usr/bin/perl # # ssh-addauth - Adds automatic login to a remote SSH server # from the current machine. use strict; use Sys::Hostname; my ($default_key) = "id_dsa_1024_a.pub"; # These parameters may come from: /etc/ssh2/ssh2_config my ($ssh_dir) = ".ssh2"; # UserConfigDirectory my ($ssh_authfile) = "authorization"; # AuthorizationFile my ($hostname) = hostname(); my ($username) = getpwuid($<); foreach my $arg (@ARGV) { AddAuth($arg); } sub AddAuth { my ($remote_host) = @_; system ("scp $ENV{HOME}/$ssh_dir/$default_key $remote_host:$ss +h_dir/${username}_${hostname}_dsa_1024.pub"); system ("ssh $remote_host \"echo 'Key ${username}_${hostname}_ +dsa_1024.pub' >> $ssh_dir/$ssh_authfile\""); }
Re (tilly) 1: expect w/scp
by tilly (Archbishop) on Mar 23, 2001 at 07:49 UTC
    I would like to second tadman's recommendation of configuring ssh so you can lose the passwords in scripts. In addition I would recommend using rsync over scp. With ssh you can handle a lot of potential failure conditions by just saying, "I will syncronize ever X minutes anyways, so it doesn't mastter too much if I bomb out whenever there is a problem..."

    That facility can be very useful.