File: target.exp

package info (click to toggle)
opa-ff 10.10.3.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 17,372 kB
  • sloc: ansic: 189,318; sh: 25,941; perl: 15,145; exp: 8,415; makefile: 5,315; xml: 189; cpp: 93; tcl: 50; python: 44
file content (2390 lines) | stat: -rw-r--r-- 66,171 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
# BEGIN_ICS_COPYRIGHT8 ****************************************
#
# Copyright (c) 2015-2020, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Intel Corporation nor the names of its contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# END_ICS_COPYRIGHT8   ****************************************

# [ICS VERSION STRING: unknown]

# This is an expect (tcl) library of procedures to aid testing
# These functions provide support for remote login and command execution
# against a test target host

## tcl procedures to support testing:
## =============================================

# pattern to match for unix prompts (both local and on target)
global target_unix_prompt
set target_unix_prompt {[$#>][\033 ]}

# pattern to match for chassis prompts
# since it starts with a -, -gl must be passed to expect
global target_chassis_prompt
set target_chassis_prompt {-> }

# prompt which comes after login when connect to support shell
global target_chassis_shell_prompt
set target_chassis_shell_prompt {shell-> }

# limit on login retries
global target_chassis_retry_limit
set target_chassis_retry_limit 3

# is the present chassis session a support shell session?
global target_chassis_shell
set target_chassis_shell 0
# save area for main card when in a rlogin shell
global target_chassis_shell_save
set target_chassis_shell_save 0

global os_type
set os_type [ exec uname -s ]

proc chassis_strip_slots { chassis } {
##
## chassis_strip_slots
## -------------------
## strip slot specifications from chassis
##
## Usage:
##	chassis_strip_slots chassis
## Arguments:
##	chassis - chassis name of the form:   name[:slot,slot...]
## Returns:
##	resulting chassis name
##	-code error on failure

	if { [ regexp {.*\[(.*)\].*:.*} "$chassis" full ret] } {
		# [chassis]:slot format
		return "$ret"
	} elseif { [ regexp {.*\[(.*)\].*} "$chassis" full ret] } {
		# [chassis] format
		return "$ret"
	} elseif { [ regexp {.*:.*:.*} "$chassis" full ] } {
		# ipv6 without [] nor slot
		return "$chassis"
	} elseif { [regexp {([^:]+):.*} "$chassis" full ret] } {
		# chassis:slot format
		return "$ret"
	} else {
		# chassis without [] nor slot
		return "$chassis"
	}
}

proc chassis_get_slots { chassis } {
##
## chassis_get_slots
## -------------------
## get slot specifications from chassis
##
## Usage:
##	chassis_get_slots chassis
## Arguments:
##	chassis - chassis name of the form:   name[:slot,slot...]
## Returns:
##	resulting list of slots as a TCL list : slot nos after colon
##	if no slots, returns "all"
##	-code error on failure

	if { [ regexp {.*\[.*\].*:(.*)} "$chassis" full slots] } {
		# [chassis]:slot format
		return [string map {, " "} "$slots"]
	} elseif { [ regexp {.*\[.*\].*} "$chassis" full] } {
		# [chassis] format
		return "all"
	} elseif { [ regexp {.*:.*:.*} "$chassis" full ] } {
		# ipv6 without [] nor slot
		return "all"
	} elseif { [regexp {[^:]+:(.*)} "$chassis" full slots] } {
		# chassis:slot format
		return [string map {, " "} "$slots"]
	} else {
		# chassis without [] nor slot
		return "all"
	}	
}

proc local_sh { } {
##
## local_sh
## -------------------
## start a local shell
##
## Usage:
##	local_sh
## Arguments:
## Returns:
##	0 - success
##	-code error on failure

	global spawn_id expect_out spawn_out timeout
	global expecting

	test_spawn "bash" bash

	expect_unix_prompt 60
	local_set_env
	return 0
}

proc local_sh_exit { } {
##
## local_sh_exit
## ----------------
## exit from shell connection created by local_sh
##
## Usage:
##	local_sh_exit
##
## Returns:
##	0 - success
##	-code error on failure

	send_unix_cmd "exit 0"
	ignore_rest

	return 0
}

proc local_set_env { } {
##
## local_set_env
## -------------------
## setup environment for shell session to allow testing
##
## Usage:
##	local_set_env
## Arguments:
##	None
## Returns:
##	0 - success
##	-code error on failure

	# set COLUMNS real large so the Posix shell doesn't enter
	# command edit mode and confuse us
	#unix_cmd 60 "" "export COLUMNS=500"
	#unix_cmd 60 "" "unset EDITOR"
	#unix_cmd 60 "" "unset VISUAL"
	#unix_cmd 60 "" "umask 0"
	#target_set_intr
	# get rid of cr and control characters in logs for easier reading
	#unix_cmd 60 0 {unset PROMPT_COMMAND; export PS1='[\u@\h]\$ '; stty -onlcr columns 500}
	unix_cmd 60 0 {unset PROMPT_COMMAND HISTFILE; set +o history; export PS1='[\u@\h]\$ '}

}

proc target_rlogin { target usercode password } {
##
## target_rlogin
## -------------------
## rlogin to the given target host
##
## Usage:
##	target_rlogin target usercode password
## Arguments:
##	target - hostname to rlogin to
##	usercode - usercode
##	password - password
## Returns:
##	0 - success
##	-code error on failure

	global spawn_id expect_out spawn_out timeout
	global expecting
	global stty_init
	global target_unix_prompt

	test_spawn "rlogin" rlogin $target -l $usercode
	set_timeout 120
	set expecting "rlogin session ($target $usercode)"
	expect {
		"assword:"	{ exp_send "$password\r"
				  exp_continue
				}
		"incorrect"		{ set info "Failed to rlogin to $target as $usercode, incorrect usercode"
				  return -code error -errorinfo $info $info
				}
		"failure"	{ set info "Failed to rlogin to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"refused"	{ set info "Failed to rlogin to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"nknown"	{ set info "Failed to rlogin to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"$target_unix_prompt"		noop
	}

	return 0
}

proc target_telnet { target usercode password } {
##
## target_telnet
## -------------------
## telnet to the given target host
##
## Usage:
##	target_telnet target usercode password
## Arguments:
##	target - hostname to rlogin to
##	usercode - usercode
##	password - password
## Returns:
##	0 - success
##	-code error on failure

	global spawn_id expect_out spawn_out timeout
	global expecting
	global stty_init
	global target_unix_prompt

	test_spawn "telnet" telnet $target -l $usercode
	set_timeout 120
	set expecting "telnet session ($target $usercode)"
	expect {
		"assword:"	{ exp_send "$password\r"
				  exp_continue
				}
		"incorrect"		{ set info "Failed to telnet to $target as $usercode, incorrect usercode"
				  return -code error -errorinfo $info $info
				}
		"failure"	{ set info "Failed to telnet to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"refused"	{ set info "Failed to telnet to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"nknown"	{ set info "Failed to telnet to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"$target_unix_prompt"		noop
	}

	return 0
}

proc target_chassis_telnet {target usercode password} {
##
## target_chassis_telnet
## -------------------
## telnet to the given target chassis
##
## Usage:
##  target_chassis_telnet target usercode password
## Arguments:
##  target - chassis name to telnet to
##  usercode - usercode
##  password - password
## Returns:
##  0 - success
##  -code error on failure
	global spawn_id expect_out spawn_out timeout
	global expecting
	global stty_init
	global target_chassis_prompt
	global target_chassis_shell target_chassis_shell_prompt
	global target_chassis_retry_limit

	set target_chassis_shell 0
	test_spawn "telnet" telnet $target
	set_timeout 120
	set expecting "telnet session ($target $usercode)"
	set retries 0
	expect {
        "ogin:" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to telnet to $target as $usercode"
				  		return -code error -errorinfo $info $info
					}
                   exp_continue
                 }
        "sername:" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to telnet to $target as $usercode"
				  		return -code error -errorinfo $info $info
					}
                   exp_continue
                 }
        "sername->" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to telnet to $target as $usercode"
				  		return -code error -errorinfo $info $info
					}
                   exp_continue
                 }
        "assword:"  { exp_send "$password\r"
                  exp_continue
                }
        "assword->"  { exp_send "$password\r"
                  exp_continue
                }
		"incorrect"		{ set info "Failed to telnet to $target as $usercode, incorrect usercode"
				  return -code error -errorinfo $info $info
				}
		"failure"	{ set info "Failed to telnet to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"refused"	{ set info "Failed to telnet to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		"nknown"	{ set info "Failed to telnet to $target as $usercode"
				  return -code error -errorinfo $info $info
				}
		-gl "$target_chassis_shell_prompt"	{ set target_chassis_shell 1 }
		-gl "$target_chassis_prompt"		noop
	}

    return 0
}


proc target_ssh { target usercode password } {
##
## target_ssh
## -------------------
## ssh to the given target host
##
## Usage:
##	target_ssh target usercode password
## Arguments:
##	target - hostname to ssh to
##	usercode - usercode
##	password - password
## Returns:
##	0 - success
##	-code error on failure

    global spawn_id expect_out spawn_out timeout
    global expecting
    global stty_init
	global target_unix_prompt

    test_spawn "ssh" ssh -q $usercode@$target
    set_timeout 30
    set expecting "ssh session ($target $usercode)"
    expect {
		"assword:" { exp_send "$password\r"
			    	exp_continue
					}
		"denied" { set info "Failed to ssh to $target as $usercode, incorrect password"
	    			return -code error -errorinfo $info $info
				}
		"failure" { set info "Failed to ssh to $target as $usercode"
	    			return -code error -errorinfo $info $info
				}
		"refused" { set info "Failed to ssh to $target as $usercode"
	    			return -code error -errorinfo $info $info
				}
		"nknown" { set info "Failed to ssh to $target as $usercode"
	    			return -code error -errorinfo $info $info
				}
		"ast login" {
	    			noop
				}
		"$target_unix_prompt"		noop
   	}

    return 0
}

proc target_chassis_ssh { target usercode password } {
##
## target_chassis_ssh
## -------------------
## ssh to the given target chassis
##
## Usage:
##	target_chassis_ssh target usercode password
## Arguments:
##	target - chassis to ssh to
##	usercode - usercode
##	password - password
## Returns:
##	0 - success
##	-code error on failure

    global spawn_id expect_out spawn_out timeout
    global expecting
    global stty_init
	global target_chassis_prompt
	global target_chassis_shell target_chassis_shell_prompt
	global target_chassis_retry_limit

	set target_chassis_shell 0
    test_spawn "ssh" ssh -q $usercode@$target
    set_timeout 60
    set expecting "ssh session ($target $usercode)"
	set retries 0
    expect {
		"ogin:" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to telnet to $target as $usercode"
				  		return -code error -errorinfo $info $info
					}
					exp_continue
					}
		"sername:" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to telnet to $target as $usercode"
				  		return -code error -errorinfo $info $info
					}
					exp_continue
					}
		"sername->" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to telnet to $target as $usercode"
				  		return -code error -errorinfo $info $info
					}
					exp_continue
					}
        "assword:"  { exp_send "$password\r"
                  	exp_continue
                	}
        "assword->"  { exp_send "$password\r"
                  	exp_continue
                	}
		"denied" { set info "Failed to ssh to $target as $usercode, incorrect password"
	    			return -code error -errorinfo $info $info
				}
		"failure" { set info "Failed to ssh to $target as $usercode"
	    			return -code error -errorinfo $info $info
				}
		"refused" { set info "Failed to ssh to $target as $usercode"
	    			return -code error -errorinfo $info $info
				}
		"nknown" { set info "Failed to ssh to $target as $usercode"
	    			return -code error -errorinfo $info $info
				}
		"continue connecting" { exp_send "yes\n"
				exp_continue
				}
		"ast login" {
	    			noop
				}
		-gl "$target_chassis_shell_prompt"	{ set target_chassis_shell 1 }
		-gl "$target_chassis_prompt"	noop
    }

    return 0
}

proc target_chassis_rlogin_slot {slot usercode password} {
##
## target_chassis_rlogin_slot
## -------------------
## rlogin to the given slot within the present chassis
## This should only be called by target_chassis_admin_sh
## otherwise tracking of shell vs cli prompt will be incorrect
##
## Usage:
##  target_chassis_rlogin_slot slot usercode password
## Arguments:
##	slot - slot to login to
##  usercode - usercode
##  password - password
## Returns:
##  0 - success
##  -code error on failure
	global spawn_id expect_out spawn_out timeout
	global expecting
	global stty_init
	global target_chassis_prompt
	global target_chassis_shell target_chassis_shell_prompt
	global target_chassis_retry_limit
	global target_chassis_shell_save

	set target_chassis_shell_save $target_chassis_shell
	send_chassis_cmd "rlogin $slot"
	set_timeout 60
	set expecting "rlogin session to slot $slot ($usercode)"
	set retries 0
	expect {
        "sername:" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to rlogin to slot $slot as $usercode"
				  		return -code error -errorinfo $info $info
					}
                   exp_continue
                 }
        "sername->" { exp_send "$usercode\r"
					set retries [ expr $retries + 1 ]
					if { $retries > $target_chassis_retry_limit } {
						 set info "Failed to rlogin to slot $slot as $usercode"
				  		return -code error -errorinfo $info $info
					}
                   exp_continue
                 }
        "assword:"  { exp_send "$password\r"
                  exp_continue
                }
        "assword->"  { exp_send "$password\r"
                  exp_continue
                }
		"incorrect"		{ set info "Failed to rlogin to slot $slot as $usercode, incorrect usercode"
				  return -code error -errorinfo $info $info
				}
		"failure"	{ set info "Failed to rlogin to slot $slot as $usercode"
				  return -code error -errorinfo $info $info
				}
		"refused"	{ set info "Failed to rlogin to slot $slot as $usercode"
				  return -code error -errorinfo $info $info
				}
		"nknown"	{ set info "Failed to rlogin to slot $slot as $usercode"
				  return -code error -errorinfo $info $info
				}
		"orry"	{ set info "Failed to rlogin to slot $slot as $usercode"
				  return -code error -errorinfo $info $info
				}
		"not responding"	{ set info "Failed to rlogin to slot $slot as $usercode"
				  return -code error -errorinfo $info $info
				}
		-gl "$target_chassis_shell_prompt"	{ set target_chassis_shell 1 }
		-gl "$target_chassis_prompt"		{ set target_chassis_shell 0 }
	}

    return 0
}

proc send_unix { text } {
##
## send_unix
## -------------------
## send a interactive response to a unix command during a login session
##
## Usage:
##	send_unix text
## Arguments:
##	text - text to send
## Returns:
##	nothing
## Additional Information:
##	Do NOT use this to send commands.
##	send_unix_cmd or unix_cmd should be used for that purpose

	global spawn_id expect_out spawn_out

	exp_send "$text"
}

proc send_chassis { text } {
##
## send_chassis
## -------------------
## send a interactive response to a chassis command during a login session
##
## Usage:
##	send_chassis text
## Arguments:
##	text - text to send
## Returns:
##	nothing
## Additional Information:
##	Do NOT use this to send commands.
##	send_chassis_cmd or chassis_cmd should be used for that purpose
	global spawn_id expect_out spawn_out

	exp_send "$text"
}

proc target_set_intr {} {
##
## target_set_intr
## ----------------
## set the interrupt character to control-C
##
## Usage:
##	target_set_intr
## Returns:
##	0 - success
##	-code error on failure

	unix_cmd 60 0 "stty intr ^C"
}

proc target_send_intr {} {
##
## target_send_intr
## ----------------
## set the interrupt character (eg. control-C) to a host
##
## Usage:
##	target_send_intr
## Returns:
##	0 - success
##	-code error on failure

    global spawn_id expect_out spawn_out timeout

	exp_send "\003"
}

proc chassis_send_intr {} {
##
## chassis_send_intr
## ----------------
## set the interrupt character (eg. control-C) to a chassis
##
## Usage:
##	chassis_send_intr
## Returns:
##	0 - success
##	-code error on failure

    global spawn_id expect_out spawn_out timeout

	exp_send "\003"
}

proc target_set_noecho {} {
##
## target_set_noecho
## ----------------
## disable echo during a unix login session or sh
##
## Usage:
##	target_set_noecho
## Returns:
##	0 - success
##	-code error on failure

	unix_cmd 60 0 "stty -echo"
}

proc target_set_echo {} {
##
## target_set_echo
## ----------------
## enable echo during a unix login session or sh
##
## Usage:
##	target_set_echo
## Returns:
##	0 - success
##	-code error on failure

	unix_cmd 60 0 "stty echo"
}

proc target_su { usercode {password "" }} {
##
## target_su
## -------------------
## change usercode during a unix login session
##
## Usage:
##	target_su usercode [password]
## Arguments:
##	usercode - usercode
##	password - password, if omitted assumes no password needed
## Returns:
##	0 - success
##	-code error on failure

	global spawn_id expect_out spawn_out timeout
	global expecting env
	global target_unix_prompt

	send_unix_cmd "su - $usercode"
	set_timeout 120
	set expecting "su session ($usercode)"
	expect {
		"assword:"	{ if { "$password" == "" } {
					set info "Failed to su $usercode, password required"
				  	return -code error -errorinfo $info $info
				  } else {
					exp_send "$password\n"
				  	exp_continue
				  }
				}
		"Sorry"		{ set info "Failed to su $usercode, incorrect password"
				  return -code error -errorinfo $info $info
				}
		"$target_unix_prompt"		noop
	}

	#unix_cmd 60 0 "ksh"
	target_set_env

	return 0
}

proc target_su_exit { } {
##
## target_su_exit
## -------------------
## exit super user shell created by target_su
##
## Usage:
##	target_su_exit
## Returns:
##	0 - success
##	-code error on failure

	global env

	# exit ksh
	#unix_cmd 60 "" "exit 0"
	# exit su
	unix_cmd 60 "" "exit 0"
}

proc target_set_env { } {
##
## target_set_env
## -------------------
## setup environment for unix shell session to allow testing
##
## Usage:
##	target_set_env
## Arguments:
##	None
## Returns:
##	0 - success
##	-code error on failure

	# set COLUMNS real large so the Posix shell doesn't enter
	# command edit mode and confuse us
	#unix_cmd 60 "" "export COLUMNS=500"
	#unix_cmd 60 "" "unset EDITOR"
	#unix_cmd 60 "" "unset VISUAL"
	#unix_cmd 60 "" "umask 0"
	#target_set_intr
	# get rid of cr and control characters in logs for easier reading
	unix_cmd 60 0 {unset PROMPT_COMMAND HISTFILE; set +o history; export PS1='[\u@\h]\$ '; stty -onlcr columns 500}
}

proc target_root_sh { target } {
##
## target_root_sh
## ----------------
## prepare for a test by login'ing to target host as user root
##
## Usage:
##	target_root_sh target
##
## Arguments:
##	target - hostname to login to
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	uses:
##		env(CFG_LOGIN_METHOD) to select between rlogin or ssh
##		env(CFG_USERNAME) as a non-root user name for rlogin (followed by su)
##		env(CFG_PASSWORD) as password for CFG_USERNAME for rlogin
##		env(CFG_ROOTPASS) as root password for ssh or su

	global env

	if { "$env(CFG_LOGIN_METHOD)" == "rlogin" } {
		target_rlogin $target $env(CFG_USERNAME) $env(CFG_PASSWORD)
		target_su root $env(CFG_ROOTPASS)
	} elseif { "$env(CFG_LOGIN_METHOD)" == "telnet" } {
		target_telnet $target $env(CFG_USERNAME) $env(CFG_PASSWORD)
		target_su root $env(CFG_ROOTPASS)
	} elseif { "$env(CFG_LOGIN_METHOD)" == "ssh" } {
		target_ssh $target root $env(CFG_ROOTPASS)
		target_set_env
	} else {
		set info "CFG_LOGIN_METHOD must be rlogin, telnet or ssh"
		return -code error -errorinfo $info $info
	}

	return 0
}

proc target_root_sh_exit { } {
##
## target_root_sh_exit
## ----------------
## exit from shell connection created by target_root_sh
##
## Usage:
##	target_root_sh_exit
##
## Returns:
##	0 - success
##	-code error on failure
##	uses:
##		env(CFG_LOGIN_METHOD) to select between rlogin or ssh

	global env

	if { "$env(CFG_LOGIN_METHOD)" == "rlogin" } {
		target_su_exit
	} elseif { "$env(CFG_LOGIN_METHOD)" == "telnet" } {
		target_su_exit
	} elseif { "$env(CFG_LOGIN_METHOD)" == "ssh" } {
		#unix_cmd 60 "" "exit 0"
		send_unix_cmd "exit 0"
	} else {
		set info "CFG_LOGIN_METHOD must be rlogin, telnet or ssh"
		return -code error -errorinfo $info $info
	}

	ignore_rest

	return 0
}

proc target_chassis_admin_sh { target {slot ""} {user "admin"}} {
##
## target_chassis_admin_sh
## ----------------
## prepare for a test by login'ing to chassis as user admin
##
## Usage:
##	target_chassis_admin_sh target [slot] [user]
##
## Arguments:
##	target - chassis to login to
##	slot - slot to login to, default is ""
##	user - user code to login as, default is admin
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	uses:
##		env(CFG_CHASSIS_LOGIN_METHOD) to select between telnet or ssh
##		env(CFG_CHASSIS_ADMIN_PASSWORD) as password for admin (if no password-less ssh)

	global env

	if { "$env(CFG_CHASSIS_LOGIN_METHOD)" == "ssh" } {
		target_chassis_ssh $target $user $env(CFG_CHASSIS_ADMIN_PASSWORD)
	} elseif { "$env(CFG_CHASSIS_LOGIN_METHOD)" == "telnet" } {
		target_chassis_telnet $target $user $env(CFG_CHASSIS_ADMIN_PASSWORD)
	} else {
		set info "CFG_CHASSIS_LOGIN_METHOD must be ssh or telnet"
		return -code error -errorinfo $info $info
	}
	if { "$slot" != "" } {
		target_chassis_rlogin_slot $slot $user $env(CFG_CHASSIS_ADMIN_PASSWORD)
	}

	return 0
}

proc target_chassis_admin_sh_exit { {rlogin 0} } {
##
## target_chassis_admin_sh_exit
## ----------------
## exit from shell connection created by target_chassis_admin_sh
##
## Usage:
##	target_chassis_admin_sh_exit [rlogin]
## Arguments:
##	rlogin - are we in an rlogin subshell
##
## Returns:
##	0 - success
##	-code error on failure
##	uses:

	global target_chassis_shell
	global target_chassis_shell_save

	if { $rlogin } {
		send_chassis "logout\n"
		expect_list 60 "connection closed"
		set target_chassis_shell $target_chassis_shell_save
	}
	send_chassis "logout\n"
	set target_chassis_shell 0

	ignore_rest

	return 0
}

proc expect_prompt { timelimit prompt {out_var ""}} {
##
## expect_prompt
## -------------------------
## wait for prompt during a session
##
## Usage:
##	expect_prompt timelimit prompt [out_var]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	prompt - regular expression for prompt
##	out_var - variable in callers context to receive all text which was
##		received during during this expect
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:

	global spawn_id expect_out spawn_out timeout
	
	if { "$out_var" != "" } {
		upvar $out_var $out_var
	}

	expect_list $timelimit "{$prompt}" {} $out_var

	return 0
}

proc expect_unix_prompt { timelimit {out_var ""}} {
##
## expect_unix_prompt
## -------------------------
## wait for a unix/darwin prompt during a session
##
## Usage:
##	expect_unix_prompt timelimit [out_var]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	out_var - variable in callers context to receive all text which was
##		received during during this expect
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
	global target_unix_prompt

	if { "$out_var" != "" } {
		upvar $out_var $out_var
	}
	return [ expect_prompt $timelimit "$target_unix_prompt" $out_var ]
}

proc expect_chassis_prompt { timelimit {out_var ""}} {
##
## expect_chassis_prompt
## -------------------------
## wait for a chassis prompt during a session
##
## Usage:
##	expect_chassis_prompt timelimit [out_var]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	out_var - variable in callers context to receive all text which was
##		received during during this expect
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
	global target_chassis_prompt
	global target_chassis_shell target_chassis_shell_prompt

	if { "$out_var" != "" } {
		upvar $out_var $out_var
	}
	if { $target_chassis_shell } {
		set prompt "$target_chassis_shell_prompt"
	} else {
		set prompt "$target_chassis_prompt"
	}
	return [ expect_prompt $timelimit "$prompt" $out_var ]
}

proc target_get_config_stack_type { { host "" } } {
##
## target_get_config_stack_type
## -------------------------
## get the configured stack type of the specified system.
##
## Usage:
##	target_get_config_stack_type host
## Arguments:
##	host - host to look for in CFG_OFED_HOSTS
## Returns:
##	"OPENIB" or "IBACCESS"
##	-code error on failure (eg. unable to identify stack type)
## Additional Information:
##		checks for host CFG_OFED_HOSTS and stack type is reported based solely
##		on CFG_OFED_HOSTS.  If not specified or CFG_OFED_HOSTS is not exported
##		then returns an error
	global env

	if { "$host" == "" || ! [ info exists env(CFG_OFED_HOSTS) ] } {
		set info "Unable to Determine IB Stack Type"
		return -code error -errorinfo $info $info
	}
	set index [ lsearch -exact $env(CFG_OFED_HOSTS) $host ]
	if { $index != -1 } {
		return "OPENIB"
	} else {
		return "IBACCESS"
	}
}

proc target_get_stack_type { { checkrun "y"} { host "" } } {
##
## target_get_stack_type
## -------------------------
## get the stack type of the current target Linux/Unix system.
##
## Usage:
##	target_get_stack_type [checkrun [host]]
## Arguments:
##	checkrun - if y, the running stack is checked first, otherwise
##		just installed stack is checked.  Note that during reinstall
##		an old stack could be left running until the next reboot
##		default is y.  Beware, due to newer distros including openib, if it
##		appears both are installed and 'n' is used, this reports IBACCESS.
##		It is safest to invoke this with 'y'.
##	host - if specified and CFG_OFED_HOSTS is exported, then host is looked
##		for in CFG_OFED_HOSTS and stack type is reported based solely on
##		CFG_OFED_HOSTS.  If not specified or CFG_OFED_HOSTS is not exported
##		then actual running or installed stack is checked
## Returns:
##	"OPENIB" or "IBACCESS"
##	-code error on failure (eg. unable to identify stack type)
## Additional Information:
##	The global timeout is changed by this routine
##	If no IB stack is found running, then checks which stack is installed

	global spawn_id expect_out spawn_out
	global env

	# try static lookup first
	if { [ catch { target_get_config_stack_type "$host" } res ] == 0 } {
		return "$res";
	}

	set target_os_type [target_get_os_type]

	if { "$checkrun" == "y" } {
		# ib_umad is a required module for OPEN IB stack
		send_unix_cmd {lsmod|egrep '^ib_umad[[:space:]]'}
		if { 0 == [ get_exit_status 60 ] } {
			return "OPENIB"
		}
		# ibt is a required module for IbAccess stack
		send_unix_cmd {lsmod|egrep '^ibt[[:space:]]'}
		if { 0 == [ get_exit_status 60 ] } {
			return "IBACCESS"
		}
	}

	# see if IbAccess installed, 0 -> yes
	send_unix_cmd {test -e /etc/init.d/iba -o -e /lib/modules/`uname -r`/iba/ibt.ko}
	set iba_not_installed [ get_exit_status 60 ]

	# see if Open IB installed, 0 -> yes
	send_unix_cmd {test -e /etc/init.d/openibd -o -e /lib/modules/`uname -r`/kernel/drivers/infiniband/core/ib_umad.ko -o -e /lib/modules/`uname -r`/kernel/drivers/infiniband/core/ib_umad.ko.xz}
	set openib_not_installed [ get_exit_status 60 ]

	# newer distros include openib, so if both appear installed also assume iba
	# presently this is only called with checkrun=n for automated tests configio
	if { ! $iba_not_installed } {
		return "IBACCESS"
	}
	if { $iba_not_installed && ! $openib_not_installed } {
		return "OPENIB"
	}
	set info "Unable to Determine IB Stack Type"
	return -code error -errorinfo $info $info
}

proc target_get_os_type { } {
##
## target_get_os_type
## -------------------------
## get the os_type of the current target Linux/Darwin/Unix system.
##
## Usage:
##	target_get_os_type
## Arguments:
## Returns:
##	"Linux" or "Darwin" (eg. uname -s output of target system)
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	set_timeout 60
	# we use SSS around output so we don't mistake other text in output
	# (such as in hostname in prompt) for the os_type
	send_unix_cmd "echo SSS`uname -s`SSS"
	set expecting "target os_type"
	expect {
		-re {SSS[A-Za-z0-9]+SSS}	noop
	}

	regexp {SSS([A-Za-z0-9]+)SSS} $expect_out(0,string) line ret
	expect_unix_prompt 60

	return $ret
}

# TBD - could be moved to TestTools/mpi.exp
# Note: not used by FF, only by Int Tests
proc target_get_registered_mpi_version {mpi_type} {
    global spawn_id expect_out spawn_out timeout
    global expecting

    # Below cmd to flushing out the out buffet that might contains
    # too many lines of screen output, causing the output of a cmd
    # not being capture in the output. May be there is other better
    # way to clean the out buffet
    set out ""
    send_unix_cmd "echo SSS`mpi-selector --list`EEE"
    expect_list 60 { "mpi-selector" "EEE" "EEE" } {} out

    set out ""
    send_unix_cmd "echo SSS`mpi-selector --list`EEE"
    expect_list 60 { "mpi-selector" "EEE" "EEE" } {} out
    
    regexp "EEE.*SSS+(.*)EEE$" $out full_out mpi_list

    check_exit_status 60 0    
    
    if {![regexp "($mpi_type)-(\[^ ]+)" $mpi_list line mpi_type_match mpi_version]} {
        set info "Did not find mpi_type($mpi_type) in mpi_list($mpi_list)"
        return -code error -errorinfo $info $info
    }
    
    return $mpi_version
}

proc get_exit_status { timelimit { wait_prompt 1 } } {
##
## get_exit_status
## -------------------------
## wait for shell prompt and return the exit status of the last command executed
## during a login session, then wait for shell prompt
##
## Usage:
##	get_exit_status timelimit [wait_prompt]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	wait_prompt - should we wait for prompt before issuing echo of status
## Returns:
##	exit code - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	if { $wait_prompt } {
		expect_unix_prompt $timelimit
	}

	set_timeout 60
	# we use SSS around number so we don't mistake other numbers in output
	# (such as in hostname in prompt) for the exit code
	send_unix_cmd "echo SSS$?SSS"
	set expecting "exit code"
	expect {
		-re {SSS[0-9]+SSS}	noop
	}

	regexp {[0-9]+} $expect_out(0,string) ret
	expect_unix_prompt 60

	return $ret
}

proc check_exit_status { timelimit exit_code { wait_prompt 1 } } {
##
## check_exit_status
## -------------------------
## wait for shell prompt and check the exit status of the last command executed
## during a login session, then wait for shell prompt
##
## Usage:
##	check_exit_status timelimit exit_code [wait_prompt]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	exit_code - expected exit code ([0-9] if don't care)
##	wait_prompt - should we wait for prompt before issuing echo of status
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	if { $wait_prompt } {
		expect_unix_prompt $timelimit
	}

	set_timeout 60
	# we use SSS around number so we don't mistake other numbers in output
	# (such as in hostname in prompt) for the exit code
	send_unix_cmd "echo SSS$?SSS"
	set expecting "exit code: $exit_code"
	expect {
		"SSS${exit_code}SSS"	noop
		-re {SSS[0-9]+SSS}	{ set info "Incorrect exit code: Expected $exit_code, Got: $expect_out(0,string)"
				  return -code error -errorinfo $info $info
				}
	}

	expect_unix_prompt 60

	return 0
}

proc get_chassis_exit_status { timelimit { wait_prompt 1 } } {
##
## get_chassis_exit_status
## -------------------------
## wait for shell prompt and return the exit status of the last command executed
## during an chassis session, then wait for shell prompt
##
## Usage:
##	get_exit_status timelimit [wait_prompt]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	wait_prompt - should we wait for prompt before issuing echo of status
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	if { $wait_prompt } {
		expect_chassis_prompt $timelimit
	}

	set_timeout 60
	send_chassis_cmd "showLastRetCode"
	set expecting "exit code"
	expect {
		-re {Code: [0-9]+:}	noop
	}

	regexp {[0-9]+} $expect_out(0,string) ret
	expect_chassis_prompt 60

	return $ret
}

proc check_chassis_exit_status { timelimit exit_code { wait_prompt 1 } } {
##
## check_chassis_exit_status
## -------------------------
## wait for shell prompt and check the exit status of the last command executed
## during an chassis session, then wait for shell prompt
##
## Usage:
##	check_exit_status timelimit exit_code [wait_prompt]
## Arguments:
##	timelimit - maximum wait for shell prompt
##	exit_code - expected exit code ([0-9] if don't care)
##	wait_prompt - should we wait for prompt before issuing echo of status
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	if { $wait_prompt } {
		expect_chassis_prompt $timelimit
	}

	set_timeout 60
	send_chassis_cmd "showLastRetCode"
	set expecting "exit code: $exit_code"
	expect {
		"Code: ${exit_code}:"	noop
		-re {Code: [0-9]+:}	{ set info "Incorrect exit code: Expected $exit_code, Got: $expect_out(0,string)"
				  return -code error -errorinfo $info $info
				}
	}
	expect_chassis_prompt 60

	return 0
}

proc longcmd { cmd } {
##
## longcmd
## --------
## Add newlines and backslashes to a command to avoid command line editor
##
## Usage:
##	longcmd cmd
## Arguments:
##	cmd - a shell command to break up into multiple lines
## Returns:
##	A command to execute
##
## Additional Information:
##	On some platforms the command line editor cannot be turned off.  On other
##	platforms setting COLUMNS=500, unset EDITOR and unset VISUAL would
##	cause the command line editor to not affect entry of long command lines
##	Unfortunately, the entry of such long
##	lines causes the command line editor to re-display the line
##	including the # or $ prompt.  This makes the expect scripts think the
##	command has completed before it has even started.
##
	if { [ string length $cmd ] < 70 } {
		return $cmd
	}
	# break up every 70th character
	set result {}
	while { [ string length $cmd ] > 0 } {
		append result [string range $cmd 0 69 ]
		append result "\\\n"
		set cmd [ string range $cmd 70 end ]
	}
	return $result
}
		
proc send_unix_cmd { cmd { separator "\n"} } {
##
## send_unix_cmd
## -------------------
## issue a unix command to the target system during a login session
##
## Usage:
##	send_unix_cmd cmd [separator]
## Arguments:
##	cmd - unix command to issue
##	separator - separator at end of command, default is newline
## Returns:
##	nothing
## Additional Information:
##	This must be used to send any command which could be larger than
##	70 characters and for which the first expect is for a shell prompt
##	If there are other expect's before the wait for the shell prompt,
##	a simple send can also be used
##
##	Do NOT use this to send responses to interactive commands.
##	exp_send should be used for that purpose

	global spawn_id expect_out spawn_out timeout
	global env

	exp_send "$cmd$separator"
	# possible workaround if command line editor gets involved
	#exp_send [ longcmd "$cmd$separator" ]
}

proc send_chassis_cmd { cmd } {
##
## send_chassis_cmd
## -------------------
## issue a command to the target chassis during a login session
##
## Usage:
##	send_chassis_cmd cmd
## Arguments:
##	cmd - chassis command to issue
## Returns:
##	nothing
## Additional Information:
##	This should be used to send all chassis commands.  It handles
##	the support shell vs cli interface as needed.  The decision as to
##	type of shell inteface is made per chassis during login.
##
##	Do NOT use this to send responses to interactive commands.
##	exp_send should be used for that purpose
##
##	unlike send_unix_cmd, there is no "separator" argument.  This is for a
##	few reasons:
##		- implementation with target_chassis_shell could be tricky
##		- older versions of chassis FW only allowed newline as the separator

	global spawn_id expect_out spawn_out timeout
	global env
	global target_chassis_shell

	if { $target_chassis_shell } {
		exp_send "cli \"$cmd\"\n"
	} else {
		exp_send "$cmd\n"
	}
}

proc unix_cmd { timelimit exit_code cmd } {
##
## unix_cmd
## -------------------
## issue a unix command to the target system during a login session
##
## Usage:
##	unix_cmd timelimit exit_code cmd
## Arguments:
##	timelimit - maximum wait for command to complete (shell prompt)
##	exit_code - expected exit code ("" if don't care)
##	cmd - unix command to issue
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global env


	if { "$exit_code" != "" } {
		send_unix_cmd $cmd ";"
		check_exit_status $timelimit $exit_code 0
	} else {
		send_unix_cmd $cmd
		expect_unix_prompt $timelimit
	}

	return 0
}

proc chassis_cmd { timelimit exit_code cmd } {
##
## chassis_cmd
## -------------------
## issue a chassis command to the target system during a login session
##
## Usage:
##	chassis_cmd timelimit exit_code cmd
## Arguments:
##	timelimit - maximum wait for command to complete (shell prompt)
##	exit_code - expected exit code ("" if don't care)
##	cmd - chassis command to issue
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global env

	if { "$exit_code" != "" } {
		send_chassis_cmd $cmd
		check_chassis_exit_status $timelimit $exit_code
	} else {
		send_chassis_cmd $cmd
		expect_chassis_prompt $timelimit
	}

	return 0
}

proc host_send_unix_cmd { host cmd { separator "\n"} } {
##
## host_send_unix_cmd
## -------------------
## issue a unix command to host via the target system during a login session
##
## Usage:
##	host_send_unix_cmd host cmd [separator]
## Arguments:
##	host - host to issue command to
##	cmd - unix command to issue
##	separator - separator at end of command, default is newline
## Returns:
##	nothing
## Additional Information:
##	This must be used to send any command which could be larger than
##	70 characters and for which the first expect is for a shell prompt
##	If there are other expect's before the wait for the shell prompt,
##	a simple send can be used

	global spawn_id expect_out spawn_out timeout
	global env

	exp_send "ssh -q -o ForwardX11=no -P root@$host $cmd$separator"
	# possible workaround if command line editor gets involved
	#exp_send [ longcmd "ssh -q -o ForwardX11=no -P root@$host $cmd$separator" ]
}

proc opacmdall_stop_child { spawn_id } {
##
## opacmdall_stop_child
## ----------
## stop the given child process
## provided for use by opacmdall only
## This avoids use of the test infrastructure
##
## Usage:
##	opacmdall_stop_child spawn_id
## Arguments:
##	spawn_id - spawn id's from spawn
## Returns:
##	exit status of process
##	or -code error if unable to wait for process

	global errorCode

	# see if child still running
	if { [catch {set pid [exp_pid -i $spawn_id]}] == 1 } {
	    # child must already be dead
	    #puts "Child exited"
	} else {
	    #puts "Stopping $pid"
	    # terminate child
	    catch {exp_close -i $spawn_id }
		catch {exec kill -s INT $pid }
		#catch {exec kill -s TERM $pid }
	}
	set status {}
	catch {set status [exp_wait -i $spawn_id] }
	# return the exit status of the process
	# exp_wait returns a list:
	# { pid spawn_id OS_ret exit_status }
	# if OS_ret is 0, status is process exit status
	# if OS_ret is -1, status is OS errno and errorCode also set
	# if child was killed, there can be 3 more elements in list:
	# { pid spawn_id 0 0 CHILDKILLED signal desc}
	if { [ llength $status ] < 4 } {
		set info "wait for child failed"
		return -code error -errorinfo $info $info
	} elseif { [ lindex $status 4] == "CHILDKILLED" } {
		set info "child killed: [lrange $status 5 end]"
		return -code error -errorinfo $info $info
	} elseif { [ lindex $status 2 ] == 0 } {
		# process exited
		set exit_status [ lindex $status 3]
		return $exit_status
	} else {
		# OS error running wait
		set info "wait for child failed: $errorCode"
		return -code error -errorinfo $info $info
	}
}

proc host_run_cmd { host user cmd {quiet 0} {timelimit -1} {prefix ""} } {
##
## host_run_cmd
## -------------------
## issue a unix command to host directly via ssh
## provided for use by opacmdall only
## This avoids use of the test infrastructure
##
## Usage:
##	host_run_cmd host user cmd [[[quiet] timelimit] prefix]
## Arguments:
##	host - host to issue command to
##	user - user code to connect to host as
##	cmd - unix command to issue
##	quiet - if 1 command is not shown
##	timelimit - maximum time to allow for command to complete, -1-> unlimited
##	prefix - prefix to output at start of every line output by host
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global spawn_id expect_out spawn_out timeout errorCode
	global env

	if { ! $quiet } {
		puts "$prefix\[$user@$host\]# $cmd"
	}
	if { $timelimit <= 0 } {
		set timelimit -1
	}

	if { $timelimit == -1 &&  [string equal "$prefix" "" ] } {
		if { [ catch { exec ssh -q -o ForwardX11=no -P "$user@$host" "$cmd" >@ stdout 2>@ stderr } res ] != 0 } {
			set exit_status [concat $errorCode]
			if { [ lindex $exit_status 0 ] == "CHILDSTATUS" } {
				set res "child exit code: [lindex $exit_status 2]"
			} elseif { [ lindex $exit_status 0 ] == "CHILDKILLED" } {
				set res "child killed: [lrange $exit_status 2 end]"
			} else {
				set res "$res: $errorCode"
			}
			puts stderr "$prefix$user@$host: $cmd: Command execution FAILED: $res"
		}
	} else {
    	global spawn_id user_spawn_id expect_out spawn_out timeout
		# disable normal output so we can insert prefix
		set save_log_user [log_user -info]
		if { [ catch { 
				log_user 0
    			spawn -noecho ssh -q -o ForwardX11=no -P "$user@$host" "$cmd"
				# do not use TEST_TIMEOUT_MULT
    			set timeout $timelimit
    			expect {
					\n		{puts -nonewline "$prefix$expect_out(buffer)"; exp_continue -continue_timer}
					\r		{puts -nonewline "$prefix$expect_out(buffer)"; exp_continue -continue_timer}
					eof		{
								# handle output without newline at end
								if { ! [ string equal "$expect_out(buffer)" "" ] } {
									puts "$prefix$expect_out(buffer)"

								}
							}
					timeout {return -code error -errorinfo "Timeout" "Timeout" }
				}
 				#interact {
				#	-input $user_spawn_id
				#		timeout [ calc_timeout $timelimit ] {
				#			#puts "Timeout"
				#			 return -code error -errorinfo "Timeout" "Timeout"
				#			}
				#		\003	{
				#			#puts "Interrupted"
				#			 return -code error -errorinfo "Interrupted" "Interrupted"
				#			}
				#	-output $spawn_id
				#		eof	return
 				#}
				set ret [opacmdall_stop_child $spawn_id]
				if { $ret != 0 } {
					set info "child exit code: $ret"
				  	return -code error -errorinfo $info $info
				}
			} res ] != 0 } {
				catch { opacmdall_stop_child $spawn_id }
				puts stderr "$prefix$user@$host: $cmd: Command execution FAILED: $res"
		}
		log_user $save_log_user
	}
}

proc chassis_run_cmd { chassis user cmd {quiet 0} {slot ""} {marker ""} {prefix ""}} {
##
## chassis_run_cmd
## -------------------
## issue a chassis command to chassis
## provided for use by opacmdall only
##
## Usage:
##	chassis_run_cmd host user cmd [quiet [slot [marker]]]
## Arguments:
##	chassis - chassis to issue command to
##	user - user code to connect to chassis as
##	cmd - chassis CLI command to issue
##	quiet - if 1 command is not shown
##	slot - slot to login to, default is ""
##	marker - marker for end of command output, otherwise first instance of
##			prompt marks end of data.  When marker is supplied, prompts which
##			preceed marker are ignored, but a final prompt is still waited for
##			after the marker
##	prefix - prefix to output at start of every line output by host
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global spawn_id expect_out spawn_out timeout
	global env
	global target_chassis_prompt
	global target_chassis_shell target_chassis_shell_prompt
	global log_disable

	# disable detailed logging to stdout
	log_user 0
	set log_disable 1
	setup_expect_after
	if { ! $quiet } {
		if { "$slot" != "" } {
			puts "$prefix\[$user@$chassis:$slot\]# $cmd"
		} else {
			puts "$prefix\[$user@$chassis\]# $cmd"
		}
	}
	if { [ catch { target_chassis_admin_sh "$chassis" "$slot" "$user" } res ] != 0 } {
		puts stderr "$prefix$user@$chassis: $cmd: Command execution FAILED (Login): $res"
		return
	}
	if { $target_chassis_shell } {
		set prompt "$target_chassis_shell_prompt"
	} else {
		set prompt "$target_chassis_prompt"
	}
	if { [ catch {
				send_chassis_cmd "$cmd"
				set out ""
				if { "$marker" == "" } {
					expect_progress 120 {[\r\n\b]} "{$prompt}" {} out
				} else {
					expect_progress 120 {[\r\n\b]} "{$marker} {$prompt}" {} out
				}
 				} res ] != 0 } {
		puts stderr "$prefix$user@$chassis: $cmd: Command execution FAILED: $res"
	} elseif { [ catch {
				# filter out $cmd at start
				set match [string first "$cmd" "$out"]
				set length [string length "$cmd" ]
				if { $match != -1 } {
					set out [string range "$out" [expr $match + $length] end]
				}
				# filter out prompt
				if { "$marker" == "" } {
					regexp "\[\\\r\\\n\]+(.*)${prompt}" "$out" full_out output
				} else {
					regexp "\[\\\r\\\n\]+(.*$marker.*)${prompt}" "$out" full_out output
				}
				if { ! [string equal "$prefix" "" ] } {
					# insert prefix
					regsub -all -line {^.*$} "$output" "$prefix\\0" new_output
					# if last character is not newline add one
					set lastchar [string index $new_output end ] 
					if { "$lastchar" != {\n} && "$lastchar" != {\r} } {
						puts stdout $new_output
					} else {
						puts -nonewline stdout $new_output
					}
				} else {
					puts -nonewline stdout $output
				}
				check_chassis_exit_status 60 0 0
 				} res ] != 0 } {
		puts stderr "$prefix$user@$chassis: $cmd: Command execution FAILED: $res"
	}
	catch { target_chassis_admin_sh_exit [expr {"$slot" != ""} ] }
}

proc hosts_run_cmd { hosts user cmd {quiet 0} {timelimit -1} {output_prefix 0} } {
##
## hosts_run_cmd
## -------------------
## issue a unix command to hosts directly via ssh
## provided for use by opacmdall only
## This avoids use of the test infrastructure
##
## Usage:
##	hosts_run_cmd hosts user cmd [[[quiet] timelimit] output_prefix]
## Arguments:
##	hosts - hosts to issue command to, if "" uses $env(CFG_HOSTS)
##	user - user code to connect to host as
##	cmd - unix command to issue
##	quiet - if 1 command is not shown
##	timelimit - maximum time to allow for command to complete, -1-> unlimited
##	output_prefix - should host: be output as a prefix to each line
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global env
	if { "$hosts" == "" } {
		set hosts $env(CFG_HOSTS)
	}
	foreach host $hosts {
		if { $output_prefix } {
			set prefix "$host: "
		} else {
			set prefix ""
		}
		host_run_cmd $host $user "$cmd" $quiet $timelimit "$prefix"
	}
}

# pardon my spelling but I need a plural version of chassis
# which has a different spelling so function names are unique
proc chassises_run_cmd { chassises user cmd {quiet 0} {marker ""} {output_prefix 0}} {
##
## chassises_run_cmd
## -------------------
## issue a chassis command to chassises directly via ssh
## provided for use by opacmdall only
##
## Usage:
##	chassises_run_cmd hosts user cmd [[quiet [marker]] output_prefix]
## Arguments:
##	chassises - chassises to issue command to, if "" uses $env(CFG_CHASSIS)
##			can have slot specifier per chassis entry
##	user - user code to connect to host as
##	cmd - unix command to issue
##	quiet - if 1 command is not shown
##	marker - marker for end of command output, otherwise first instance of
##			prompt marks end of data.  When marker is supplied, prompts which
##			preceed marker are ignored, but a final prompt is still waited for
##			after the marker
##	output_prefix - should host: be output as a prefix to each line
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global env
	if { "$chassises" == "" } {
		set chassises $env(CFG_CHASSIS)
	}
	foreach chassis $chassises {
		set slots [chassis_get_slots "$chassis"]
		set chassis [chassis_strip_slots "$chassis"]
		foreach slot $slots {
			if { "$slot" == "all" } {
				set slot ""
			}
			if { $output_prefix } {
				if { "$slot" != "" } {
					set prefix "$chassis:$slot: "
				} else {
					set prefix "$chassis: "
				}
			} else {
				set prefix ""
			}
			chassis_run_cmd $chassis $user "$cmd" $quiet $slot "$marker" "$prefix"
		}
	}
}

proc hosts_parallel_run_cmd { hosts user cmd {quiet 0} {timelimit -1} {output_prefix 0}} {
##
## hosts_parallel_run_cmd
## -------------------
## issue a unix command to hosts directly via ssh, in parallel on all hosts
## provided for use by opacmdall only
## This avoids use of the test infrastructure
##
## Usage:
##	hosts_parallel_run_cmd hosts user cmd [[[quiet] timelimit] output_prefix]
## Arguments:
##	hosts - hosts to issue command to, if "" uses $env(CFG_HOSTS)
##	user - user code to connect to host as
##	cmd - unix command to issue
##	quiet - if 1 command is not shown
##	timelimit - maximum time to allow for command to complete, -1-> unlimited
##	output_prefix - should host: be output as a prefix to each line
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global env
	global os_type
	if { "$hosts" == "" } {
		set hosts $env(CFG_HOSTS)
	}
	parallel host $hosts {
		if { $output_prefix } {
			set prefix "$host: "
		} else {
			set prefix ""
		}
		 host_run_cmd "$host" $user "$cmd" $quiet $timelimit "$prefix"
	}
}

# pardon my spelling but I need a plural version of chassis
# which has a different spelling so function names are unique
proc chassises_parallel_run_cmd { chassises user cmd {quiet 0} {marker ""} {output_prefix 0}} {
##
## chassises_parallel_run_cmd
## -------------------
## issue a chassis command to chassises, in parallel on all chassises
## provided for use by opacmdall only
##
## Usage:
##	chassises_parallel_run_cmd chassises user cmd [[quiet [marker]] output_prefix]
## Arguments:
##	chassises - chassises to issue command to, if "" uses $env(CFG_HOSTS)
##			can have slot specifier per chassis entry
##	user - user code to connect to chassis as
##	cmd - chassis command to issue
##	quiet - if 1 command is not shown
##	marker - marker for end of command output, otherwise first instance of
##			prompt marks end of data.  When marker is supplied, prompts which
##			preceed marker are ignored, but a final prompt is still waited for
##			after the marker
##	output_prefix - should host: be output as a prefix to each line
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global env
	if { "$chassises" == "" } {
		set chassises $env(CFG_CHASSIS)
	}
	parallel chassis $chassises {
		set slots [chassis_get_slots "$chassis"]
		set chassis [chassis_strip_slots "$chassis"]
		foreach slot $slots {
			if { "$slot" == "all" } {
				set slot ""
			}
			if { $output_prefix } {
				if { "$slot" != "" } {
					set prefix "$chassis:$slot: "
				} else {
					set prefix "$chassis: "
				}
			} else {
				set prefix ""
			}
			chassis_run_cmd "$chassis" $user "$cmd" $quiet $slot "$marker" "$prefix"
		}
	}
}

proc chassis_scp_cmd { scp_cmd } {
##
## chassis_scp_cmd
## -------------------
## perform an scp to a chassis
## provided for use by opascpall, opauploadall, opadownloadall only
##
## Usage:
##	chassis_scp_cmd scp_cmd
## Arguments:
##	scp_cmd - scp unix command to issue
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global spawn_id expect_out spawn_out timeout
	global env
	global log_disable
	global expecting

	# disable detailed logging to stdout
	log_user 0
	set log_disable 1

	local_sh
	send_unix_cmd "$scp_cmd; echo 'SCP IS DONE'"
	# handle password prompts, once we start to see progress indicators
	# we can do a normal wait progress
	set_timeout 120
	set expecting "scp session ($scp_cmd)"
	expect {
        "assword:"  { exp_send "$env(CFG_CHASSIS_ADMIN_PASSWORD)\r"
                  exp_continue
                }
        "assword->"  { exp_send "$env(CFG_CHASSIS_ADMIN_PASSWORD)\r"
                  exp_continue
                }
		"refused"	{ set info "Failed to $scp_cmd"
				  return -code error -errorinfo $info $info
				}
		"denied"	{ set info "Failed to $scp_cmd"
				  return -code error -errorinfo $info $info
				}
		"Error"	{ set info "Failed to $scp_cmd"
				  return -code error -errorinfo $info $info
				}
		"continue connecting" { exp_send "yes\r"
				exp_continue
				}
		{\*}	noop
		"%"		noop
		"No such" { set info "Failed to $scp_cmd"
				  return -code error -errorinfo $info $info
				}
	}
	expect_progress 200 { "\\\*" "\\\." "=" "-" "%" } { "SCP IS DONE" } {"No such" "Error" "Invalid" "lost" }
	check_exit_status 60 0 
	local_sh_exit

    return 0
}

proc chassis_sftp_cmd { sftp_cmd sftp_action { not_found 1 } } {
##
## chassis_sftp_cmd
## -------------------
## perform an sftp to a chassis
## provided for use by opachassisadmin
##
## Usage:
##	chassis_sftp_cmd sftp_cmd sftp_action
## Arguments:
##	sftp_cmd - sftp unix command to issue
##  sftp_action - sftp command to issue once sftp connection has
##                been established
## Returns:
##	nothing
## Additional Information:
##	errors are caught and output to stderr

	global spawn_id expect_out spawn_out timeout
	global env
	global log_disable
	global expecting

	# disable detailed logging to stdout
	log_user 0
	set log_disable 1

	local_sh
	send_unix_cmd "$sftp_cmd; echo 'SFTP IS DONE'"
	
	# handle password prompts, once we start to see progress indicators
	# we can do a normal wait progress
	set_timeout 120
	set expecting "sftp session ($sftp_cmd)"
	expect {
        "assword:"  { exp_send "$env(CFG_CHASSIS_ADMIN_PASSWORD)\r"
                  exp_continue
                }
        "assword->"  { exp_send "$env(CFG_CHASSIS_ADMIN_PASSWORD)\r"
                  exp_continue
                }
		"refused"	{ set info "Failed to $sftp_cmd"
				  return -code error -errorinfo $info $info
				}
		"denied"	{ set info "Failed to $sftp_cmd"
				  return -code error -errorinfo $info $info
				}
		"Error"	{ set info "Failed to $sftp_cmd"
				  return -code error -errorinfo $info $info
				}
		"continue connecting" { exp_send "yes\r"
				exp_continue
				}
		"sftp>" 	{ exp_send "$sftp_action\r" 
					exp_send "bye\r"
					}
		{\*}	noop
		"%"		noop
		"No such" { set info "Failed to $sftp_cmd"
				  return -code error -errorinfo $info $info
				}
	}
	if { $not_found } {
		expect_progress 200 { "\\\*" "\\\." "=" "-" "%" } { "SFTP IS DONE" } {"No such" "Error" "Invalid" "lost" "not found"}
	} else {
		expect_progress 200 { "\\\*" "\\\." "=" "-" "%" } { "SFTP IS DONE" } {"No such" "Error" "Invalid" "lost"}
	}
	check_exit_status 60 0 
	local_sh_exit

    return 0
}

proc host_unix_cmd { timelimit exit_code host cmd } {
##
## host_unix_cmd
## -------------------
## issue a unix command to host via the target system during a login session
##
## Usage:
##	host_unix_cmd timelimit exit_code host cmd
## Arguments:
##	timelimit - maximum wait for command to complete (shell prompt)
##	exit_code - expected exit code ("" if don't care)
##	host - host to issue command to
##	cmd - unix command to issue
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global env

	if { "$exit_code" != "" } {
		host_send_unix_cmd $host $cmd ";"
		check_exit_status $timelimit $exit_code 0
	} else {
		host_send_unix_cmd $host $cmd
		expect_unix_prompt $timelimit
	}

	return 0
}

proc host_save_log { hostname } {
##
## host_save_log
## -------------------
## Save OS log file for hostname for later use by check_log
##
## Usage:
##	host_save_log hostname
## Arguments:
##	hostname - host to save log on, saved to /tmp/log
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:

	global tests_dir

	exec "ssh -q root@$hostname '$tests_dir/bin/save_log /tmp/log'"
}

proc host_check_log { hostname testname } {
##
## host_check_log
## -------------------
## Check OS log file for hostname relative to last host_save_log
##
## Usage:
##	host_check_log hostname testname
## Arguments:
##	hostname - host to check log on
##	testname - name of test to be used in log filenames
## Returns:
##	0 - success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##	assumes already logged into hostname

	global tests_dir

	send_unix_cmd "cd /root/$tests_dir; ./bin/check_log /tmp/log ./logs/$test/$test. ./logs/$test"

	expect_list 120 "DONE" { "ERROR" "WARN" "kernel" }
}

proc host_basename { hostname } {
##
## host_basename
## -------------------
## return hostname with any $env(CFG_IPOIB_SUFFIX) or $env(CFG_INIC_SUFFIX)
## suffix removed
##
## Usage:
##	host_basename hostname
## Arguments:
##	hostname - host name used by test driver (could be an $env(CFG_IPOIB_SUFFIX)
##				or $env(CFG_INIC_SUFFIX) name)
## Returns:
##	base of hostname, suitable for an $env(CFG_IPOIB_SUFFIX),
##	or $env(CFG_INIC_SUFFIX) suffix to be added
## Additional Information:
##	When called by FastFabric, CFG_IPOIB_SUFFIX is "", so this is a noop
	global env

	set result $hostname
	if { "$env(CFG_IPOIB_SUFFIX)" != "" } {
		regsub -- "$env(CFG_IPOIB_SUFFIX)\$" $hostname "" result
	}
	if { "$env(CFG_INIC_SUFFIX)" != "" } {
		regsub -- "$env(CFG_INIC_SUFFIX)\$" $hostname "" result
	}
	return "$result"
}

proc get_hostname { number } {
##
## get_hostname
## -------------------
## return hostname for number'th host in CFG_HOSTS
##
## Usage:
##	get_hostname number
## Arguments:
##	number - host number, 1 is 1st host
## Returns:
##	hostname from CFG_HOSTS, might not be the host_basename

	global env

	return [ lindex $env(CFG_HOSTS) [ expr $number - 1] ]
}

proc same_host { hostname1 hostname2 } {
##
## same_host
## -------------------
## determine if hostnames are equivalent basenames
##
## Usage:
##	same_host hostname1 hostname2
## Arguments:
##	hostname1 - host name used by test driver (could be an
##			 $env(CFG_IPOIB_SUFFIX) or $env(CFG_IPOIB_SUFFIX) name)
##	hostname2 - host name used by test driver (could be an
##			 $env(CFG_IPOIB_SUFFIX) or $env(CFG_IPOIB_SUFFIX) name)
## Returns:
##	1 - names are same
##	0 - names are different

	return [ expr {"[ host_basename $hostname1 ]" == "[ host_basename $hostname2 ]"} ]
}

proc get_hostnumber { name } {
##
## get_hostnumber
## -------------------
## return host number for host in CFG_HOSTS
##
## Usage:
##	get_hostnumber name
## Arguments:
##	name - host name
## Returns:
##	host number in CFG_HOSTS, 1 is 1st host
##	0 - if name not in CFG_HOSTS

	global env

	set number 1
	foreach host $env(CFG_HOSTS) {
		if { [same_host $name $host ] } {
			return $number
		}
		incr number
	}

	return 0
}

proc have_other_hosts { hostname desthosts } {
##
## have_other_hosts
## -------------------
## determine if desthosts includes any others other than hostname
##
## Usage:
##	have_other_hosts hostname desthosts
## Arguments:
##	hostname - host name used by test driver (could be an $env(CFG_IPOIB_SUFFIX) or $env(CFG_IPOIB_SUFFIX) name)
##	desthosts - list of hosts to check
## Returns:
##	1 - there is at least 1 host other than hostname in desthosts
##	0 - desthosts has no hosts other than hostname

	foreach host $desthosts {
		if { ! [ same_host $hostname $host ] } {
			return 1
		}
	}
	return 0
}

proc get_local_stack_type { {checkrun "y"}} {
##
## get_local_stack_type
## --------------------
## get IB stack type on this host
##
## Arguments:
##	checkrun - if y, the running stack is checked first, otherwise
##		just installed stack is checked.  Note that during reinstall
##		an old stack could be left running until the next reboot
##		default is y.  Beware, due to newer distros including openib, if it
##		appears both are installed and 'n' is used, this reports IBACCESS.
##		It is safest to invoke this with 'y'.
##  
	global spawn_id expect_out spawn_out timeout
	global env
	global log_disable
	global expecting

	# disable detailed logging to stdout
	log_user 0
	set log_disable 1

	set hostname [ host_basename [exec hostname | cut -f1 -d.]]
	local_sh
	set target_stack [target_get_stack_type "$checkrun" "$hostname"]
	local_sh_exit
	return "$target_stack"
}

proc add_module_parameter { module_name module_parameter value {conffile ""} } {
##
## add_module_parameter
## --------------------
## edit modules.conf to add the specified parameter
##
## Arguments:
##  module_name - (i.e ics_sdp)
##  module_parameter - parameter (i.e. NoTcpFilter)
##  value - literal value
##  conffile - existing file within /etc/modprobe.d on RHEL6 and newer distros
##  
	# determine correct file name
	if { ! [string equal "$conffile" "" ] && [ catch { unix_cmd 60 0 "[ -e /etc/modprobe.d/$conffile ]" } res ] == 0 } {
		set mconf "/etc/modprobe.d/$conffile"
	} else {
		set mconf "/etc/modprobe.conf"
	}
   	send_unix_cmd "cat > /tmp/add.txt <<!
/^#.*options $module_name/ \{
        s/#.*options $module_name/options $module_name $module_parameter=$value/;
        ba;
\}
/^options $module_name/s/ $module_parameter=\[^ \]//;s/options $module_name/options $module_name $module_parameter=$value/
:a
!
"
	check_exit_status 60 0
	unix_cmd 60 0 "sed -f /tmp/add.txt $mconf > $mconf-new && mv -f $mconf-new $mconf && rm -f /tmp/add.txt"
}

proc remove_module_parameter { module_name module_parameter {conffile ""}} {
##
## remove_module_parameter
## --------------------
## edit modules.conf to remove the specified parameter
## Arguments:
##  module_name - (i.e ics_sdp)
##  module_parameter - parameter (i.e. NoTcpFilter)
##  parameter - literal value
##  conffile - existing file within /etc/modprobe.d on RHEL6 and newer distros
##  
	# determine correct file name
	if { ! [string equal "$conffile" "" ] && [ catch { unix_cmd 60 0 "[ -e /etc/modprobe.d/$conffile ]" } res ] == 0 } {
		set mconf "/etc/modprobe.d/$conffile"
	} elseif { [ catch { unix_cmd 60 0 {[ -e /etc/modprobe.conf ]} } res ] == 0 } {
		set mconf "/etc/modprobe.conf"
	} else {
		# parameter is not in a place we added it, do nothing
		return
	}
   	send_unix_cmd "cat > /tmp/remove.txt <<!
/^options $module_name/s/ $module_parameter=\[^ \]//;s/^options $module_name$/# options $module_name/
!
"
	check_exit_status 60 0
	unix_cmd 60 0 "sed -f /tmp/remove.txt $mconf > $mconf-new && mv -f $mconf-new $mconf && rm -f /tmp/add.txt"
}

proc scp_local_file { file_name host {directory "."} } {
##
## scp_local_file
## -------------------
## scp a file from local_sh to remote host/directory
## Arguments:
##  file_name - local file name to scp to remote host
##  host - remote host to scp file to
##  optional: directory - the remote directory to scp to (should end in '/.')
    global spawn_id

    set save_spawn_id $spawn_id
    local_sh
    send_unix_cmd "scp $file_name root@$host:$directory"
#    catch {expect_list 10 {"password:"} {"command not found"}  out}
#    send_unix_cmd "thepassword"
    expect_unix_prompt 20
    local_sh_exit
    set spawn_id $save_spawn_id
}