File: shell_cmd_n.exp

package info (click to toggle)
conman 0.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,424 kB
  • sloc: ansic: 11,173; sh: 4,072; exp: 2,132; makefile: 153; perl: 122
file content (90 lines) | stat: -rwxr-xr-x 2,969 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/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/lib/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