linux - How to return from shell 'read' command passed in expect script? -
i new using expect, , bit confused regarding passing commands expect script, please bear me... have searched numerous forums, cannot seem find example of expect script uses read command user input.
in korn shell script, call expect script (expectssh.exp) ssh login host , user input regarding host's network configuration (network interface card number , subnet mask information). pass 4 arguments expect script: remote host ip address, username, password, , list of commands run. expect script below:
#!/usr/bin/expect # usage: expectssh <host> <ssh user> <ssh password> <script> set timeout 60 set prompt "(%|#|\\$) $" set commands [lindex $argv 3]; spawn ssh [lindex $argv 1]@[lindex $argv 0] expect { "*assword:" { send -- "[lindex $argv 2]\r" expect -re "$prompt" send -- "$commands\r" } "you sure want continue connecting" { send -- "yes\r" expect "*assword:" send -- "[lindex $argv 2]\r" expect -re "$prompt" send -- "$commands\r" } timeout { exit } }
the script runs well, except when gets 'read' command, script not continue or exit after user presses enter. hangs.
the commands pass expect script , call follows:
script='hostname > response.txt;netstat -rn;read net_card?"what network interface card number? " >> response.txt; read net_mask?"what subnet mask? " >> response.txt' /usr/bin/expect ./expectssh.exp $hostip $usr $pswd "$script"
any suggestions on how can pass read command through expect script without hanging?
on side note because know come - not allowed key-based automatic ssh login. have prompt username , password, done korn shell script calls expect script.
thanks suggestions , can provide!
for interested, able read
command work user input doing few things:
(1) putting within -re $prompt
block instead of appending send -- "$commands\r"
after password entry.
(2) hard coding commands script rather passing them in.
(3) following command interact
statement next send command isn't entered before user responds.
my expect block looks this:
expect { -re "(.*)assword:" { send -s "$pswd\r" exp_continue } "denied, please try again" { send_user "invalid password or account.\n" exit 5 } "incorrect" { send_user "invalid password or account.\n" exit 5 } "you sure want continue connecting" { send -s "yes\r" exp_continue } -re $prompt { set timeout -1 send -- "hostname > partnerinit\r" expect -exact "hostname > partnerinit\r" send -s "netstat -rn\r" expect -re "$prompt" send -- "read n_card?'enter network interface card number server (i.e. eth0): '\r" interact "\r" return send -- "\r" send -- "echo \$n_card >> partnerinit\r" send -- "msk=\$(cat /etc/sysconfig/network-scripts/ifcfg-\$n_card | grep netmask)\r" send -- "msk=\$(echo \${msk#netmask=})\r" send -- "echo \$msk >> partnerinit\r" send -- "cat partnerinit\r" set retval 0 } timeout { send_user "connection host $host timed out.\n" exit 10 } eof { send_user "connection host $host failed.\n" exit 1 } }
i updated script automatically determine subnet mask based on network interface card number entered user. brought attention finding network interface card number difficult automate in case of box has multiple interface cards. better start small , have user enter , fine-tune/automate later once overall script working.
now i'm working on modifying scp partnerinit file local host , return meaningful exit statuses each of expect conditions.
Comments
Post a Comment