Sponsored Content
Top Forums Shell Programming and Scripting Spawn sftp and Shell commands Post 302814633 by spacebar on Thursday 30th of May 2013 12:40:43 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
lindex(n)						       Tcl Built-In Commands							 lindex(n)

__________________________________________________________________________________________________________________________________________________

NAME
lindex - Retrieve an element from a list SYNOPSIS
lindex list ?index...? _________________________________________________________________ DESCRIPTION
The lindex command accepts a parameter, list, which it treats as a Tcl list. It also accepts zero or more indices into the list. The indices may be presented either consecutively on the command line, or grouped in a Tcl list and presented as a single argument. If no indices are presented, the command takes the form: lindex list or lindex list {} In this case, the return value of lindex is simply the value of the list parameter. When presented with a single index, the lindex command treats list as a Tcl list and returns the index'th element from it (0 refers to the first element of the list). In extracting the element, lindex observes the same rules concerning braces and quotes and backslashes as the Tcl command interpreter; however, variable substitution and command substitution do not occur. If index is negative or greater than or equal to the number of elements in value, then an empty string is returned. The interpretation of each simple index value is the same as | for the command string index, supporting simple index arithmetic and indices relative to the end of the list. If additional index arguments are supplied, then each argument is used in turn to select an element from the previous indexing operation, allowing the script to select elements from sublists. The command, lindex $a 1 2 3 or lindex $a {1 2 3} is synonymous with lindex [lindex [lindex $a 1] 2] 3 EXAMPLES
lindex {a b c} -> a b c lindex {a b c} {} -> a b c lindex {a b c} 0 -> a lindex {a b c} 2 -> c lindex {a b c} end -> c lindex {a b c} end-1 -> b lindex {{a b c} {d e f} {g h i}} 2 1 -> h lindex {{a b c} {d e f} {g h i}} {2 1} -> h lindex {{{a b} {c d}} {{e f} {g h}}} 1 1 0 -> g lindex {{{a b} {c d}} {{e f} {g h}}} {1 1 0} -> g SEE ALSO
list(n), lappend(n), linsert(n), llength(n), lsearch(n), lset(n), lsort(n), lrange(n), lreplace(n), string(n) | KEYWORDS
element, index, list Tcl 8.4 lindex(n)
All times are GMT -4. The time now is 11:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy