Copy the text files into router's flash through a Telnet session

Were you ever in a situation where a file that would have to be on the router was sitting on your laptop, but you couldn't store it into the router's flash across the Telnet session or through the console port?

If the file in question is a text file, and the router supports Tcl shell, danshtr documented an interesting trick: you create the file in Tclsh interpreter, cut-and-paste the text through the telnet session into a Tcl string and write the string to the file. If you want to have a more cryptic solution here it is:
  • Start tclsh;
  • Enter puts [open "flash:filename" w+] {. Do not hit the ENTER key at the end of the line
  • Copy-paste the file contents. The contents should not include unmatched curly right brackets (every curly right bracket has to be preceded by a matching curly left bracket).
  • After the file contents have been pasted, enter } and press ENTER.
  • End the tclsh session with tclquit.

21 comments:

  1. Hello everyone,I am working with a tcl script on cisco ios.
    This command has been very helpful puts [open "flash:filename" w+] {.
    however how do I use this repeatedly to append more strings into the same file without losing the previous data from the file?

    Secondly, teh command at the end says input is from a file however can I need to use a tcl variable as the input to the command. How can I do this?
    All suggestions are welcomed. Thank you in advance for all teh help.

    You can reply back to me at [email protected]
  2. If you want to reopen the file and append more data to it, use a+ instead of w+.

    See http://tmml.sourceforge.net/doc/tcl/open.html for details.

    If you want to use more than one "puts" statement, store the channel# into a variable and use that variable in puts. For example,

    set fl [open ...]
    puts $fl "string"
    puts $fl {string}
    close $fl

    See also http://tmml.sourceforge.net/doc/tcl/puts.html (examples)
  3. Thank you :)
    That was very helpfull.
  4. Hello,

    I´m doing a script as well. Every time that my script run, it add some data to the flash file that I define.
    I would like to copy the file to tftp server.

    Well, the question is, I know how to copy it, the problem is when the file already exists in TFTP, ios says that the file already exists... I doesnt know how to overwrite the file in tftp with the new data.

    Anyone knows if there are any command to do that? or any way to do that?

    Thanks in advance
  5. I'm just guessing ... Storing configurations on a TFTP server is something I do on a regular basis, and I usually overwrite existing files.

    The problem is probably in the configuration of the TFTP server, which rejects an attempt to overwrite existing files.
  6. Thank you :)
    I´m using ftp and the configuration is ok.

    Actually, my script is running. But, by the end I would like to copy a file from flash to ftp server and here is the BIG problem of my script.

    I use this command to do that:

    if [catch {cli_exec $cli1(fd) "copy flash:test ftp://10.0.0.1"} result] {
    error $result $errorInfo
    }

    Well, it runs without any kind of error, but in ftp server the file doesn't appear.
    My question is: is it possible to copy from flash to ftp, while inside tcl script running as a policy?
    Should this command works fine, no ?

    If anyone knows how I can do that copy, please help me.

    Thanks in advance
  7. Try executing the same command manually from the CLI to test if it works. It should not generate any prompts, you might have to use file prompt quiet.
  8. Hello,
    I am trying to create a tcl script in flash by using this method but it does not work,
    it does actually try to execute the tcl script instead of writing it to a file, the output I get is the following :

    Switch(tcl)#puts [open "flash:script" w+]{
    file2{

    Switch(tcl)#proc_trust_state {} {
    +>set sisb ....................
    +>........
    +>}}
    wrong # args: should be "proc name args body"
    Switch(tcl)#


    do you think there is actually a way to do it, am I missing something in the script ?
    Let me know. Thanks a lot !

    Ju
  9. In response to the Anonymous user above, you need to leave a space between the closing ']' and the '{' to avoid the error you're seeing.
  10. Hello,

    If im using tclsh, I know there is a command to read a .txt file that is storage in flash: (I mean, with that command, the flash:filename.txt file appear line by line in IOS screen. So I dont need to upload the txt file to my computer to see what is inside...)

    I can't remmember what command is it, can u help me?

    Thanks in advance,
    Corcunda
  11. sorry for the post above, I already find how to do that =)
  12. Hello,

    I want to get the running config of a remote router by using tcl. Could you please show me how since I am new to TCL.

    Any help will be highly appreciated !

    Nguyen Su Tam
  13. Brilliant sir, brilliant.
  14. Hello, I am wrote a TCL script on notepad and I am trying to copy and paste it on a router (GNS3) but shows this:

    R3(tcl)#
    R3(tcl)#R3(tcl)#and name "address" ^
    invalid command name "R3(tcl)#and" ^
    % Invalid input detected at '^' marker.

    If anyone can help I would appreciate
  15. This will not work NX-OS due to the removal of command "put" from TCLSH (unfortunately) :(
  16. Hello, i know this is an old topic. i have done these but when i try to display the txt file that i just created using "more flash:filename.txt" it returns with a command authorization failed.

    this does work with multi lined text files right? as long as the matching curly bracket is supplied at the very end of the file?
  17. Is it possible to send an ios image to multiples routers with a TCL script?
  18. Hey, great article, but do you know if something like that can be used on NX-OS too?
  19. Ditto... How can we do this on NX-OS?
  20. The following is working for me on NX-OS 7.0(6)N1(1):
    tclsh
    set MyFile [open "/bootflash/x.txt" w+]
    puts $MyFile {Line2
    Line3}
    flush $MyFile
    close $MyFile
Add comment
Sidebar