I want to access properties of a file named AH_store_20120117_00432.csv at ftp server with url ftp.mysite.com. Properties such as last modified date/time,size, etc.
I am accessing ftp server from batch file as follows:
#echo off
>ftp2.txt Echo open _ftp.mysite.com
>>ftp2.txt Echo username
>>ftp2.txt Echo password
>>ftp2.txt Echo cd dir1\dir2
>>ftp2.txt Echo quit
ftp -s:ftp2.txt
The file is present in dir2.
FTP site can be accessed from above code, but not the file properties.
Kindly help.
Thanks
command dir gets some info about files
For your batch file:
#echo off
>ftp2.txt Echo open _ftp.mysite.com
>>ftp2.txt Echo username
>>ftp2.txt Echo password
>>ftp2.txt Echo cd dir1\dir2
>>ftp2.txt Echo dir
>>ftp2.txt Echo quit
ftp -s:ftp2.txt
You will get a list of the records included date of file. Format of the records may vary widely from system to system (RFC 959 - File Transfer Protocol).
For example Windows FTP server may response:
12-04-11 08:00PM 209665 04a64ad1-33c6-48ec-9931-abcc6445c491
...
Linux may response:
-rwxrwxrwx 1 owner group 156 Jan 27 2010 warning.log
...
Related
I'm attempting to move multiple files on an FTP server to a different directory on the same server. So far, I've written a bash script that will login and retrieve any new files in the remote directory but the ftp command doesn't support a 'mv' command. Essentially the script would download the new file(s) and then once downloaded move the file(s) to a different directory on the same server. Please Note that the filenames will be different every time so the use of wildcards is important here.
Before you answer please note that this needs to be automated so using a GUI like Filezilla wouldn't help me as I would have to login to various ftp sites and move the files manually, also, keep in mind that I'm unable to ssh into any of the servers as they are managed by other company's and ftp access is all I'm able to get. Last thing, I won't know what the file names are so using a wildcard would be helpful.
Any help or guidance is truly appreciated.
Thank you!
Perhaps the rename command in ftp could work for you?
rename [from [to]]
Rename the file from on the remote machine, to the file to.
I gave it a bash with an old file I had sitting on a server and it seemed to do what you want:
ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||55572|)
150 Accepted data connection
-rw-r--r-- 1 sinasohn sinasohn 21 Mar 31 16:37 tmp/testfile01
226-Options: -a -l
226 1 matches total
ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||64715|)
150 Accepted data connection
226-Options: -a -l
226 0 matches total
ftp> rename tmp/testfile01 tmp2/testfile01
350 RNFR accepted - file exists, ready for destination
250 File successfully renamed or moved
ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||56698|)
150 Accepted data connection
226-Options: -a -l
226 0 matches total
ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||50239|)
150 Accepted data connection
-rw-r--r-- 1 sinasohn sinasohn 21 Mar 31 16:37 tmp2/testfile01
226-Options: -a -l
226 1 matches total
ftp>
I put blank lines in between commands here for clarity.
Hope this helps!
full script to achieve move more than one file
1. get file list from ftp server with mls command
2. generate to do list file
2.1 get file
2.2 rename (move file)
3. execute ftp command with to do list file
#!/bin/sh
clear
# change local directory
cd [local-directory]
#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls abc*.* list.txt
quit
EOF
# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
echo >>todo.lst cd /OUT
echo >>todo.lst get $N
echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt
echo >>todo.lst quit
# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst
# cleanup
rm todo.lst
I have this script to upload files to FTP (I know FTP is not secure, though client insists of using FTP..). It works fine, but the problem with it is does not recognize path provided when doing upload, even though message says it uploaded successfully, but nothing is uploaded.
So the script looks like this:
#!/bin/bash
HOST=host
USER=user
PASS=pass
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ftp -inv $HOST << EOF
user $USER $PASS
get
cd /path/in/ftp/
prompt
mput $DIR/*.csv
# End FTP Connection
bye
EOF
rm $DIR/*.csv
Here what is outputted:
Connected to host.
220 You have connected to Client's FTP server.
?Invalid command
331 User name okay, need password.
230-Welcome user from ip. You are now logged in to the server.
230 User logged in, proceed.
Remote system type is UNIX.
Using binary mode to transfer files.
?Invalid command
250 Directory changed to "/path/in/ftp/"
?Invalid command
Interactive mode on.
mput /path/inv_numbers_2016-11-21_12_09.csv? 200 PORT command successful.
150 File status okay; about to open data connection.
226 Closing data connection. Transferred 140 bytes in 1 seconds. 0KB/second.
140 bytes sent in 0.00 secs (1395.1 kB/s)
?Invalid command
221 Session Ended. Downloaded 0KB, Uploaded 1KB. Goodbye user from ip.
Now if I change mput $DIR/*.csv to mput *.csv, then it works (I get same log output like with previous one, except it shows path as being directly in directory where script is). But this only works if I would run script directly from directory it is placed in.
Any ideas?
Replace
ftp -inv $HOST << EOF
by
cd "$DIR" && ftp -inv $HOST << EOF
or by
cd "$DIR" || exit 1
ftp -inv $HOST << EOF
I have been stuck in this problem in a few days now and I really need help. My goal is to FTP a certain file into a bridge server. But before I can FTP, I need to enter some login credentials first. I want the login part to be automated that's why I created a separated parameter file. That parameter file has the login details.
So when I run the script, first it will create a txt file. Then the text file will be passed into the bridge server. Now, the script will also pass the login details from the parameter file to access the bridge server and finally a successful FTP. Any way to do this?
FTPFILE="File to be ftped"
Lets say the parameterised file has the details in the format
HostName username password.
Read the file contents using a loop statement like or however you like
I am using a while loop here
while read hostname username password
do
HOST=${hostname}
LOGIN=${username}
PWD=${password}
done
write the details - hostname,login, password to the $HOME/.netrc file
echo "machine ${HOST} login ${LOGIN} password ${PWD}" > /$HOME/.netrc
echo "macdef init" >> /$HOME/.netrc
echo "put ${FTPFILE} " >> /$HOME/.netrc
echo "bye" >> /$HOME/.netrc
echo >> /$HOME/.netrc
Ftp statement (Ftp first looks for .netrc file in $HOME directory to initiate the login process. If the file is not found then the username and password will be prompted)
ftp -i $HOST
This code will do the job:
#!/bin/sh
FTP_USERNAME=username
FTP_PASSWORD=password
FTP_SERVER=server_domaine
touch /directory/textfile.txt
run_access_server()
{
lftp <<STOP
#automatically access the server
open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_SERVER
#changing directory
cd /directory/on/server
lcd /from/where/you/fetch/
#upload the file using get
mget textfile.txt
bye
STOP
}
run_access_server
Tell me how it works out with you.Regards
I´d like to make Windows .cmd script file. One of its commands makes use of the ftp command. When I execute the batch script, the commmand prompt does not wait for the user to type his user/password, so the next commands are not properly executed. How can I make it to pause?
Thanks.
Edited:
My question was more related to the interactive use of the command (-i option) than the automatic login (-n option). I want to wait for the user to enter their credentials.
Also, I have seen that by typing the command:
ftp -n -i -s:myFtpCommands.txt 192.168.0.20
and that myFtpCommands.txt contains:
use myUser
mget *
bye
There is no need to type the password to get files. Where is the associated security problem?
You could ask the user for their details before hand, then build the command file based on their input.
#echo off
set /p un=Enter your FTP username:
set /p pw=Enter your FTP password:
echo open 192.168.0.20 >ftpscript.txt
echo %un% >>ftpscript.txt
echo %pw% >>ftpscript.txt
mget * >>ftpscript.txt
bye >>ftpscript.txt
ftp -i -s:ftpscript.txt
With regards to the auto login, this is a feature of FTP, it's an anonymous login, that is setup on FTP servers to allow this sort of access.
Whoever setup the FTP server would be able to control the accounts that are allowed to be used to access the server, and the permissions on the files that they don't want everyone to have access to.
I have a text file on one computer that I want to send to a folder on a ftp site.
Can someone please show me a batch file code that will login to the FTP site with the username and password, and copy the text file.
Thanks for your help.
If you mean batch as in Windows batch, you can do that with the following script tst.cmd:
#ftp -n -stst.ftp myTargetMachine.com
(replacing myTargetMachine.com with the name of your actual FTP server) and the following FTP command file tst.ftp:
user myUser myPassword
dir
bye
Obviously, you should replace the myUser and myPassword with your actual username and password, and also dir command with whatever you really want to do, such as:
put localfile.txt /fullpath/remotefile.txt
If you're talking about a UNIX-like environment, the script would be:
#!/bin/bash
ftp -n myTargetMachine.com <<EOF
user myUser myPassword
dir
bye
EOF
Same deal with the FTP server name, user ID and password, of course.
And, in response to your comment:
Yes, I am talking about from a Windows environment. So for arguments sake, let's say the text file is C:\textfile.txt and I need to copy it to the FTP site in the folder called BACKUPS.
You would use a script like the following transfer.cmd:
#setlocal enableextensions enabledelayedexpansion
#echo off
c:
cd \
ftp -n -stransfer.ftp myTargetMachine.com
endlocal
and transfer.ftp:
user myUser myPassword
put textfile.txt /backups/textfile.txt
bye
If you wanted the Windows version as a single file, you could use something like:
#echo off
echo user myUser myPassword>tst.ftp
echo dir>>tst.ftp
echo bye>>tst.ftp
ftp -n -s:tst.ftp myTargetMachine.com
del /q tst.ftp
which temporarily creates tst.ftp and then deletes it when it's finished.
Command:
ftp -n -sScriptName HostName
And in the Script:
[User_id]
[ftp_password]
ascii
put myfilehere.html /remotedir/remotename.txt
quit