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
Related
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
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.
I had made a script that allows ftp login.But I dont know how to add password for the user
#!/bin/bash
if grep -q $1 "/opt/proftpd.conf"; then
echo "$1 is an ftp user"
HOST='192.168.1.212'
USER=''$1''
FILE=''$2''
ftp -n -v $HOST <<END_SCRIPT
quote USER $USER
put $FILE
quit
END_SCRIPT
ls -la /home/$1
exit 0
else
echo "$1 is not an ftp user"
fi
exit 0
How can I add the user password for ftpuser?...
An example:
#!/bin/bash
ftp_host="192.168.1.212"
ftp_user="$1"
ftp_file="$2"
read -s -p "Enter password for user ${ftp_user}: " ftp_pass
ftp -n -v ${ftp_host} << EOF
quote USER ${ftp_user}
quote pass ${ftp_pass}
bin
put ${ftp_file}
EOF
It depends on the ftp version.
Sometime versions allow to give user and password together with host name:
ftp://[user[:password]#]host[:port]/path
There are also two ftp commands that allow to pass credentials (man ftp):
account [passwd]
Supply a supplemental password required by a remote system
for access to resources once a login has been successfully
completed. If no argument is included, the user will be
prompted for an account password in a non-echoing input mode.
user user-name [password] [account]
Identify yourself to the remote FTP server. If the password
is not specified and the server requires it, ftp will prompt
the user for it (after disabling local echo). If an account
field is not specified, and the FTP server requires it, the
user will be prompted for it. If an account field is speci-
fied, an account command will be relayed to the remote server
after the login sequence is completed if the remote server
did not require it for logging in. Unless ftp is invoked
with "auto-login" disabled, this process is done automati-
cally on initial connection to the FTP server.
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 am writing my first shell script ever and trying to figure out how to transform this command:
sftp -o IdentityFile=/home/test/test/id_dsa test#test.test.com < sftp_put.txt;
into an equivalent command where I connect to a ftp server. The key difference is that I will be logging into this server via a username and password not my ssh credentials. Note I am trying to upload two files.
Again any help would be more than appreciated!
You can use .netrc for this:
$ cat > .netrc
machine your.machine.ip.address
login your.login.name
^D
$ ftp your.machine.ip.address < ftp_cmds.txt
Which would prompt you for a password. If you're okay with it, you can save the password (clear text) in .netrc to skip this prompt. See man netrc for more details.
ftp -v -n <<EOF > ${LOG_FTP} 2>&1
open ${IP_ADDRESS_SERVER}
user ${FTPUSER} ${FTPPASS}
cd ${REMOTE_DIR}
put sftp_put.txt
EOF