11-15-2010 04:46 AM
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
Solved! Go to Solution.
11-15-2010 02:22 PM
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
11-15-2010 11:15 PM
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"]
11-17-2010 02:40 PM
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"
}
11-25-2010 06:17 PM
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"
11-26-2010 01:08 AM
11-15-2010 02:22 PM
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
11-15-2010 10:49 PM
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
11-15-2010 11:15 PM
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"]
11-16-2010 11:22 PM
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
11-17-2010 02:40 PM
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"
}
11-25-2010 07:40 AM
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
11-25-2010 11:49 AM
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
}
}
11-25-2010 06:17 PM
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"
11-26-2010 12:53 AM
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.
11-26-2010 01:08 AM
11-02-2012 11:51 AM
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
11-03-2012 11:04 AM
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.
11-05-2012 04:06 AM
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.
11-05-2012 04:52 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide