Cześć.
Wykonałem sobie skrypt w shellu, który potrzebuje kilku logowań na inne serwery. Chciałbym dlatego skorzystać z narzędzia "expect", by móc podać hasło tylko 1 raz, a potem nieinteraktywnie podawać je przez pomocy "expect" podczas logowania przez ssh / uaktualnienia svn, itp.
Oto wynik działania "autoexpect":
`#!/usr/bin/expect -f
- This Expect script was generated by autoexpect on Fri Jul 1 0806 2016
- Expect and autoexpect were both written by Don Libes, NIST.
- ...
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "srvc15:/home/usr/scripts/Tools/da $ "
send -- "ssh -q i7c9fy3@srvc16"
expect -exact "ssh -q i7c9fy3@srvc16"
send -- "\r"
expect -exact "\r
i7c9fy3@srvc16's password: "
send -- ".my.pass.\r"
expect -exact "\r
Last login: Fri Jul 1 0821 2016 from srvc15\r
\r
\r
\r
Hostname: srvc16\r
Operating system: Linux-RedHat-RHEL-5\r
Additional infos: \r
*******************************************************************************\r
\r
[r[999;999H[6n"
send -- "[40;119R"
srvc16:/home/i7c9fy3 $ "
send -- "exit\r"
expect -exact "exit\r
logout\r
[H[2Jsrvc15:/home/usr/scripts/Tools/da $ "
send -- ""
expect -exact "Use "exit" to leave the shell.\r
srvc15:/home/usr/scripts/Tools/da $ "
send -- ""
expect eof`
Wiecie może, co to za dziwne "kody": [r[999;999H[6n?
Wiecie też może, po co wysyłany jest poniższy ciąg?
send -- "[40;119R"
Mój skrypt 'expect' wygląda tak:
#!/usr/bin/expect -f
set user [lrange $argv 0 0]
set server [lrange $argv 1 1]
set ssap [lrange $argv 2 2]
set timeout 3
spawn ssh -q -o NumberOfPasswordPrompts=1 $user@$server
expect {
timeout { send_user "\nSSH timeout\n"; exit 1 }
eof { send_user "\nSSH eof\n"; exit 2 }
-re "\(yes/no\)" { send -- "yes\r" }
"assword:" { send -- "$ssap\r" }
}
#send -- "R"
expect {
timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 3 }
eof { send_user "\nPassword eof\n"; exit 4 }
-re "999H" {
send_user " --- expect_out = $expect_out(0,string) --- "
send_user "\nFound 999H => sending /r...\n"
sleep 1
send_user "1s sleeped\n"
send -- ";96R"
exp_continue
}
-re "/home/.*\$" { send_user "\nIs DOLLAR = prompt\n" }
}
send_user " - - expect_out = $expect_out(0,string) - - "
send_user "\n#yoU arE loggeD iN#\n"
send "exit 77\r"
#expect eof
close $spawn_id
Po wywołaniu login.exp i7c9fy3 srvc16 .my.pass.
jest takie coś:
srvc16:/home/usr/scripts/Tools/da $ ./login.exp i7c9fy3 srvc16 .my.pass.
spawn ssh -q -o NumberOfPasswordPrompts=1 i7c9fy3@srvc16
i7c9fy3@srvc16's password:
Last login: Fri Jul 1 08:20:33 2016 from srvc15
Hostname: srvc16
Operating system: Linux-RedHat-RHEL-5
Additional infos:
*******************************************************************************
--- expect_out = 999H ---
Found 999H => sending /r...
^[[40;119R1s sleeped
srvc16:/home/i7c9fy3 $
Is DOLLAR = prompt
- - expect_out = :/home/i7c9fy3 $ - -
#yoU arE loggeD iN#
srvc15:/home/usr/scripts/Tools/da $ ;119R
Przed pojawieniem się 1s sleeped srvc16:/home/i7c9fy3 $
jest sek. przerwy (oczywiście). Dlaczego jednak po zalogowaniu pojawia się dodatkowo ;119R
?
Bardzo proszę o pomoc.