Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6791917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:54:17+00:00 2026-05-26T17:54:17+00:00

Currently I have some code that is doing an SFTP via expect/tcl. Its something

  • 0

Currently I have some code that is doing an SFTP via expect/tcl. Its something like this:

send -i $ftpid "$cmd\r"
expect {
    -i $ftpid -re "\n5\[0-9]\[0-9] \[^bB].*\nsftp> " {
        set errorCode 149
        set errorInfo "SFTP command error on $cmd."
        return 1
    }
    -i $ftpid "452 Err.*\nsftp> " {
        set errorCode 149
        set errorInfo "SFTP 452 command error on $cmd."
        return 1
    }
    -i $ftpid "Invalid command*\nsftp> " {
        set errorCode 149
        set errorInfo "SFTP invalid command on $cmd."
        return 1
    }
    -i $ftpid -re "\n2\[0-9]\[0-9] .*\nsftp> " {
        return 0
    }
}

Occasionally I run into situations where an FTP server has return codes or messages that do not match what is already populated in the code base. Instead of modifying the core code for these what I would like to do is have an external file (ex: returncodes.tbl) where I can have a list of messages like:

552 Invalid Return*\nsftp>;<errorCode>;<errorInfo>
400 Some Error*\nsftp>;<errorCode>;<errorInfo>
...

So then it will interpret it in the expect code like:

-i $ftpid "552 Invalid Return*\nsftp>" {
    set errorCode <errorCode>
    set errorInfo "<errorInfo>"
    return 1
}
-i $ftpid "400 Some Error*\nsftp>" {
    set errorCode <errorCode>
    set errorInfo "<errorInfo>"
    return 1
}

I know how to read the external file and chop the variables up (open/read/split). However I cannot figure out how to create the loop needed within the expect statement. Was hoping someone might have an idea on how to accomplish this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:54:18+00:00Added an answer on May 26, 2026 at 5:54 pm

    I think I would approach this, by building a case on the end of your existing expect statement that can catch a pattern and parse it with a regexp. Then you just need a for loop to weed through the file and snag a match.

    You may need to play with the regexp to get it to work just right – sending \r a second time will align the expect buffer so that you can catch the error code.

    (I don’t know how you loaded the file, so right now I’m assuming it’s a flat list split by “\n” and “;”)

    expect {
         #your existing expect code here
    
        -i $ftpid -re "\[0-9]{3}.*\\*\nsftp>" {
            send -i $ftpid "\r"
            expect -i $ftpid "sftp>"
            regexp "^(.*\nsftp>)\nsftp>" $expect_out(buffer) garbage errorMsg
            set index 0
            foreach element $filelistyouloaded {
                if { $element == $errorMsg } { 
                     set errorCode $filelistyouloaded([expr $index + 1])
                     set errorInfo $filelistyouloaded([expr $index + 2])
                     return 1
                }
                incr index
            }
        }        
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have some code that will produce a crash dump when my application
I currently have some code that pulls down a list of users in a
I've been trying to implement unit testing and currently have some code that does
I have some JSF code that currently works (as shown below), and I need
Currently I have some legacy ASP.NET 2.0 code that uses the ASP Xml web
Currently I have the following code for a project that represents some probability trees
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have some Python code that works correctly when I use python.exe to run
I'm using Zend_Search_Lucene, the PHP port of Java Lucene. I currently have some code
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.