Change shell script from ftp to sftp - shell

I have below shell script to get files from ftp server.
I need to change this script to point to same SFTP server ?
Can some one assist to change this script from ftp to sftp?
HOST='some.site.com'
USER='yourid'`enter code here`
PASSWD='yourpw'
FILE='file.txt'
ftp $HOST <<END_SCRIPT
user $USER
$PASSWD
put $FILE
quit
END_SCRIPT
exit 0

It's not so complicated, just use the sftp binary instead of the ftp. The FTP commands stay exactly the same (it's still the FTP protocol, just over an encrypted connection), the user may be specified as a part of the SFTP's host argument:
sftp $USER#$HOST <<EOF
$PASSWD
put $FILE
quit
EOF

Related

change ftp to sftp in a shell script

I have this shell script which transfers CSV files to another server using the FTP service, and I need to change this service to SFTP. Can anyone help me?
ftp -inv >$FTP_LOG_FILE <<EOF
open $FTP_HOST
user $FTP_USERNAME $FTP_PASSWORD
lcd $REPORT_LOCAL_SOURCE
cd $DESTINATION_DIRECTORY
mput *$FILE_TYPE
exit
EOF
Can you use public key authentication? That makes it pretty easy - no password required.
Also, personal preference - ftp gives you no way to reasonably interact with the file transfers and react to misbehavior. Try scp.
Assuming automatic public-key authentication and the same vars you used above -
scp $REPORT_LOCAL_SOURCE/*$FILE_TYPE $FTP_USERNAME#$FTP_HOST:$DESTINATION_DIRECTORY/
or, with shorter names...
if scp ldir/*$ext $me#$host:$dir/
then echo "No errors"
else echo "There were errors"
fi
Generally, try to never use all cap vars.

ftp username and pasword automated in shell script

I want to created .sh file
// Tried to connect to ftp server
ftp name_of_server
//input user name
username
//input password
password
link given below
https://github.com/prokid221/shell-programing.git
Instead of login, it again asked to enter username and password
can any one help with this problem?
If you only need file transfers, you could use curl.
download a file:
curl -O -u user:password ftp://example.com/some-file
upload a file:
curl -T some-file -u user:password ftp://example.com
Note: This method may result in your credentials being saved in your command history.
The best solution is to look at your ftp command manual. It probably provides command line flags or can use environment variables to allow you to specify username and password.
If there is no such thing, an alternate way is to feed ftp standard input. I guess this is what you try to do, but instead here is what your script does:
Run ftp and wait for the command to return. That's where ftp asks about username.
Once ftp returned, run a command named after the username. There is probably no command of that name so it will complain about it.
Then, run a command named after the password. It will fail too, but depending on the special characters in the password, it could become a disaster :-)
So, to really feed stdin, you can use printf(1):
printf "username\npassword\n" | ftp name_of_website
Edit: Another way I forgot is to put those informations in the URL: ftp://username:password#name_of_website.
Try :
#!/bin/sh
HOST='your.ftp.server.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
#put $FILE
#quit
END_SCRIPT
exit 0
If you want to provide hostname from outside the script as commandline, then you can use,
HOST = $1 ,
So if you scriptname is serverftp.sh, you would provide hostname as;
serverftp.sh <ftp_server_name>
how about use expect in shell script?
#!/bin/sh
SERVER="example.com"
ID="toor"
PASSWD="secret"
expect <<EOF
spawn ftp $SERVER
expect ": "
send "$ID\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "ls\r
expect "ftp>"
send "quit\r
interact
EOF

Script to use SFTP

I have an FTP Script as below. And I want the same to be converted to Secured FTP (SFTP) script.
FTP_OUT=`ftp -v -n << END_SCRIPT
open $HOST
user $USR $PWD
lcd $LOC_PATH
cd $REM_PATH
mput $FILENM
quit
END_SCRIPT`
Suggest the SFTP equivalent of the above FTP script to transfer file from local server to remote server.
You should use scp
scp filename user#host:remote_path
This requires you to enter the password. If you want to avoid entering the password every time you use scp, you could generate an authentication key with ssh-keygen.

Download files from specific folder in AIX box

I have an AIX box. I wanted to connect to remote FTP server and download a specified folder "abc".
I have created a script; but it isn't working.
Here is my Code:
#!/bin/sh HOST='ftp.abc.xysz.net' USER='ftp' PASSWD='password' FILE='ababababababababababababab.abab';
ftp $HOST user $USER $PASSWD mget $FILE
quit END_SCRIPT
exit 0
Here is the error I receive when I execute Script..
Anyone has any idea to download the files from remote FTP server. Is there any single command available
Your script doesn't look properly formatted... it would be something like this:
#!/bin/sh
HOST='10.129.151.41'
USER='technicolor'
PASSWD='blueray'
FILE='ababababababababababababab.abab'
ftp -n <<END_SCRIPT
open $HOST
user $USER $PASSWD
get $FILE
quit
END_SCRIPT

ftp shell script breaks up after the lcd-command

I created a shell script to download several files, all starting with "2014" from an ftp server. I use mget for this and the filename 2014*.
To make sure that the files are saved at the right local place I use lcd before.
It looks like this:
#!/bin/sh
HOST='ftpserver.name.de'
USER='user1'
PASSWD='pw1'
FILE='2014*'
LOCDIR='/home/local/data2014/'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd $LOCDIR
mget $FILE
quit
END_SCRIPT
exit 0
when I try this, the script just runs:
lx9000: ftp_get.sh
Connected to ftpserver.name.de.
220 FTP-Server: ftpserver.name.de
331 Password required for user1
230 User user1 logged in
Local directory now: /home/local/data2014/
221 Goodbye.
why dose it stop before the downloading proceeds?
Thanks for help!
Try adding:
prompt off
before the mget.

Resources