linux script to automate ftp operation - linux

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

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"

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

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/

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

Related

Linux crontab script log into a local user in ssh

At work on a Linux server, I try to find a way to execute a script by crontab(in root) for:
Log into a local user by ssh with the command ssh -X user#localhost
answer at the password question for the user
execute a command
wait the end
exit ssh session
I make the process because it works when I do this manually.
I can't use crontab Otherwise than in root.
I try to add a public key for avoid the password step but I don't succeed into.
I must execute my command log into a specific user.
And finally I find a way to get environnement variables with the options -X ... if I log into the specific user with the command su -l user ... my command won't works.
I don't know if I am clear, I am sorry because my knowledge in Linux environments are limited and I try my best to explain to you in an approximate English (I'm French)
Best regards
Sorry I may not enough explicit I will try differently
First of all my first message is not too specific because I don't know if my way to deal with my issue is the best way that why I had explain it in a general way
Like I said I must configure crontab with root user, but the command (specific command of an application installed on my Linux server) that I try to execute automatically must be execute with a specific server user.
With all those constraints I try to develop an except script
[code]
!/usr/bin/expect
spawn ssh -X specific-user#localhost
expect "assword :"
send "PASSWORD\r"
sleep 10
SpecificUtility + some parameters
Expect "localhost #"
Send "exit\r"
interact [/code]
If I execute this script manually it is ok but in crontab the sequence if not respected
I do something wrong but I have no clue
I hope explain it better
Best regards

copy file from one server to another in linux

how to run commands like ftp or sftp or scp in background? Also how to setup password less connection for running these command?
Look for manual pages for scp or rsync, which both can do this job well, if not being forced you don't want to use sftp or even the non encrypted ftp file transfer!
something like the following, for example:
rsync [some other parameters] -e ssh SOURCE TARGET
Assuming these commands are coming from a bash script , you would need to make sure that the two (or more ) systems have ssh certificates generated that allow you to access said systems without providing a "password" per se.
Briefly, you could do it by running this command on one system:
ssh-keygen
following through, this will generate a key. Then run:
ssh-copy-id user#some-remote-system
to copy it to the remote system, which will allow passwordless access, enabling scripts to go about their business without stalling for password prompts.

ftp command not found only on shell script

I'm trying to use FTP on shell script to put a file from one server to other one. Following the code
HOST='206.35.8.213'
USER='my_username'
PASSWD='_mypassword'
FILE='mydata.log'
PATH='/export/home/oracle/europa/'
ftp -inv $HOST << EOF
user $USER $PASSWD
cd $PATH
put $FILE
bye
EOF
this is throwing the error as follows
./ftp.sh: line 9: ftp: command not found
Here the FTP available on the server. Could any one help on identifying the issue.
You've over written the PATH variable which is used to search for binaries. What you've effectively told the shell is that the ftp binary will be in /export/home/oracle/europa/. Change your variable name to something else.
As a rule, bash uses upper case for it's variables, see Shell Variables in the bash manpage. I'd recommend using a different naming convention for your own variables. If you had named PATH, path exportDir or similar you wouldn't have had any problems.
The package for FTP server is vsftpd , but for ftp client it is ftp .
You need to install ftp service to start ftp sessions.

Using a shell script to interact with programs

I need to upload a file via FTP with shell script.
Can I interact with FTP from the script? My Script is at http://pastebin.com/A76fsaM3
cd MyLocalUploadDir
ftp
open ftp.server.com
myusername
mypassword
put LocalIteam.TXT
Will this work? Is there another way to interact with FTP from within a shell script? Any help is appreciated!
See http://www.tldp.org/HOWTO/FTP-3.html for information regarding interaction with FTP.
You need to use a here-doc. Otherwise, the lines after ftp will not be used as input to the program.
cd MyLocalUploadDir
ftp <<EOF
open ftp.server.com
myusername
mypassword
put LocalIteam.TXT
quit
EOF
You can also avoid putting your username and password in the script by using a .netrc file.
And instead of using the ftp program, you could use curl to do it in one line.
curl --upload-file MyLocalUploadDir/LocalIteam.TXT 'ftp://myusername:mypassword#ftp.server.com/'
You can also use the --netrc option to curl to tell it to get the username and password from the .netrc file.

How to login to an ftp server using shell scripting in linux?

I want to login to my FTP server using shell scripting. I made a .sh file having contents as
ftp
open 172.31.1.45
but it's not working. The second command is not executing. Please help. I am new to linux so please forgive if this is a stupid question
try this:
#!/bin/sh
HOST="yourhost"
USER="user"
PASSWD="pass"
FILE="file.txt"
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
If it's just a matter of simple file transfers, you might want to look at using a tool like ncFTP, wget, or cURL, which can do file transfers in a single command. Using these tools, you can simply pass the username, password, host address, source file, and destination file in one command.

Resources