expect script to ssh returns invalid command name -
i trying write expect script ssh server, send sudo su, check iptables status , put output in log file on server. below script.
1 #!/usr/bin/expect 2 exp_internal 1 3 log_user 0 4 set timeout 10 5 set password "******" 6 7 spawn /usr/bin/ssh -l subhasish *.*.*.* -p 10022 8 9 expect { 10 -re "password: " {send "$password\r"} 11 -re "$ " {send "sudo su\r"} 12 -re "[sudo] password subhasish:" {send "$password\r"} 13 -re "# " {send "service iptables status\r"} 14 } 15 set output $expect_out(buffer) 16 send "exit\r" 17 puts "$output\r\n" >> output.log
but while run in debug mode, getting error this;
expect -d testcase expect version 5.44.1.15 argv[0] = expect argv[1] = -d argv[2] = testcase set argc 0 set argv0 "testcase" set argv "" executing commands command file testcase parent: waiting sync byte parent: telling child go ahead parent: unsynchronized child spawn: returns {24105} invalid command name "sudo" while executing "sudo" invoked within "expect { -re "password: " {send "$password\r"} -re "$ " {send "sudo su\r"} -re "[sudo] password subhasish:" {send "$password\r"} ..." (file "testcase" line 9)
not sure going wrong. says invalid command name "sudo", guess because expect doesn;t understand these command. how go around it. please help. thanks.
the problem in line
-re "[sudo] password subhasish:" {send "$password\r"}
in tcl (and hence in expect) square brackets syntax command substitution (like backticks in shell). either need escape brackets or use different quotes prevent various expansions:
-re {[sudo] password subhasish:} {send "$password\r"}
that brings different issue: expecting see these exact characters? because you're instructing expect treat regular expression, , square brackets in regular expression means character class, match single character, either 's', 'u', 'd' or 'o'. need this:
-re {\[sudo\] password subhasish:} {send "$password\r"}
or
-ex {[sudo] password subhasish:} {send "$password\r"}