I have a requirement to connect to an FTP server from Unix, and download a particular file which has the most recent date/time stamp.
For example here is what the file name might look like: FILE_NAME_W5215.ZIP
The "W5215" part is the date/time stamp.
If I was to try and get the latest file locally I would do something like this:
ls -t FILE_NAME_W*.ZIP | head -1
however that doesn't work on the remote server.
I don't know what OS the FTP server is running on. I know that when I establish the connection, a lot of the commands that I can do locally on Unix don't work when I'm connected to the FTP.
Any ideas, thoughts, suggestions would be greatly appreciated.
You can do something like this:
Get the file list on ftp server in a temp file
ftp -n $SERVER >tempfile <<EOF
user $USER $PASSWORD
ls -t
quit
EOF
Get the latest filename from the list
filename=`cut -c57- tempfile|head -1`
Note: In ls file list, filename starts from the 57th position, change it if necessary
Now get that particular filename from ftp server
ftp -n $SERVER<<EOF
user $USER $PASSWORD
get $filename
quit
EOF
Related
I've written a script that checks for a specific file of format("OLO2OLO_$DATE.txt.zip") in the ftp server and then copies it to my local machine:
/usr/bin/ftp -n 93.179.136.9 << !EOF!
user $USR $PASSWD
cd "/0009/Codici Migrazione"
get $FILE
bye
!EOF!
echo "$FILE"
But I'm not getting the desired result from this.
This line triggers the error.
SOURCE_FOLDER="/0009/"Codici Migrazione""
It tries to execute the command Migrazione with the environment variable SOURCE_FOLDER set to /0009/Codici which doesn't exists.
What you probably wanted to do was:
SOURCE_FOLDER="/0009/Codici Migrazione"
I tried like this :
#!/bin/bash
hostname="xx.xx.xx.xx"
username="ftp"
password="123456"
ftp $username:$password#$hostname <<EOF
read filename
put $filename
quit
EOF
erorr is coming as below :
ftp: ftp:123456#10.64.40.11: Name or service not known
?Invalid command
Not connected.
If my question is too easy , please don't bother to answer.
I am beginner and trying to learn. Any help will be appreciated.
The problem you're running into is that the default FTP client doesn't allow you to specify user and password along with host at the command line like that. It's looking for a host named ftp:123456#10.64.40.11, which clearly wouldn't exist. You can specify the host at the command line, but that's it. This situation and solution is well described in this article, which contains other versions and examples.
The basic idea is to turn off "auto-login" with -n and specify the user and password inside the HERE document instead of at the command line:
#!/bin/bash
hostname="xx.xx.xx.xx"
username="ftp"
password="123456"
ftp -n $hostname <<EOF
user $username $password
read $filename
put $filename
quit
EOF
(Notice that I added the $ to read filename, which appeared to be a typo in your original version.)
There are other FTP clients that allow for user and password specification at the command line (such as ncftp), but using the one you have seems the simplest option.
I need to get a file from a remote host in Unix. I am using the ftp command. The issue is I need the latest file from that location. This is how I am doing it:
dir=/home/user/nyfolders
latest_file=$(ls *abc.123.* | tail -1)
ftp -nv <<EOF
open $hostname
user $username $password
binary
cd $dir
get $latest_file
bye
EOF
But I get this error:
(remote-file) usage: get remote-file [ local-file ]
I think the way I am trying to get the file from within the ftp command is incorrect, can someone please help me out?
You cannot use shell features, like aliases, piping, variables, etc, in ftp command script.
The ftp does not support such advanced features using any syntax.
Though, you can do it two-step (to make use of shell features between the steps).
First get a listing of remote directory to a local file (/tmp/listing.txt):
ftp -nv <<EOF
open $hostname
user $username $password
cd $dir
nlist *abc.123.* /tmp/listing.txt
bye
EOF
Find the latest file:
latest_file=`tail -1 /tmp/listing.txt`
And download it:
ftp -nv <<EOF
open $hostname
user $username $password
binary
cd $dir
get $latest_file
bye
EOF
I'm running the following command (where variables have valid values for ssh command and $file - is a .sql file).
nohup ssh -qn ${ssh_user}#${dbs} "sqlplus $dbuser/${dbpswd}#${dbname} <<ENDSQL | tee "${sql_run_output_file}".ssh.log
set echo off
set echo on
set timing on
set time on
set serveroutput on size 1000000
#${file}
ENDSQL
"
When I was using the above command without "nohup" before ssh command, after 1 hour or so, my connection from source server (where im running ssh) was getting an error/message "Connection reset...." and hanging my BASH shell script (which contains this ssh command in it). When, I use nohup, i dont see the connection issue.
Here's what I'm trying to get and need your help.
Change the command shown above so that the command will NOT create a nohup.out
(Did I read that I can use > instead of | tee ... and use 2>&1)
I DO NOT want to run the command giving a "&" (background)
I DO want a LOG file for the sqlplus session that's running on the target DB server via ssh command/connection (initiated from source server).
Thanks.
You can still lose the connection when running ssh under nohup, so it's not really a good solution. If possible, I would recommend that you copy the sql file via scp to the target server, then ssh in to the server, open a screen and run the command from there (Or run it under nohup). Is that an option?
I'm trying to get terminal to upload a file for me, in this case: file.txt
Unfortunately, it won't work, no matter what I try.
#!/bin/bash
HOST=*
USER=*
PASS=*
# I'm 100% sure the host/user/pass are correct.
#Terminal also connects with the host provided
ftp -inv $HOST << EOF
user $USER $PASS
cd /Users/myname/Desktop
get file.txt #which is located on my desktop
bye
EOF
I've tried 100 different scripts but it just won't upload :(
This is the output after saving to an .sh file, chmod +x and sudo the .sh file:
Connected to *hostname*.
220 ProFTPD 1.3.4b Server ready.
331 Password required for *username*
230 User *username* logged in
Remote system type is UNIX.
Using binary mode to transfer files.
550 /Users/myname/Desktop: No such file or directory
local: file.txt remote: file.txt
229 Entering Extended Passive Mode (|||35098|)
550 file.txt: No such file or directory
221 Goodbye.
myname:Desktop Myname$
I've browsed through many other topics about the same issue here, but I just can't figure it out. I've started playing with UNIX since this morning, so excuse me for this (probably) foolish question.
Try:
#!/bin/bash
HOST=*
USER=*
PASS=*
# I'm 100% sure the host/user/pass are correct.
#Terminal also connects with the host provided
cd /Users/myname/Desktop # Go the the local folder where the file is located in
ftp -inv $HOST << EOF
user $USER $PASS
cd /User/$USER/Desktop # Go to the folder in which you want to upload the file
put file.txt #which is located on my desktop
bye
EOF
So use put and make sure your file is the current working directory and the remote directory exists.
You are using get but talk about an upload. Probably you just want to use put?
Anyway, I'm not sure this can be done using a basic ftp client. I'm always using ncftp for things like this. This comes with command line utilities like ncftpput which accept command line arguments and options to perform the task.
Alfe is right, you need to use put <filename> to upload a file to FTP. You can find a quick guide here. It should be possible using the basic FTP tool but I would also recommend ncftp :-)
You need to use put to upload a file.