I have a Perl script which does this: It generates a ssh authentication key on my system and then copies this key to a remote Linux system for passwordless ssh connects. The code is as below:
# Generate an rsa key and store it in the given file
system("ssh-keygen -t rsa -N '' -f /root/.ssh/id_rsa 1>/dev/null");
# Copy the generated key to a remote system whose username
# is stored in variable $uname and IP address is stored in variable $ip
system("ssh-copy-id -i /root/.ssh/id_rsa.pub $uname\@$ip 2>&1 1>/dev/null");
The problem I have is this: The ssh-copy-id
command takes quite some time to copy the key to the remote system. So, when this Perl script is run, it will appear like the script has hung.
Therefore, I want to do display a "progress message": When the copy is in progress I want to display "SSH authentication key copy is progress" and if the copy has failed, display "Failed to copy" and if it has succeeded, display "Copy succeeded".
How do I go about doing this?
One more(based on Chas's answer):
I tried the code as Chas suggested
die "could not fork: $!" unless defined(my $pid = fork);
#child sleeps then exits with a random exit code
unless ($pid) {
print "Connecting to server ";
exec "ssh-copy-id -i /root/.ssh/id_rsa.pub $uname\@$ip 2>&1 1>/dev/null";
exit int rand 255;
}
#parent waits for child to finish
$| = 1;
print "waiting: ";
my @throbber = qw/ . o O o . /;
until ($pid = waitpid(-1, WNOHANG)) {
#get the next frame
my $frame = shift @throbber;
#display it<br />
print $frame;
#put it at the end of the list of frames
push @throbber, $frame;
#wait a quarter second<br />
select undef, undef, undef, .25;<br />
#backspace over the frame<br />
print "\b";<br />
}
The problem is this:
Now ssh-copy-id asks for a Password input while connecting to the remote server. So, the "throbber" output(i.e the circle of varying size that get's displayed) comes after the Password input which looks weird. This is how it looks like:
CURRENT OUTPUT
Connecting to remote server o0O0o #This is the throbber. The output doesn't exactly look like this but I can't print dynamically changing output, can I
Password:o0O0oXXXXXo0O0o #You get it right, the throbber unnecessarily comes at the Password prompt too
THE OUTPUT THAT I WANT:
Connecting to remote server o0O0o #The throbber should be displayed HERE ONLY, NOWHERE ELSE
Password:XXXXX
Any ideas, anyone?