I am writing my first shell-script ever and I am trying to connect to an FTP server. However, I am utterly at a loss for how to do this. I tried a google search, but I am still stumped.
I am trying to connect with a username and password (not a ssh id).
Thanks for your help. Again this is my first shell-script ever.
The command man ftp should give you the necessary pointers.
This being said, this page might help you build a complete shell script
Here how you connect to FTP server via a shell-script :
nano MyConnectFTPScript.sh
#!/bin/sh
$HOST='hostAdresss'
$USER='NameOfUser'
$PASSWD='YourPass'
$FILEtoPut='myFile1'
$FILEtoGet='myFile2'
$FILEtoDelete='myFile3'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILEtoPut
get $FILEtoGet
delete $FILEtoDelete
quit
END_SCRIPT
exit 0
chmod +x MyConnectFTPScript.sh
and execute :
./MyConnectFTPScript.sh
I hope these will be helpful.
Samir
Related
I want to upload a file using a shellscript. I know scp is the better way to solve such a task, but I need to use FTP. This is what I got:
#!/bin/bash
HOST='myServer'
USER='anonymous'
PASSWD=''
DIR = '/Public'
ftp -n -v $HOST << EOT
user $USER $PASSWD
prompt
cd $MYDIRECTORY
mput cam.jpg
bye
EOT
The directory Public is accessible with the anonymous user. I'll get this response:
Connected to myServer.
220 Some text
331 Anonymous login ok, send your
complete email address as your password
There is still a prompt for a username. I don't get this and I don't see the error... Thank You for any advice
Anonymous login requires a password, but you're trying to send an empty password. Change it to:
PASSWD=user#yourdomain.com
Well, at least this should work but I think there is a much better solution to your issue.
Try
PASSWD='\n'
This should "work" and solve your problem.
Suggestions: Make sure you can interactively log in with 'anonymous' id and it works
Another thing I noticed, you should remove the space before and after the equal sign. Otherwise bash will say 'line 5: DIR: command not found'
DIR = '/Public'
Also, it is not clear that $MYDIRECTORY is already defined the environment. It is not in the script.
I am trying to write a script to automatically upload files to a sftp server. My problem is authentication.
I know it is not possible to store a password in a bash script for sftp.
I can't use keys because the admin of the server won't allow me.
I don't want to use any extras (sshpass/expect) because I can't
guarantee they will be on the machine I'm using (the script are wanted so that the processes are not tied down to a particular machine).
Manual entry of the password is not a problem I just need to get the script to wait for the user to put the password in. At the minute when I run the script it opens terminal, prompts for the password, but when this is entered nothing else happens. If I enter the lines of code manual after it uploads everything correctly.
#!bin/bash/
cd /remote_directory
lcd /local_directory
put some_file.txt
After months of looking for an answer I have finally found the solution. It was in a comment on an answer in some other thread I can't even remember. Hope this can help others out there.
Your bash script should look like this and will connect to the sftp server, prompt the user for the password, and then execute the remaining commands.
#!/bin/bash
sftp user#server <<!
cd /the/remote/directory
lcd /your/local/directory
put/get some.file
!
First I have to say you that I'm a newbie, but I wanna learn!
I need a script which suppose to check the ftp and sftp communication. This is for 26 ftp and sftp.
Script has to execute just 1 times and script suppose to login to ftp and sftp servers using username with password, easy from an extern list.
If ftp works also it has to send an mail and even if it is fail also it suppose to send the mail. Or just a print on screen with status : alive or not alive should be ok.
I am starting with the below script:
I found something like :
"You can use your script with a regular user, creating the file .netrc in the user's homedir (~/.netrc), with the following contents:
Code:
machine 192.1.1.1
login usename
password user-passwd"
...
ftp -v -n <<EOF > ${LOG_FTP} 2>&1
open ${IP_ADDRESS_SERVER}
user ${FTPUSER} ${FTPPASS}
...
EOF
I need now to understand how I can send the email to my email adres or just print on screen the results.
Please can you suggest me the right way or help me write the script.
It would be great help for me.
Thanks in advance for all your help!
Nico
you can use sendmail for sending emails to your email id. IF you're working on bash, then pre-pend this line to your script:
#!/bin/bash
Then do this on terminal:
chmod +x <scriptname> #setting executable permission for script
./<scriptname> #executing the script
I would like to automate a bash script, that connects to server using sftp and does a file transfer. I have the password for this, and initially I tried for this
sftp $acc#$host << EOF
<passwd_here>
cd $dir
get $file
quit
EOF
but it still prompted for password, and I had to enter it manually at the prompt.
After searching SO, I found this post which had a solution with expect, which I tried and I got the following error:
Script:
sftp -b cmdfile.txt $acc#$host
expect "Password:"
send "<passwd>\n";
interact
Error:
Permission denied (publickey,keyboard-interactive).
cmdfile.txt
cd $dir
get $file
quit
Please let me know, How to connect using the password in a bash script?
Please try the below steps
lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
With scp/sftp you should use key-based authentication. Public key from the user you want to authenticate copy into ~/.ssh/authorized_keys file on the server, into home directory of user on which you want log on. Storing password in clear text on client side is not a good practice, you know :) That way you "workaround" problem of reading password from the prompt too.
Yes key-based auth is the way to go.
Check here for some direction.
I am trying to connect to my ftp server by running a batch script. I can log into it fine by typing the following:
ftp home.ptd.net
USERNAME
PASSWORD
cd Public
put FILENAME.txt
bye
But if I put this into a script in this order, it connects to the server, but still asks for a username. Can anyone point me in the right direction on how to make it login? Thanks.
please see the following msdn article:
ftp reference from msdn
i think you need to place the commands in a text file and pass it to the ftp command with the -s parameter