linux script to automate ftp operation

2020-02-13 05:52发布

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

标签: linux shell ftp
4条回答
劫难
2楼-- · 2020-02-13 06:35

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

查看更多
祖国的老花朵
3楼-- · 2020-02-13 06:38

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

查看更多
够拽才男人
4楼-- · 2020-02-13 06:39

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"
查看更多
Bombasti
5楼-- · 2020-02-13 06:39

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/

查看更多
登录 后发表回答