Automate sftp process using script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Automate sftp process using script
# 1  
Old 07-26-2013
Tools 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:
Code:
sftp [username]@[servername] >[listFile directory] << EOF
>cd [data location]
>ls -l
>EOF 
>Connecting to [servername]
[username]@servername password:

After I typed in the password, it will execute the above command, and after I check, I got the result correctly

But when I put it inside a script:

Code:
! /bin/csh
#!/usr/local/bin/expect -f

set listFile = /cluster/pehdm/root_tools/subcon/SFTP_log

spawn sftp [username]@[servername] >[listFile directory] << EOF
expect ">"
send "cd pe_ase/data/vax\n";
expect ">"
send "ls -l\n";
expect ">"
send "EOF\n";
expect "password:"
send "[my password]\n";
send_user "\ndone.\n";
exit 0

But it seems that the "spawn sftp ...." need to have some more pointer like -b, or -s. I just want it run normally like when I type in manually. Can any one please fix the peice of code for me, thanks
# 2  
Old 07-26-2013
if you want t o use expect then create a separate file for your sftp program with no extension. for example:
Code:
sftp-script

Code:
#!/usr/bin/expect
spawn /usr/bin/sftp [username]@[servername]
expect "[username]@[servername]'s password:"
send "password\r"
expect ">"
send "cd pe_ase/data/vax\r";
expect ">"
send "ls -l\r";
expect ">"
send "bye\r"
expect eof

use \r instead of \n and the 1st line of the sftp script should be
Code:
#!/usr/bin/expect

while calling this sftp script simply give the script name using relative or absolute path.
Code:
./sftp-script

# 3  
Old 07-26-2013
Thanks for your post, but I need to get the result of the command "ls -l" to be stored in a file in a local directory (which is the listFile). And I can't find a way to do other than the command:

Code:
sftp [username]@[servername] >[listFile directory] << EOF

Please guide me if there is a way Smilie
# 4  
Old 07-26-2013
while calling the sftp script you can redirect the output of sftp to a local file and then grep the lines starting with -

Code:
./sftp-script > ls_output

cat ls_output | grep "^-" > listFile

if you are using the sftp code in your shell script

Code:
sftp [username]@[servername]  << EOF > listFile

[CODE]
redirection should come after <<EOF
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Help with writing Shell Script to automate process using multiple commands

Hello! Need help to write a Linux script that can be run from windows using command/Cygwin/any other way. I am new to scripting, actually i am trying to automate server health check like free disk space, memory along with few services status, if any services is not running then start services ,... (7 Replies)
Discussion started by: Sayed Ibrahim
7 Replies

2. Shell Programming and Scripting

Script to automate recovery process

Hello All! First post... I am working on a script that is used to recover a crashed drive from an rsync backup. I'm down to the place where I need to create all of the directories in /mnt where I will then mount each of the volumes and begin the restore process to each volume... I have... (3 Replies)
Discussion started by: RogerBaran
3 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Need help in creating a shell script to automate svnstats process

Hi, I am trying to create a shell script to automate the following process of getting svn stats:- Step1:- cd to checkout location. Note that the checked code have multiple modules in respective folders Step2:- Execute this command inside each module:- svn log -v --xml >... (0 Replies)
Discussion started by: d8011
0 Replies

6. Shell Programming and Scripting

Bash script to Automate the Virtual Host creation process!!

Hi all, This is my sample code in /etc/httpd/conf.d/applications.conf file currently we are creating subdomain mannually for every new subdomain. I want to automate this process througs bash script , how its possible. <VirtualHost *:80> ServerName google.com ServerAlias google.com... (5 Replies)
Discussion started by: anishkumarv
5 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. Shell Programming and Scripting

Error in script to automate the daily monitoring process of UNIX server and it's proc

hi friends, I am trying to automate the daily monitoring process of UNIX server and it's processes. the script are below i executed the above script using ksh -x monitortest1.sh in root login . It shows error at some lines . 1. i logged in using root ,but it... (8 Replies)
Discussion started by: rdhaprakasam
8 Replies

10. Shell Programming and Scripting

automate sftp using unix script

Hi All, I need to write a UNIX script that automates the sftp process. I should be able to do a sftp to a secure box and get a file from there. I am having a problem doing this because no matter what I do, when I run my script, I get a prompt at command line asking for a password. How could I... (34 Replies)
Discussion started by: priyamurthy2005
34 Replies
Login or Register to Ask a Question