So I am attempting to use expect to automate backing up the running config of some switches on our network. The problem that I'm running into is its changing a part of my send commands and I dont know how to fix it.

For example I'm using whats below.


spawn /usr/bin/plink -ssh x.x.x.x

expect -exact "User Name:"

send "USERNAME\r"

expect -exact "Password:"

send "PASSWORD\r"

send "copy running-config flash:\\startup-config"

expect "Overwrite file [startup-config] ?[Yes/Press any key for no]"

send "y\r"

expect "Copy succeeded"

send "exit/r"

expect "eof"

but it ends up messing up on "flash:\\startup-config" instead sending "flash:\startup-config" and I'm at a loss as to what this is called (in order to search how to fix it).

Is anyone able to help with pointing me in the right direction?

  • Just tried that, it still only sends "flash:\startup-config" – GTerry Mar 23 '16 at 16:39
  • 1
    These things, you kind of need to experiment with escaping the backslashes: Try send "... flash:\\\\startup..." and if that fails, try 8 backslashes – glenn jackman Mar 23 '16 at 18:39
  • or send {copy running-config flash:\\startup-config} – Johannes Kuhn Mar 23 '16 at 18:43
  • Is that what term I should have searched? Escaped characters? Also I tried the initial suggestion of four and it didnt wish to work, before attempting more backslashes I tried {copy running-config.....} and it ended up working. I'm sure the other would have worked as well, not to take anything away from your answer. Id like to give upvotes where they are due since both helped, would you be able to post as an answer? I cant seem to vote on comments. – GTerry Mar 23 '16 at 19:27
  • @GTerry, feel free to add your own answer that described what you did to solve the problem. You can then accept your own answer. – glenn jackman Mar 23 '16 at 21:13

Tcl uses the \ character to escape things, like you used \r for the newline. If you want to send a single \ you either have to escape it with an other \, so it becomes \\ or you escape the string with braces {} which will prevent such special escapes. Your options are therefore:

send "copy running-config flash:\\\\startup-config"


send {copy running-config flash:\\startup-config}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.