Solved

sftp script quotes

Posted on 2014-10-29
17
143 Views
Last Modified: 2014-10-31
I need to upload a file and them move the file locally to an archive directory.
I seem to be missing " " around the mv statement but am not sure where to place them
Thank you : )

#!/usr/bin/expect

spawn /usr/bin/sftp username@192.168.1.40
#sleep 5

send "mput /directory_name/*.* \n"
mv /directory_name/*.xml /directory_name/archive/ \n

send "bye\n"
0
Comment
Question by:Peter Kuczynski
  • 9
  • 4
  • 3
  • +1
17 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40411140
What was the command intended to do?
Did you want to send it to the sftp process?
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411178
the command is intended to
1. upload a file to a sftp server, this works
then
2. move the local file to a archive folder retaining it's name

I made this change, so now the command runs but it does not move the file to the local archive directory.

expect "sftp>"
send "mput /directory_name/*.* \n"
send "lmove /directory_name/*.xml /directory_name/archive/ \n"
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40411210
Try using "!":
send "mput /directory_name/*.* \n"
!mv /directory_name/*.xml /directory_name/archive/ 

send "bye\n" 

Open in new window

PS: "lmove" is not valid for many *nix implementations.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40411231
If you meant
system "mv /directory_name/*.xml /directory_name/archive/"
you might want to do it after sftp is finished uploading them
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411252
I am now getting >>>> invalid command name "!mv"
                                        while executing

send "mput /directory_name/*.* \n"
!mv /directory_name/*.xml /directory_name/archive/

send "bye\n"

>you might want to do it after sftp is finished uploading them
                         I would like to do all this within one script if possible.
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411289
To recap my entire script plus the mv additions I need to make the files move which still I cannot get to move
I ran the mv commnd by itself and its correct, just not with the script.

#!/usr/bin/expect
spawn /usr/bin/sftp username@123.456.789.123
#sleep 5
expect "sftp>"
send "mput /directory1/*.* \n"
!mv /directory1/*.xml /directory1/archive/
expect "sftp>"
send "mput /directory2/*.* \n"
!mv /directory2/*.xml /directory2/archive/
expect "sftp>"
send "mput /directory3/*.* \n"
!mv /directory3/*.xml /directory3/archive/
expect "sftp>"
send "mput /directory4/*.* \n"
!mv /directory4/*.xml /directory4/archive/
expect "sftp>"
send "mput /directory5/*.* \n"
!mv /directory5/*.xml /directory5/archive/
expect "sftp>"
send "mput /directory6/*.* \n"
!mv /directory6/*.xml /directory6/archive/
send "bye\n"
0
 
LVL 84

Expert Comment

by:ozo
ID: 40411295
Did you try system?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40411326
Mmmm, works for me in AIX. Perhaps its not available in Linux.
Then you may want to follow Peters suggestion to do it after sftp is finished uploading.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411422
This is interesting, I pasted the !mv command at the sftp prompt and it works!
So why would it not work in the script?

I'm doing this on RHEL 6.5
So if all fails, perform the mv commands in a seperate script?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40411450
Maybe you need to send it?:

send "!mv /directory5/*.xml /directory5/archive/\n"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411459
runs with no errors but dosnt move the file
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40411481
Did you try
system "mv /directory_name/*.xml /directory_name/archive/"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411485
OMG it worked!
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411497
finished product.....

#!/usr/bin/expect
spawn /usr/bin/sftp username@123.456.789.123
#sleep 5
expect "sftp>"
send "mput /directory1/*.* \n"
system "!mv /directory1/*.xml /directory1/archive/ \n"
expect "sftp>"
send "mput /directory2/*.* \n"
system "!mv /directory2/*.xml /directory2/archive/ \n"
expect "sftp>"
send "mput /directory3/*.* \n"
system "!mv /directory3/*.xml /directory3/archive/ \n"
expect "sftp>"
send "mput /directory4/*.* \n"
system "!mv /directory4/*.xml /directory4/archive/ \n"
expect "sftp>"
send "mput /directory5/*.* \n"
system "!mv /directory5/*.xml /directory5/archive/ \n"
expect "sftp>"
send "mput /directory6/*.* \n"
system "!mv /directory6/*.xml /directory6/archive/ \n"
send "bye\n"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411581
it works but it does not allow to execute multiple system commands embedded after a send command
meaning, once the first system command runs it does not process the next system command
it does appear to work if you group all the system command at the end.
Another issue is, if there is no file in a directory to move, the script stops.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40412001
sftp is better for interactive use.   scp is much better in a script

#!/bin/bash
for dir in /directory?
do
  scp $dir/*.* username@123.456.789.123:
  mv $dir/*.xml $dir/archive
done

Open in new window

0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40415852
Here is what I finally wound up with, it runs fast.
Now I just need to add successful logging for files that send and ignore directories with no files



#!/bin/sh
sftp username@123.456.789.123
sleep 5
lcd /directory1
mput *.xml
!mv //directory1/*.xml /directory1/archive/

lcd /directory2
mput *.xml
!mv /directory2/*.xml /directory2/archive/

lcd /directory3
mput *.xml
!mv /directory3/*.xml /directory3/archive/

quit
EOF
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now