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

  • SEARCH
  • Home
  • 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 8733701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:42:19+00:00 2026-06-13T09:42:19+00:00

I am writing an expect script that will ssh into several IPs to test

  • 0

I am writing an expect script that will ssh into several IPs to test their connectivity. I would like to include a “puts” statement to write the result of each test to a file on the machine calling the script. Although I think I’m following the manual for puts to write to a file, it only writes to stdout. Please see the script below. The end result is that the file is created on the local machine, but no output is directed to it. Instead, the output goes to stdout.

#!/bin/bash

USER=user
PASSWORD=password
IPSTART=12.34.56.
OUTFILE="TEST.log"
PROMPT="#"

for IPEND in `seq 200 231`
do

expect -c "
        set timeout 3
        set chan [open $OUTFILE w]
        spawn ssh $USER@$IPSTART$IPEND
                expect -re \".*ssword.*\" {send \"$PASSWORD\n\"}

                expect {
                        -re \".*Are you sure you want to continue connecting.*\" {send \"yes\n\"; exp_continue}
                        -re \".*$PROMPT.*\$.*\"  {puts $chan \"$IPSTART$IPEND\n\"; send \"exit\n\"}
                }

        close $chan
"

done

I’m wondering if there might be an issue with the quoting, but I can’t figure it out.

For reference, this is the example from http://www.tcl.tk/man/tcl8.4/TclCmd/puts.htm

set chan [open my.log a]
set timestamp [clock format [clock seconds]]
puts $chan "$timestamp - Hello, World!"
close $chan
  • 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-06-13T09:42:20+00:00Added an answer on June 13, 2026 at 9:42 am

    You’re probable problem was that you were using double-quotes. Therefore you should escape the literal $ that will be passed to expect (ie. \$chan):

    #!/bin/bash
    
    USER=user
    PASSWORD=password
    IPSTART=12.34.56.
    OUTFILE="TEST.log"
    PROMPT="#"
    
    for IPEND in `seq 200 231`
    do
    
    expect -c "
            set timeout 3
            set chan [open $OUTFILE w]
            spawn ssh $USER@$IPSTART$IPEND
                    expect -re \".*ssword.*\" {send \"$PASSWORD\n\"}
    
                    expect {
                            -re \".*Are you sure you want to continue connecting.*\" {send \"yes\n\"; exp_continue}
                            -re \".*$PROMPT.*\$.*\"  {puts \$chan \"$IPSTART$IPEND\n\"; send \"exit\n\"}
                    }
    
            close $chan
    "
    
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a script that will copy Valgrind onto whatever shelf that we
I'm writing a script in Python 3.1.2 that logs into a site and then
I am writing a Bash script and using Expect to do sftp. Now in
I'm writing a script that saves a figure with multiple formatting styles among which
I am in the process of writing a Ruby script/app that helps me compiling
I am writing a simple expect script for accessing to a remote server without
I'm writing a PHP script that accesses two different data objects - one is
I'm currently writing a script that exports data from a list of tables and
I'm writing a php script that iterates over the Monday of each week. However
I'm writing a web app that needs to use several ansi C functions to

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.