automate sftp using unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting automate sftp using unix script
# 22  
Old 10-17-2006
Duh, dummy me. Yes, after I posted a reply I realized what I should have done. I added the ip address of the secure server to ftpsvr and the name of the secure server for hit-dw. I also tried using the server I was ftping from for ftpsvr and that didn't work either. I got the following error message:
Code:
ftp: connect: Connection refused
Not connected.
Not connected.
Not connected.
Not connected.

# 23  
Old 11-06-2006
Even i'm also getting the same ftp: connect: Connection refused error when i'm trying out the below code

Code:
HOSTNAME=xx.xxx.xx.xxx
USER=username
PSSWD=password
datedir=`TZ=CST+48 date +%m%d%Y`
REP_SRC=/path/path1/$datedir
REP_DEST=/path2/dir
FILENAME=abc.txt

rm -f log_file.tmp

ftp -i -n -v $HOSTNAME> .ftpout$$<<EOF
open $HOSTNAME
user $USER $PSSWD
ascii
cd "$REP_SRC"
pwd
lcd "$REP_DEST"
get $FILENAME TEST_abc.txt
bye
EOF

While i'm trying to execute the same code for different remote machine its working fine. But in particular with one machine it isn't working ...

Do anyone know why its behaving like that ???
# 24  
Old 03-16-2007
sftp with lftp

Today I had the same problem, that I need to do an sftp connection for a script that will not run in interactive mode.

Code:
#!/bin/sh

HOST=XX.XXXX.XXX
USER=yourusername
PASS=yourpassword

echo "Starting to sftp..."

lftp -u ${USER},${PASS} sftp://${HOST} <<EOF
cd somedir
get somefile
bye
EOF

echo "done"

Note: lftp needs to get told which protocol to use. The above example works for sftp.
# 25  
Old 03-16-2007
Not sure if this info is still desired, but I have just finished an auto sftp script.
All servers run a script to backup files, then this script runs to gather all the output files and centralize.
ssh keys must be set up and working for script to work. hope it helps.

Code:
#!/bin/ksh

HOSTS="svr1 svr2 svr3 svr4 svr5"
DATE=`date '+%m%d%y.%H%M'`
LOG=/usr/local/scripts/xfer_failed

export HOSTS DATE LOG

### Main ###

if [ -f $LOG ];
 then mv $LOG $LOG.$DATE
 touch $LOG
 else touch $LOG
fi
#
for each in $HOSTS;
do
  if [ ! -d /usr/restore/$each ];
    then mkdir /usr/restore/$each
  fi
cd /usr/restore/$each
find . -type f -name "$each*.tar*" -mtime +14 -exec rm {} \;
#
sftp -b - $each << EOF
get /root/restore/$each.tar $each.$DATE.tar
bye
EOF
  if [ $? -ne 0 ];
    then echo "File transfer for $each failed. $DATE" >> $LOG
    cat $LOG | mailx -s "Vitalfile transfer failure" xxxxxxx@xxxxxx.com
    else continue
  fi
done

find /usr/local/scripts -type f -name "xfer_failed.*" -mtime +4 -exec rm {} \;

### End Main ###

# 26  
Old 04-08-2007
Hi,

I know some of you are looking for a solution on automating sftp using expect to work with Cron. I've been searching for the solution to thi sissue for weeks now. I've finally found the solution and thought I should share.

first I used autoexpect to generate the script from my sftp session, then i modified it to read the password from a file.

autoexpect sftp -b Batchfile username@sftpserver

here is the completed script. It works with cron also.

Code:
#!/usr/bin/expect -f
#
set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set authFile "Path to password file"
if {![file exists $authFile]} {;
send_user "$authFile does not exist; aborting\n"
exit 1
}
set fileFD [open $authFile r];
gets $fileFD authLine;
close $fileFD

set timeout -1
spawn /usr/bin/sftp -b batchfile username@sftpserver.com
match_max 100000
# note: the autoexpect command will produce the line below. helpful if the #sftpserver has a long login banner.
expect -exact "user@sftpserver.com\r
send -- "$authLine\r"
expect eof


hope this helps. it works for me. I call this script from a batch file Smilie Smilie .
# 27  
Old 04-25-2007
Hi,

I did the same as you did. but what is the reason why i can't make it work? btw, this is my script.
Code:
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
spawn /usr/bin/sftp user1@server1
match_max 100000
expect -exact "Connecting to server1...\r
user1@server1's password: "
send -- "hardcode_password\r"
expect -exact "\r
sftp> "
send -- "cd .ssh\r"
expect -exact "cd .ssh\r
sftp> "
send -- "get file1\r"
expect -exact "get file1\r
Fetching /users/user1/.ssh/file1 to file1\r
sftp> "
send -- "exit\r"
expect eof

i think the only difference we had is how the password file was handled. what is the $authfile in your script?

thanks
# 28  
Old 05-04-2007
Unattended SFTP

Hi All,

I'm attempting the same thing here; my particular problem is that I'm trying to get files from a UNIX box to a VMS box with different account names. So, I believe, the shared public key option wouldn't work for me (because the account names have to be the same?).

Anyway, I'm interested in the 'expect' solutions posted by the previous participants of this thread but they don't seem to work for me. In the last post there's a variable $arg mentioned - is this the password? And is this passed on the command line?

Many thanks,

p.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sftp automate

hi, I am trying to automate a file download process using sftp. There is some logic to download files. 1) I need to login to destination server and then go to folder. 2) find list of files and count 3) using list of files I need to eliminate three selective files and download remaining... (1 Reply)
Discussion started by: getmilo
1 Replies

2. UNIX for Dummies Questions & Answers

Automate sftp process using script

Hi, guys, I am trying to automate a sftp process using "expect" method (since the key authentication method is disabled in my company network, there is no helping it). In order to try, I type in the command manually: sftp @ > << EOF >cd >ls -l >EOF >Connecting to @servername password: ... (3 Replies)
Discussion started by: warmboy610
3 Replies

3. Shell Programming and Scripting

SFTP script to automate login in to remote server

Greetings, guys. I'm not much of a programmer forgive me for being a noob, because of someone leaving, I was put in an IT spot where I have to figure out a few things. Being new to Linux and programming has been a challenge. My boss has asked me to create an automated script to connect to a 3rd... (7 Replies)
Discussion started by: giovannym
7 Replies

4. Shell Programming and Scripting

Using expect to automate sftp

I am trying to use a for loop in my expect cmdFile that I am calling. I want to be able to call either one file name or a series of file names in the working directory (that I won't know the names before hand) and then pass the names to the sftp program. Something like for i in (ls *txt) do (0 Replies)
Discussion started by: vedder191
0 Replies

5. UNIX for Dummies Questions & Answers

automate sftp in sun solaris.

Hi, I'm using Sun Solaris OS. I have configured sftp and can exchange files in command prompt. Now when I try to automate it in ksh script, facing issue as I want to capture the status if the transfer was successful or not. So tried sftp -b and sftp -B option but its not working. The... (3 Replies)
Discussion started by: shinny
3 Replies

6. Shell Programming and Scripting

Unix Shell Script to automate email alert

Hi all, I have a task on my plate which is of high priority. I need an automated email alert that checks FTP notices subdirectory on a daily basis and forwards any word files to a group of people. This word files gets created whenever there is an issue with FTP connectivity. Please help...... (1 Reply)
Discussion started by: stunnerz_84
1 Replies

7. Shell Programming and Scripting

How to automate sftp without using expect script?

How to automate sftp with out using expect script? My batch file has the password but it is not taking. Please see below. I want to use this sftp connection in a loop for pushing new files in a directory one at a time. Hence I can not use an expect script. bash-2.05$... (5 Replies)
Discussion started by: Tuxidow
5 Replies

8. Shell Programming and Scripting

How to automate sftp in a script to 'get' files.

Hi, I read a couple of forum entries about scripting sftp using the '-b' option, but in my case it still prompts for the password. Does anyone have a sample script for an sftp block to 'get' files from the remote server without prompting for a password? Both the remote and the local servers... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

9. AIX

Automate SFTP UNIX to Windows

Hi, Could you please help to solve the below issue... my requirement is automate the SFTP between UNIX and Windows server. I want to get and put some files to UNIX AIX machine(SFTP client) to Windows server(SFTP server). For that, i have generated key pair (private/public) in my AIX machine .... (6 Replies)
Discussion started by: mahiban
6 Replies

10. Shell Programming and Scripting

Automate SFTP is not working

Hi All:cool:, i tried to automate SFTP process after passwordless authendication. Stil i am getting error... Can anyone help.... ------------------- sample code below ------------------- sftp -v $mdskk@100.4.4.75 << EOF cd /data mget *.tar.gz bye EOF... (2 Replies)
Discussion started by: senthil_seera
2 Replies
Login or Register to Ask a Question