Spawn sftp and Shell commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spawn sftp and Shell commands
# 1  
Old 05-29-2013
Spawn sftp and Shell commands

Hi everyone,

I'm no killer in shell scripting, that is why I've searched and found a little script that explained how to do what I wanted to do : a FTP transfer from distant servers. I adapted it cause as such, it didn't work.

As I needed to do some very simple shell commands (erase and rename of files) after the transfer was being done, I added some personnal but very basic sript lines.





So the result is the following :
  1. the adapted SFTP lines in the spawn do the job
  2. but the renaming and erasing parts don't.
See below the code :
Code:
#!/usr/bin/expect
cd /home/ess_tms/temp/JTMSF/
set timeout -1    
 
set USER user
set PASS etl
set HOST mon_serveur
set INF_PATH_OUT /toto/data/mutualise/out/tms/temp_files/
 
spawn sftp $USER@$HOST
 
expect password:
send "$PASS\r"
 
expect sftp>
send "cd $INF_PATH_OUT/JTMSF\r" 
 
expect sftp>
send "get *.txt\r"
 
expect sftp>
send "exit\r"
 
rm -f /home/rep/temp/JTMSF/*.gz
mv /home/rep/temp/JTMSF/toto.txt /home/rep/temp/JTMSF/titi.txt

and I get this error message :
Quote:
sftp> invalid command name "rm"
while executing
"rm -f /home/ess_tms/temp/JTMSF/*.gz"
(file "./test2.ksh" line 24)
The way I understand this error message is that, somehow, it tells me I'm not in a shell command window, I'm still in a SFTP kind of session. Which seems to be different from a shell command session (really Smilie)

This is why it was expecting SFTP or spawn commands and cannot interpete shell orders.


To make it work, I'd need to get out from this SFTP/Spawn command session, but :
  1. I don't know if my analysis is correct
  2. if it is, I don't know how to do so
Is there one of you who could help me on that little crappy script of mine ?

Thanks

Last edited by mederik; 05-29-2013 at 11:36 AM..
# 2  
Old 05-30-2013
Check out this example, I've added your 'rm' and 'mv' (i.e. exec)statements after the get of file:

Code:
#!/usr/bin/expect --
# Script: sftp_file_from.tcl
# Description: Utility to SFTP a file from(i.e. get) a host(i.e. source)
# usage: sftp_file_from.tcl source_host user pass source_directory local_directory file_name_to_ftp


# Set named parameters
set source_host              [lindex $argv 0]
set sftp_user                [lindex $argv 1]
set sftp_pass                [lindex $argv 2]
set source_directory         [lindex $argv 3]
set local_directory          [lindex $argv 4]
set file_name                [lindex $argv 5]
set sftp_prompt "sftp>?"
set timeout 300

#exp_internal 1;

# Procedure to connect to SFTP server
proc connect { sftp_pass } {
  variable sftp_prompt
  variable source_host
  expect -re "Are you sure you want to continue.*" { exp_send "YES\r"; exp_continue } \
         -re "(Password|password).*:.*"            { exp_send "$sftp_pass\r"; exp_continue } \
         -re $sftp_prompt                          { return 0 } \
         eof                                       { puts "***Error connecting to($source_host)."; return 1 }
         timeout                                   { puts "***Error connecting to($source_host)."; return 1 }
  }

# Procedure to send meesage denoting error, then quit SFTP, then exit with status denoting error.
proc abort { msg } {
  puts "$msg";
  exp_send "quit\r";
  exit 1;
  }

# Connect to the SFTP server
spawn sftp $sftp_user@$source_host
set connect_results [connect $sftp_pass]

# If successful connection, continue, else exit denoting error occured.
if { $connect_results == 0 } {
  # Change to source directory on source server.
  exp_send "cd $source_directory\r"
    expect "No such file or directory" { abort "\n**Error changing to directory($source_directory) on source server." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n**Error changing to directory($source_directory) on source server." }

  # Change local directory.
  exp_send "lcd $local_directory\r"
    expect "No such file or directory" { abort "\n**Error changing to directory($local_directory) on local host." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n**Error changing to directory($local_directory) on local host." }

  # Get file from source host
  set timeout 1800
  exp_send "get $file_name\r"
    expect "not found."                { abort "\n***Error transfering file($file_name) FROM: $source_host)." } \
           -re $sftp_prompt            {} \
           timeout { abort "\n***Error transfering file($file_name) FROM: $source_host)." }

  # QUIT!!
  exp_send "quit\r"
  
  # Remove and move files
  exec rm -f /home/rep/temp/JTMSF/*.gz
  exec mv /home/rep/temp/JTMSF/toto.txt /home/rep/temp/JTMSF/titi.txt

  # Successful session so exit with zero status
  exit 0
}
# Error connecting to SFTP server so exit with non-zero status
exit 1

# 3  
Old 05-30-2013
Could you create a key to allow password-less authentication? if you can, then expect can be removed and you can use the -b flag of sftp to run the commands you want.
Code:
sftp -b my.cmds target-server

It might just be that the sftp does not exist. I might have missed it, but I don't see a quit in your code.


Does that lead you anywhere?



Robin
Liverpool/Blackburn
Uk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Help needed to Spawn Shell on Python and Continue Execution

def gob(url): print "\n\t Running gobuster on target." params = " -e -s '307,200,204,301,302' -t 20 -u " + url + " >> /tmp/%s/gobuster.txt" % (ip) os.system("xterm -e bash -c "tail -f /tmp/%/gobuster.txt"") for i in bflist: dirbf = "gobuster -w " + i... (3 Replies)
Discussion started by: alvinoo
3 Replies

2. Shell Programming and Scripting

Sftp with spawn - can't execute

Hi All, I have below script which is just trying to sftp transfer a file to another server with 'expect' option. I have expect installed on the instance. Below is the script: #!/usr/bin/expect spawn sftp oracle@<HOST> expect "password:" send "<mypassword>\n" expect "sftp>"... (6 Replies)
Discussion started by: festerbg
6 Replies

3. Shell Programming and Scripting

Help with spawn.. newbie to shell

Hi, I have a problem with the spawn execution with expect.. i have done the code for expect in a separate file and i am calling the this execution from the bash script.. as given below.. -bash-4.1$ cat main.sh #!/usr/bin/bash ./spawn.exp ========================== -bash-4.1$ cat... (2 Replies)
Discussion started by: satishkumar432
2 Replies

4. Shell Programming and Scripting

Sftp commands not working in shell script

hi, i am having 2 linux boxes as source and 1 linux box as destination.i want to create a shell script containing code to transfer a csv file from either of the 2 linux boxes (file will be present in just one box, i need to check both the boxes to see which box has the csv file) to 3rd linux box... (1 Reply)
Discussion started by: linuxlearn2013
1 Replies

5. Shell Programming and Scripting

Using expect command, spawn will not start sftp process

Hi all, I have a script that runs sftp with expect so I can login and send a file in a cronjob. I've installed this on a couple other servers and it has been fine. However, this time on this machine, it seems to be giving me an issue. It won't move past the spawn sftp command and return a... (3 Replies)
Discussion started by: ltyrrell
3 Replies

6. UNIX for Dummies Questions & Answers

Help on commands in sftp using BatchFile

Hi, The script didn't continue as "20130109" folder is already created on the destination server. Please help. Entry in script: cat > $filebatch << __EOF__ mkdir $current_date mkdir $current_date/$fpdomain cd $current_date/$fpdomain ls -l __EOF__ Error: sftp -b... (12 Replies)
Discussion started by: chococrunch6
12 Replies

7. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

8. Shell Programming and Scripting

shell script for getting pid of spawn processes from shell

Hi, I am new this forum. I request you peoples help in understanding and finding some solution to my problem. Here it goes: I need to perform this set of actions by writing a shell script. I need to read a config file for the bunch of processes to execute. I need to fecth the pid of... (4 Replies)
Discussion started by: sachin4sachi
4 Replies

9. Shell Programming and Scripting

execute shell commands with in sftp

Hi All, Please let me know how do I execute some of the shell commands like cat, find ,grep within sftp. Any help in this regard would be greatly appreciated. Thanks, (5 Replies)
Discussion started by: tommy1
5 Replies

10. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question