shell script to check ftp and sftp communication - ftp

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

Related

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

Pause for password sftp bash script file

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
!

pass password to sftp in a bash 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.

Batch script connect to ftp sever?

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

How do you connect to FTP server via a shell-script

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

Resources