Sophie

Sophie

distrib > Fedora > 16 > i386 > by-pkgid > 47ec2bdcad83b6af68ff90e7374e5cf6 > files > 36

conman-0.2.7-0.fc16.i686.rpm

#!/usr/bin/expect --
###############################################################################
# $Id: shell_cmd_n.exp 500 2005-02-10 02:19:46Z dun $
#   by Chris Dunlap <cdunlap@llnl.gov>
###############################################################################
# USAGE:
#   shell_cmd_n.exp <console(s)...> <command>
#
# DESCRIPTION:
#   This script takes a console name followed by a single-argument command.
#   The console "name" argument(s) can contain conman options such as "-j"
#   (console-sharing) or "-f" (console-stealing).  The "command" argument
#   can contain any number of commands as long as they are all enclosed
#   within quotes such that the shell interprets it as a single argument.
#
#   A connection is made to each of the specified consoles, after which a
#   single carriage-return is sent.  If a recognizable shell prompt is
#   returned, then the "command" argument is sent to the shell.
#
#   Each line output by the shell in response to the command (ie, up to the
#   next recognizable shell prompt) is prepended with the console name.
#   This allows data being returned from multiple concurrent consoles to be
#   demux'd with a stable sort such as: "sort -s -t: -k1,1".
#
# NOTES:
#   The shell prompt recognition cannot account for all the diverse types
#   of prompts seen in the wild (via the PS1 environment variable),
#   but it tries really hard.
###############################################################################

set env(PATH) "/bin:/usr/bin:/usr/local/bin"
source /usr/share/conman/conman.exp
exp_internal 0
log_user 0

# The number of concurrent console session on which the cmd is run.
set fanout 64

# Additional options to pass to the conman command.  Typical options
#   would include '-f' for console-stealing and '-j' for console-sharing.
set opts ""

# If set to 1, console output is echoed back; if set to 0, nothing is echoed.
set echo 1

# The amount of time to wait for console input before giving up.
set timeout 3

proc do_shell_cmd {spawn_id output_id console cmd {echo 1} {tmout 3}} {

  set expect_out(buffer) ""
  set cmdstr "[join $cmd]"

  exp_send "\r"
  set timeout $tmout
  expect {
    -re "^\[^\r]*(%|#|\\\$|]|\[^>]>) \$" {
      set prompt $expect_out(0,string)
    }
    -gl "\n" {
      exp_continue -continue_timer
    }
    default {
      exp_send -i $output_id "ERROR: No shell prompt.\n"; return 0
    }
  }

  exp_send -- "$cmdstr\r"
  set timeout $tmout
  expect {
    -re "^ *$cmdstr\r+\n" {
      exp_continue -continue_timer
    }
    -re "^$prompt\$" {
      ;
    }
    -re "(^\[^\r]*)\r+\n" {
      if {$echo} {exp_send -i $output_id "$expect_out(1,string)\n"}
      exp_continue -continue_timer
    }
    default {
      exp_send -i $output_id "ERROR: Timed-out.\n"; return 0
    }
  }
  return 1
}

set consoles [lrange $argv 0 [expr [llength $argv] - 1]]
set command [lindex $argv end]
conman_run $fanout "$opts $consoles" do_shell_cmd $command $echo $timeout