cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
9012
Views
0
Helpful
22
Replies
vaba
Beginner

EEM copy multiple file from flash to ftp

Hi,

I am trying to copy multiple files from flash to a FTP server.  Is it possible to copy multiple files using one statement?  For example:

First i type

dir flash: | i file
166  -rw-         73986              Oct 7 2010 12:25:36 +03:00  file1.817
167  -rw-         73265              Oct 7 2010 13:26:08 +03:00  file2.861
168  -rw-         204278            Oct 8 2010 13:38:06 +03:00  file3.950
 

And i need to copy all file begin with name "file" to ftp or tftp server.

copy flash:/file1.817 ftp://username:passwd@10.10.10.1/
copy flash:/file2.861 ftp://username:passwd@10.10.10.1/
copy flash:/file3.950 ftp://username:passwd@10.10.10.1/

Is this possible to do this with EEM or TCL script automatically

Can you give me some examples?

Thanks in advanced

5 ACCEPTED SOLUTIONS

Accepted Solutions
yjdabear
Collaborator

I think the TCL script "tm_ca_backup.tcl" in the following thread only needs minor modifications to suit your requirement of file copying based on wildcard names:

https://supportforums.cisco.com/thread/176257?decorator=print&displayFullThread=true

View solution in original post

Vaba,

I still think that script is a prime candidate.

The "search for “file” on flash" requirement is fulfilled by the following two features in that TCL script:

# modify the cron to schedule your "search" as often as you want

::cisco::eem::event_register_timer cron cron_entry "0 3 * * 0-6"

# change the "*.cnm" part to match "all file begin with name "file""; it's glob, sorta like regex

set fileList [glob -directory flash: -nocomplain "*.cnm"]

View solution in original post

For 1, you can simply wrap an "if/else" conditional around the existing ftp code, as seen below (untested).

For 2, just add the matching "else" clause to the existing "if" that does the FTP. However, I'd rather not delete anything unless one really needs the flash space. For the "If returned some error – then after some time to execute script again", I'd choose to do nothing--just let the next cron scheduled run take care of it.

set ftpServer "ftp-server-addr"

if {[regexp "!" [exec "ping $i"]]} {
     puts "ping $i success"

     set fileList [glob -directory flash: -nocomplain "file*"]
          foreach file $fileList {
               if [catch {cli_exec $cli1(fd) "copy $file ftp://$ftpServer/"} _cli_result] {
                    error $_cli_result $errorInfo
               } else {

                    puts "deleting $file"

                    cli_exec $cli1(fd) "del flash:$file"

               }

      }

} else {
     puts "ping $i failed"

}

View solution in original post

Joe Clarke
Hall of Fame Cisco Employee

You can use ci_read_pattern instead to implement interactive commands.  For example:

cli_write $cli1(fd) "del fash:$file"

set output [cli_read_pattern $cli1(fd) "Delete"]

cli_write $cli1(fd) "\r"

set output [cli_read_pattern $cli1(fd) "Delete"]

cli_exec $cli1(fd) "\r"

View solution in original post

Joe Clarke
Hall of Fame Cisco Employee

Try this version.

View solution in original post

22 REPLIES 22
yjdabear
Collaborator

I think the TCL script "tm_ca_backup.tcl" in the following thread only needs minor modifications to suit your requirement of file copying based on wildcard names:

https://supportforums.cisco.com/thread/176257?decorator=print&displayFullThread=true

View solution in original post

Thank for respond yjdabear,

I read your post and it is very helpful, but I need a script that can search for “file” on flash – based on regular expression or something else. If on flash is file with contain name “file” then execute this script and copy to ftp all file.
I need this procedure for backup /copy/ all missed CDR (call detail records) file stored on flash when restored connection to FTP. This file every time is with different names.

Thanks in advance

Vaba,

I still think that script is a prime candidate.

The "search for “file” on flash" requirement is fulfilled by the following two features in that TCL script:

# modify the cron to schedule your "search" as often as you want

::cisco::eem::event_register_timer cron cron_entry "0 3 * * 0-6"

# change the "*.cnm" part to match "all file begin with name "file""; it's glob, sorta like regex

set fileList [glob -directory flash: -nocomplain "*.cnm"]

View solution in original post

Hi yjdabear,

I modify this script and now everything is working just fine – great job, thank again.

I have another questions, because I have no experience with TCL :

1.      Is it possible to tracking FTP server? When ftp server go up then execute this script

2.      Is it possible check “copy flash: ftp” status and if no returned error to delete this files from flash. If returned some error – then after some time to execute script again.

Thanks in advanced

For 1, you can simply wrap an "if/else" conditional around the existing ftp code, as seen below (untested).

For 2, just add the matching "else" clause to the existing "if" that does the FTP. However, I'd rather not delete anything unless one really needs the flash space. For the "If returned some error – then after some time to execute script again", I'd choose to do nothing--just let the next cron scheduled run take care of it.

set ftpServer "ftp-server-addr"

if {[regexp "!" [exec "ping $i"]]} {
     puts "ping $i success"

     set fileList [glob -directory flash: -nocomplain "file*"]
          foreach file $fileList {
               if [catch {cli_exec $cli1(fd) "copy $file ftp://$ftpServer/"} _cli_result] {
                    error $_cli_result $errorInfo
               } else {

                    puts "deleting $file"

                    cli_exec $cli1(fd) "del flash:$file"

               }

      }

} else {
     puts "ping $i failed"

}

View solution in original post

Hi yjdabear,

We are testing a script

and when need to delete file from flash

          } else {

                    puts "deleting $file"

                    cli_exec $cli1(fd) "del flash:$file"

               }

we аре receiving confirmation massage:

#delete flash:/file_1                

#Delete flash:/ file_1? [confirm]

And question is how to confirm?

I read for command “typeahead "\r"”, but I do not understand this command. Can you help us?

Thanks in advanced

I read a comment by Joe that "typeahead" shouldn't be used outside  of tclsh. I assume that excludes EEM tcl script as well. In that case, "file prompt quiet" may do the trick, though I'm not sure it works with "del" operation on IOS or not.

} else {
        puts "deleting $file"


        if [catch {cli_exec $cli1(fd) "enable"} result] {
            error $result $errorInfo
        }
       
        if [catch {cli_exec $cli1(fd) "config t"} result] {
            error $result $errorInfo
        }

        if [catch {cli_exec $cli1(fd) "file prompt quiet"} result] {
            error $result $errorInfo
        }

        if [catch {cli_exec $cli1(fd) "end"} result] {
            error $result $errorInfo
        }

        if [catch {cli_exec $cli1(fd) "cli_exec $cli1(fd) "del flash:$file"} result] {
            error $result $errorInfo
        }

        if [catch {cli_exec $cli1(fd) "config t"} result] {
            error $result $errorInfo
        }

        if [catch {cli_exec $cli1(fd) "no file prompt quiet"} result] {
            error $result $errorInfo
        }

        if [catch {cli_exec $cli1(fd) "end"} result] {
            error $result $errorInfo
        }
}

Joe Clarke
Hall of Fame Cisco Employee

You can use ci_read_pattern instead to implement interactive commands.  For example:

cli_write $cli1(fd) "del fash:$file"

set output [cli_read_pattern $cli1(fd) "Delete"]

cli_write $cli1(fd) "\r"

set output [cli_read_pattern $cli1(fd) "Delete"]

cli_exec $cli1(fd) "\r"

View solution in original post

Hi all,

Thanks you for responds.

I am testing now with last proposal from Joseph Clarke and this is worked fine.

But now I see that, when ftp connection Is dropped after sending several files from router to server I receive error:

“%Error opening ftp://*****:*****@192.168.235.168/cdr.871 (Undefined error)” 

and then all other files are deleted from flash.

And my question is whether it can do so to stop deleting other files or stop script

I am attaching my script.

Thanks in advanced.

Joe Clarke
Hall of Fame Cisco Employee

Try this version.

View solution in original post

Joseph, I really apreciate your job, I just only wanna ask about a issue that I found with your script, I  modified your script to the following version, but i found a issue. When I stop the ftp, the next script run, do not copy the files but delete this one.

if {[regexp "!!!!" [cli_exec $cli1(fd) "ping $ftpServer"]]} {

     puts "PING $ftpServer SUCCESS"

    if {[regexp "Open" [cli_exec $cli1(fd) "telnet $ftpServer 21"]]} {

                puts "FTP SERVER $ftpServer IS OPEN"

         set fileList [glob -directory flash: -nocomplain "cdr.*"]

              foreach file $fileList {

                   if { [catch {cli_exec $cli1(fd) "copy $file ftp://$ftpServer/cdr/"} _cli_result] || [regexp {Error opening ftp.*Undefined error} $_cli_result] } {

                        error $_cli_result $errorInfo

               } else {

                    puts "DELETING $file"

                    cli_write $cli1(fd) "delete $file"

                    set output [cli_read_pattern $cli1(fd) "Delete"]

                    cli_exec $cli1(fd) "\r"

                }

            }

        } else {

         puts "FTP SERVER $ftpServer IS CLOSED"     

        } 

} else {

     puts "PING $ftpServer UNSUCCESSFUL"

}

Regards

Joe Clarke
Hall of Fame Cisco Employee

I don't understand what you're doing.  How do you "stop the FTP?"  It would help to get a step-by-step set of instructions as to what you're doing, what errors you see, and what happens when you try the same transfer manually.

I have a vsftp running on a linux machine the issue happen when I stop this one with a simple "/etc/init.d/vsftp stop", The script delete the files.

Joseph maybe this will clarify the issue, when I stop the server, the next time that the script run and detect a ping successfully, but a telnet closed, this delete the files, by the way the at the second run this work fine.

This is the syslog on cisco router.