Using a shell script to interact with programs - linux

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.

Related

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.

Shell script - SSH

I am using shell script to add some file to server. Is there any way to write a shell script that will execute one part on local computer and the other part when you're logged into that server?
For example, I want to log in, do something, add some file, and then I want to list everything on that server.
ssh something#something
I enter password
Then list files from server.
You can just add a command to end of the ssh command; for example:
ssh username#host ls
will run ls on the server, instead of giving you a login shell.

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.

how to read username & password from file in shell script?

I want to login to remote computer using shell script through ssh, i want to pass username and password from text file. how to do that?
here is code what i have tried.
#!/bin/sh
#
username=user
ip=192.168.2.21
ssh $username#$ip < user.txt
You can't SSH in the method that you're referring to. You will want to use SSH keys which are created to help with what you're doing.
Also, avoid storing your password in a text file.

linux script to automate ftp operation

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

Resources