linux script to automate ftp operation

2020-02-13 06:28发布

问题:

I need to transfer a file from my linux server to a FTP server.

My shell script is :

    #! /bin/ksh

    HOST='my_ip'
    USER='userid'
    PASSWD='password'
    FILE='file.txt'
    DIREC='/eir_log'
    ftp -in $HOST << EOMYF 
    user $USER $PASSWD
    binary
    mkdir $DIREC 
    cd $DIREC
    pwd
    quit
    EOMYF

pretty simple code. but the problem is though I am logging in the FTP server fine, but its not allowing me to create a new directory in the FTP server. At first i thought some error with my script, but even individually if i run a mkdir in the ftp server its showing create directory failed. Can somebody let me know the possible error, or if any eror in my code that i am missing out on.The pwd is working fine though, which means there is no problem loging in the ftp site through script.

Thanks in advance for your help

回答1:

Have a look at expect

Something to get you started

#!/usr/bin/expect

set timeout 120
spawn ftp 192.168.0.210
expect "Name"
send "root\r"
expect "Password:"
send "pass\r"
expect "ftp> "
send "bye\r"


回答2:

Probably lftp ( ftp scripting client ) will be something you need ( look among your distro's packages ). Error creating directories is probably related to permissions of the dir inside which you try to create it.

http://lftp.yar.ru/desc.html



回答3:

Have you tried using SCP (Secure Copy)?

scp -r /dir/to/"file.txt" username@hostname

*if you're in the directory of the file/folder you wish to send then you don't need to define the complete filepath

Have a look at this article if you want to scp without having to enter your password. http://www.thegeekstuff.com/2008/06/perform-ssh-and-scp-without-entering-password-on-openssh/



回答4:

If you are root or have admin privileges then you shouldn't need to sudo your way into making a directory. It will be best to run remote commands without sudo just in case a malicious code piggybacks your script



标签: linux shell ftp