{"id":1029,"date":"2017-01-19T15:07:31","date_gmt":"2017-01-19T21:07:31","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1029"},"modified":"2017-01-19T15:07:31","modified_gmt":"2017-01-19T21:07:31","slug":"expect-and-bash","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/expect-and-bash\/","title":{"rendered":"Expect and bash"},"content":{"rendered":"
Quick example of spawning a problem that have a password prompt and can't accept a documented parameter for a password. I used another bash script to simulate a password prompt but in my automation challenge it was an executable that prompted.<\/p>\n
Main script just to take a password for passing to expect. Could also be hard coded.<\/p>\n
$ cat expect_example_main.sh
\n#!\/bin\/bash
\necho \"Enter the password: \"
\nread -s -e password
\n.\/expect_example.exp $password ;<\/p>\n
Here is the expect script that will be interacting with the real program with the password prompt.<\/p>\n
$ cat expect_example.exp
\n#!\/usr\/bin\/expect -f<\/p>\n
# Set variables
\nset password [lindex $argv 0]
\nset date [exec date +%F]<\/p>\n
# Log results
\nlog_file -a expect-$date.log<\/p>\n
# Announce device & time
\nsend_user \"\\n\"
\nsend_user \">>>>> Working @ [exec date] <<<<<\\n\"\nsend_user \"\\n\"\n\nspawn .\/expect_example_prompt.sh\nexpect \"*assword:\" {send \"$password\\r\"}\ninteract\n\nThis is the simulated executable with the prompt. Expect will be spawning this one.\n\n$ cat expect_example_prompt.sh \n#!\/bin\/bash\necho \"Enter the password: \"\nread -s -e pwd\nif [ $pwd == 'fool' ]; then\n echo \"password correct\"\nelse\n echo \"password NOT correct!\"\nfi\n\nShowing run time with correct password.\n\n$ .\/expect_example_main.sh \nEnter the password: \n\n>>>>> Working @ Thu Jan 19 14:54:00 CST 2017 <<<<<\n\nspawn .\/expect_example_prompt.sh\nEnter the password: \npassword correct\n\nShowing run time with incorrect password.\n\n$ .\/expect_example_main.sh \nEnter the password: \n\n>>>>> Working @ Thu Jan 19 14:54:28 CST 2017 <<<<<\n\nspawn .\/expect_example_prompt.sh\nEnter the password: \npassword NOT correct!\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"
Quick example of spawning a problem that have a password prompt and can’t accept a documented parameter for a password.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,72],"tags":[],"class_list":["post-1029","post","type-post","status-publish","format-standard","hentry","category-bash","category-expect"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1029"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1029\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}