scp +find

I'm trying to backup some log files (older than 15 days) from a remote server using scp.
I usued:
scp me@remote_machine:`find folder/*.log -mtime +15` .
but don't works!

Someone can help me?
Thanks.
:confused:

Are you expecting that find to be executed on the machine you're connecting to? It won't be. To use shell commands on a remote host, use ssh.

Thanks Corona688...
can you give me the right commands?

ssh me@remote_machine:"find folder/*.log -mtime +15" > /tmp/remote_list
while read filename; do
   echo "get $filename" >> /tmp/batch
done < /tmp/remote_list
echo "bye" >> /tmp/batch
sftp -b /tmp/batch me@remote_machine

Not tested at all! Please test before using in production (if any).

just for future reference, use scp to copy files to and from remote areas.

I agree, but from the OP's first post, it looks like there might be multiple files, and scp might have to be run several times. Since each time scp is run it will have to setup the connection and then release it, an sftp might be better in this case.

really? i could have sworn you could scp with '*'. yes in fact i recall now, you can scp multiple files...something like this if i recall correctly...

scp root@testbox:/var/tmp/mydir/* .

this would connect once and ask for password and then its copy away....i really should test this before i post, but i am not at work right now.

But to put some logic in there for the older than 15 days, cant be done with scp as far as i know.

-Sowser

You are right. You can scp with *, but the OP wanted a certain bunch of files only, the ones that came out as the result of his find command. Hence the sftp batch script.

yup, i agree with ya, i am just posting for informational purposes.

-S

Thanks for your posts!!!
...but the problem is that I can't use for my project sftp... :frowning: (I can't install the sftp server on the management log server).
I give you more details about my project:
I must realize script with this tool:

  • Scripting language: PERL
  • Remote command execution: EXPECT
  • Remote shell: SSH
  • File transfer: SCP

The log archiving should do the following tasks:
-Connect to the management log server from a sun solaris server and order a log switch every day at midnight.
...

  • Log files older than 15 days must de retrieved from the firewall management server using scp, then the log will be archieved automatically.
    ...

Thanks sincerely for your prompt replays. Have a good day.
M.

If you can do 'scp', then you can do 'sftp'. Try it.

another option ...

create a script that does what you need on the remote server --- replace the logfile, finds the files based on your criteria and makes a list of it in a predetermined directory ... have a cron job to run that script on the remote server a certain time (i.e., 12 midnight) ...

after a predetermined time, run the command below on the local server to get the files --- substitute the correct values for the variables ... you might want to test and play with the escapes as i can't test right now ...

ssh $remserver "cd /$remdir; tar cvfp - \`< /$remfilelist\`" | (cd /$localdir; tar xvfp -)

good luck!

If I execute:
$ sftp fwtest

I obtain:

Connecting to fwtest...
Request for subsystem 'sftp' failed on channel 0
Connection closed

I attach the configuration of sshd_conf:

   $OpenBSD: sshd_config,v 1.59 2002/09/25 11:17:16 markus Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.

#Port 22
Protocol 2
#ListenAddress 0.0.0.0
#ListenAddress ::

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 3600
#ServerKeyBits 768

# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
LogLevel INFO

# Authentication:

#LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys

# rhosts authentication should not be used
#RhostsAuthentication no
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
IgnoreUserKnownHosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

#AFSTokenPassing no

# Kerberos TGT Passing only works with the AFS kaserver
#KerberosTgtPassing no

# Set this to 'yes' to enable PAM keyboard-interactive authentication
# Warning: enabling this may bypass the setting of 'PasswordAuthentication'
#PAMAuthenticationViaKbdInt no

X11Forwarding no
AllowTcpForwarding no
#X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
PrintMotd yes
#PrintLastLog yes
KeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression yes

#MaxStartups 10
# no default banner path
#Banner /some/path
#VerifyReverseMapping no

# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server

DenyUsers root shutdown halt nobody ntp pcap rpm
AllowGroups root

In /usr/libexec/openssh/ I haven't the sftp-server file... :frowning:

the tar command thru ssh will do the file transfer securely as it will on scp so why can't you try that? there are more than a few ways to do things in unix and as long as what you're doing gets the job done and falls within the requirements --- there is no rule that says you have to do things a certain way ...

unless i'm missing something, you're required to use scp to do the file transfer because it happens thru an encrypted channel and it doesn't need a password ... the ssh-tar option will do the same thing --- as will ssh-cpio and ssh-ufsdump, etc. ...

anyways, good luck!

I found the solution using some perl modules. I attach the script:

#!/usr/local/bin/perl -w
use strict;
use Expect;
use File::Remote;

####################################################
# Could put a loop here with different host names so you can ssh to multiple servers...
####################################################
my $hostname = "fwtest";
my $user = "admin";

my $ssh = Expect->spawn("ssh -l $user $hostname"); #or return "Couldn't spawn ssh connection, ".$ssh->exp_error()."\n";

unless ($ssh->expect(30,-re,'#')) {
return "Never got the prompt on $hostname during login,
".$ssh->exp_error()."\n";
}
$ssh->clear_accum();

####################################################
# Verify version of firewall checkpoint (remote test command)
####################################################

my $cmd = "fw ver";
print $ssh "$cmd\r";

####################################################
# Enter in expert mode
####################################################
#print "\n", $sep, "Enter in expert mode.\n", $sep;
$cmd = "expert";
print $ssh "$cmd\r";
unless ($ssh->expect(30,-re,'#')) {
return "Never got the prompt on $hostname during login,
".$ssh->exp_error()."\n";
}
$ssh->clear_accum();
my $psw_exp = "didata123";
print $ssh "$psw_exp\r";

####################################################
# Change directory (where there are the log files)
####################################################

$cmd = "cd /opt/CPsuite-R60/fw1/log";
print $ssh "$cmd\r";
####################################################
# Find the right files (older than 15 days)
####################################################

$cmd = "cp `find *.log -mtime +15` log_backup";
unless ($ssh->expect(30,-re,'#')) {
return "Never got the prompt on $hostname during login,
".$ssh->exp_error()."\n";
}
$ssh->clear_accum();
print $ssh "$cmd\r";

# Now we look for a prompt, having (we hope) successfully logged in.
unless ($ssh->expect(30,-re,'#')) {
return "Never got ssh prompt after sending command $cmd
".$ssh->exp_error()."\n";
}
####################################################
# Use secure connection methods to retrive logswitch files
####################################################

my $secure = new File::Remote (rsh => "/usr/local/bin/ssh",
rcp => "/usr/local/bin/scp");
$secure->copy("fwtest:/opt/CPsuite-R60/fw1/log/log_backup/*.log", "/export/home/x2693/project/test/log_backup");

print "\n";

P.S. It's only the first version... It could be improved!
:slight_smile: :slight_smile: :slight_smile:

Thanks a lot to Blowtorch, Sowser, Corona688, Just Ice for their helps.
See you!!!!!!!!!!!!!!