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 7618359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:23:01+00:00 2026-05-31T03:23:01+00:00

The target of the following simple expect script is to get the hostname name

  • 0

The target of the following simple expect script is to get the hostname name on the remote machine

Sometimes expect script fail to perform ssh to $IP_ADDRESS ( because remote machine not active , etc )

so in this case the expect script will break after 10 second (timeout 10) , this is OK but……

There are two options

  1. Expect script perform ssh successfully , and performed the command hostname on the remote machine
  2. Expect script break because timeout was 10 seconds

On both cases expect will exit

  • in case of ssh successfully expect will break after 0.5-1 second but in case of bad ssh then it will break after 10 seconds

but I don’t know if expect script perform ssh successfully or not?

is it possible to identify timeout process ? or to verify that expect ended because timeout?

Remark my Linux machine version – red-hat 5.1

Expect script

 [TestLinux]# get_host_name_on_remote_machine=`cat << EOF
  > set timeout 10
  > spawn  ssh   $IP_ADDRESS
  >            expect {
  >                      ")?"   { send "yes\r"  ; exp_continue  }
  > 
  >                      word:  {send $PASS\r}
  >                   }
  > expect >  {send "hostname\r"}
  > expect >    {send exit\r}
  > expect eof
  > EOF`

Example in case we not have connection to the remote host

 [TestLinux]# expect -c  "$get_host_name_on_remote_machine"
 spawn ssh 10.17.180.23
 [TestLinux]# echo $?
 0
  • 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-31T03:23:03+00:00Added an answer on May 31, 2026 at 3:23 am

    To do sensible things on timeout, you need to tell expect what should happen:

    set timeout 10
    expect {
        ")?"     { send "yes\r"  ; exp_continue  }
        "word:"  { send "$PASS\r"                }
        timeout  { puts "timed out during login"; exit 1 }
    }
    set timeout -1   ; # Infinite...
    expect ">"   { send "hostname\r"             }
    expect ">"   { send "exit\r"                 }
    expect eof
    exit
    

    Notice above how I use exit 1 when I hit an error. Your shell will be able to pick that up through $?, etc. (Without the 1 argument, the exit command will cause the script to terminate “successfully”; the same happens if you drop off the bottom of the script.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very, very simple any build script: <project name=quiz_jar default=all> <target name=all>
I have the following target: <target name=promptforchoice> <input addproperty=choice> Copy the file?. [Y, n]
I have the following build.xml file setup in phpUnderControl. <target name=phpunit> <exec executable=phpunit dir=${basedir}/httpdocs
I started a simple makefile for my project (MS-windows target, MinGW) with the following
I have the following simple code: <Window x:Class=WpfApplication3.MainWindow x:Name=WindowInst …> <local:UserControl1/> </Window> <UserControl x:Class=WpfApplication3.UserControl1
I would like to ask probably simple question. Consider following php page: <p>Name of
We've got a MsBuild.proj file which contains the following section (simplified): <Target Name=WEB DependsOnTargets=CleanResults;UpdateAssemblyInfo;Services;Business>
I have the following PHONY target in Makefile install: echo /usr/bin/shelldecrypt must be writable
I'm trying to input a JSON file with the following format: [{target: stats.server14, datapoints:
I have the following in a AfterGet target in TFS. <ItemGroup> <AssemblyInfoFiles Include=$(SolutionRoot)\**\*assemblyinfo.cs />

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.