Create text file in flash over a telnet session
From CT3
Copies terminal session input to the specified file. You can use this script to copy text files to a router with no connectivity to an external file server.
Contents |
Installation
Download the source file into flash:storeFile.tcl. Configure alias exec store tclsh flash:storeFile.tcl . Invoke with store filename .
To repeatedly change a single file, include the file name in the alias, for example configure alias exec sf tclsh flash:storeFile.tcl flash:myfile.tcl force.
If your router has no external connectivity, use this method to copy the tcl script into flash.
Usage guidelines
Usage: store filename [force]
Command line parameters:
- filename: the name of the file to create. You can create files in flash, NVRAM or any other local file system;
- force: overwrite existing file.
Author
Ivan Pepelnjak, © 2008 NIL Data Communications
Source code
#
# Copyright (c) 2008 NIL Data Communications
# All rights reserved.
#
# by: Ivan Pepelnjak, NIL Data Communications
# title: Copies file from STDIN to flash
# name: storeFile.tcl
# desc: The script copies text content from STDIN to specified file
# params: fileName - name of the file to be written
# force - optional, force the overwrite
#
# ios config:
#
# * download the file into flash:storeFile.tcl
# * configure alias exec store tclsh flash:storeFile.tcl
#
# * optionally configure alias for a single file
# configure alias exec sf tclsh flash:storeFile.tcl flash:myfile.tcl force
#
# invoke with store fileName [force]
#
set fileName [lindex $argv 0]
set force [string equal [lindex $argv 1] "force"]
if { [file exists $fileName] == 1 && $force == 0 } {
puts -nonewline "File $fileName exists, overwrite? "; flush stdout
if { ! [string equal [string tolower [string index [gets stdin] 0]] "y"] } {
puts "Aborted"; return
}
}
fconfigure stdin -blocking 1 -buffering full
puts "Enter content for $fileName, finish with ctrl/C"
set content [read stdin]
set channel [open "$fileName" w+]
puts $channel $content
close $channel
puts "File $fileName successfully written"
Categories: Tcl | Tclsh
BlogMarks
del.icio.us
digg
Newsvine
reddit
Slashdot