LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-02-2016, 07:08 AM   #1
hruday
Member
 
Registered: Jun 2015
Posts: 72

Rep: Reputation: Disabled
scp files from one server to another using expect


I am using bash shell and have written below script to copy files using scp.

#!/usr/bin/expect
days=15
src=~/exp
files=($(find $src/*.log -mtime +"days"))"

for file in ${files[*]}
do

set pass "passwd208"
spawn scp $file root@ipaddress:/home
expect
{
password: {send "$pass\r"; exp_continue}
}

done

i get following error
invalid command name "days=15"
while executing
"days=15"
(file "scp.exp" line 2)


can anybody plz suggest changes to make it work?

Last edited by hruday; 01-02-2016 at 07:09 AM.
 
Old 01-02-2016, 08:40 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
The first line of a bash script needs to be

Code:
#! /bin/bash
 
Old 01-02-2016, 09:31 AM   #3
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,651

Rep: Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569
Why scp?

Why not use something that is scriptable and natively multithreads like lftp?

Not that scp is not nice, but what you are doing might be better served with a different tool.
 
Old 01-02-2016, 09:31 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,932

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
expect is an extension to tcl and so your mixing bash and tcl syntax in your script. One fix would be to use keys if possible which would eliminate the need to use expect.
 
Old 01-02-2016, 07:18 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
It would also be better to just use RSA certificate authentication to remotely login...
 
Old 01-02-2016, 08:03 PM   #6
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,651

Rep: Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569
Need a better picture.

Also, if that find command is anything like the ones I know, the syntax is incorrect.

I think that what you REALLY want to do could be done easily with a bash script and ssh tools without expect. My understanding of what you WANT to do may not be accurate. Could you please give us a high-level view of what you are trying to accomplish?
 
Old 01-03-2016, 08:29 PM   #7
hruday
Member
 
Registered: Jun 2015
Posts: 72

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
Also, if that find command is anything like the ones I know, the syntax is incorrect.

I think that what you REALLY want to do could be done easily with a bash script and ssh tools without expect. My understanding of what you WANT to do may not be accurate. Could you please give us a high-level view of what you are trying to accomplish?
I don't want to use SSH key sharing method. So.I am using expect . is there any other way to to send files to another without prompting for password
 
Old 01-04-2016, 01:41 AM   #8
hruday
Member
 
Registered: Jun 2015
Posts: 72

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
The first line of a bash script needs to be

Code:
#! /bin/bash
if i use #!/bin/bash

i get following error

scp.sh: line 11: spawn: command not found
expect1.1>
 
Old 01-04-2016, 06:32 AM   #9
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,651

Rep: Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569
use the right tools

There are several ways you could do this using a bash shell:
1. use scp started with sshpass to provide the password (check your repos for sshpass).
2. use a different utility that allows password specification. Pscp/psftp from the putty-tools and lftp are well know to me, there may be others.

You will, I presume, be testing and then background the transfers.
 
Old 01-04-2016, 03:40 PM   #10
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Quote:
Originally Posted by hruday View Post
if i use #!/bin/bash

i get following error

scp.sh: line 11: spawn: command not found
expect1.1>
Your file is mixing bash and expect commands.
 
Old 01-04-2016, 04:04 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I have a script that does this - it's for a specific application setting up embedded systems with a known default password and no need for keys. I assume you know that putting a password in plain-text in a script is BAD 99% of the time, the case for which this script was written is the exception.

Code:
#!/usr/bin/expect

set timeout -1

if { $argc == 1 } {
   set ip [lindex $argv 0]
} else {
   puts "Usage: $argv0 ip"
   exit 1
}

# Send scripts
spawn scp -p -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /path/to/files user@$ip:/path/to/destination/
expect *assword:
send "mypassword\r"
expect eof

exit 0
Of course you'd need to modify "/path/to/files", "user", "/path/to/destination/", and "mypassword" to suit your application.
 
Old 01-06-2016, 01:46 AM   #12
hruday
Member
 
Registered: Jun 2015
Posts: 72

Original Poster
Rep: Reputation: Disabled
I am tired of using expect. so I used ftp login to copy files.

#!/bin/sh

DUMPS=/root/exp/
TMP=/root/tmp

find $DUMPS/*.sql.zip -type f -mtime +15 -exec cp {} $TMP \;

cd $TMP

# FTP Login

HOST=ipaddr
USER=usrname
PASS=passwrd123
REM_DIR=/home/bkpdir

for f in *;
do
echo "Uploading file via FTP:" $f

ftp -in $HOST <<EOF

quote USER $USER
quote PASS $PASS

cd $REM_DIR
put $f
bye
EOF

done

rm -rf $TMP/*


It works fine but the problem i have to run this daily to copy file which is 15 days old in dumps dir.
there are a lot of files which are 15 days old. the second when the script runs it'll copy again duplicate files.


how should i avoid files which are already copied?
 
Old 01-06-2016, 03:00 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you can try rsync instead of scp and also you may try privat/public keys to avoid password authentication
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scp works perfectly, Net::SCP::Expect calls timing out - reasons? EnderX Programming 1 04-27-2013 02:35 AM
[SOLVED] SCP with wildcard in Expect s7upify Linux - General 5 07-18-2011 01:12 PM
Expect script, scp and find command unihiekka Programming 1 10-17-2008 04:11 PM
SCP files from one server to another rev1976 Linux - Newbie 4 09-18-2008 09:21 PM
How to use EXPECT command to scp or ssh? thefountainhead100 Linux - Software 6 03-28-2008 07:41 PM


All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration