\u4e0d\u8981\u7528which spawn\u4e4b\u7c7b\u7684\u547d\u4ee4\u53bb\u627espawn, \u56e0\u4e3a\u5e76\u6ca1\u6709\u8fd9\u6837\u7684\u7a0b\u5e8f.<\/p><\/blockquote>\n
(4) expect \"password*\"<\/pre>\n\u8fd9\u4e2a\u547d\u4ee4\u7528\u6765\u5224\u65ad\u4e0a\u6b21\u8f93\u51fa\u7ed3\u679c\u91cc\u662f\u5426\u5305\u542b\"password*\"\u7684\u5b57\u7b26\u4e32, \u5982\u679c\u6709\u5219\u7acb\u5373\u8fd4\u56de, \u5426\u5219\u5c31\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u8fd4\u56de. \u8fd9\u91cc\u7684\u7b49\u5f85\u65f6\u957f\u5c31\u662f\u524d\u9762\u8bbe\u7f6e\u7684timeout, \u4e5f\u5c31\u662f30\u79d2.<\/p>\n
(5) send \"123456\\r\"<\/pre>\n\u8fd9\u91cc\u5c31\u662f\u6267\u884c\u4ea4\u4e92\u52a8\u4f5c, \u4f5c\u7528\u7b49\u540c\u4e8e\u624b\u5de5\u8f93\u5165\u5bc6\u7801.<\/p>\n
\u63d0\u793a: \u547d\u4ee4\u5b57\u7b26\u4e32\u7ed3\u5c3e\u52a0\u4e0a\\r, \u8fd9\u6837\u7684\u8bdd, \u5982\u679c\u51fa\u73b0\u5f02\u5e38\u7b49\u5f85\u7684\u72b6\u6001\u5c31\u80fd\u591f\u505c\u7559\u4e0b\u6765, \u4f5c\u8fdb\u4e00\u6b65\u7684\u6838\u67e5.<\/p>\n
(6) interact<\/pre>\nexpect\u6267\u884c\u5b8c\u6210\u540e\u4fdd\u6301\u7528\u6237\u7684\u4ea4\u4e92\u72b6\u6001, \u8fd9\u4e2a\u65f6\u5019\u7528\u6237\u5c31\u53ef\u4ee5\u624b\u5de5\u64cd\u4f5c\u4e86.<\/p>\n
\u5982\u679c\u6ca1\u6709\u8fd9\u4e00\u53e5, expect\u6267\u884c\u5b8c\u6210\u540e\u5c31\u4f1a\u9000\u51fa\u811a\u672c\u521a\u521a\u8fdc\u7a0b\u767b\u5f55\u8fc7\u53bb\u7684\u7ec8\u7aef, \u7528\u6237\u4e5f\u5c31\u4e0d\u80fd\u7ee7\u7eed\u64cd\u4f5c\u4e86.<\/p>\n
4 \u5176\u4ed6\u811a\u672c\u4f7f\u7528\u793a\u4f8b<\/strong><\/div>\n4.1 \u76f4\u63a5\u901a\u8fc7expect\u6267\u884c\u591a\u6761\u547d\u4ee4<\/strong><\/span><\/div>\n\u6ce8\u610f\u9996\u884c\u5185\u5bb9, \u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u53ea\u80fd\u901a\u8fc7.\/script.sh\u6765\u6267\u884c\u8fd9\u7c7b\u811a\u672c\u4e86:<\/p>\n
#!\/usr\/bin\/expect -f\r\n\r\nset timeout 10\r\n# \u5207\u6362\u5230root\u7528\u6237, \u7136\u540e\u6267\u884cls\u548cdf\u547d\u4ee4:\r\nspawn su - root\r\nexpect \"Password*\"\r\nsend \"123456\\r\"\r\nexpect \"]*\" # \u901a\u914d\u7b26\r\nsend \"ls\\r\"\r\nexpect \"#*\" # \u901a\u914d\u7b26\u7684\u53e6\u4e00\u79cd\u5f62\u5f0f\r\nsend \"df -Th\\r\"\r\nsend \"exit\\r\" # \u9000\u51faspawn\u5f00\u542f\u7684\u8fdb\u7a0b\r\n\r\nexpect eof # \u9000\u51fa\u6b64expect\u4ea4\u4e92\u7a0b\u5e8f<\/pre>\n4.2 \u901a\u8fc7shell\u8c03\u7528expect\u6267\u884c\u591a\u6761\u547d\u4ee4<\/strong><\/span><\/div>\n\u6ce8\u610f\u9996\u884c\u5185\u5bb9, \u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u901a\u8fc7sh script.sh\u3001bash script.sh \u6216.\/script.sh, \u90fd\u53ef\u4ee5\u6267\u884c\u8fd9\u7c7b\u811a\u672c:<\/p>\n
#!\/bin\/bash\r\n\r\nip=\"172.16.22.131\"\r\nusername=\"root\"\r\npassword=\"123456\"\r\n\r\n# \u6307\u5b9a\u6267\u884c\u5f15\u64ce\r\n\/usr\/bin\/expect <<EOF\r\n set time 30\r\n spawn ssh $username@$ip df -Th\r\n expect {\r\n \"*yes\/no\" { send \"yes\\r\"; exp_continue }\r\n \"*password:\" { send \"$password\\r\" }\r\n }\r\n expect eof\r\nEOF<\/pre>\n5 spawn not found \u7684\u89e3\u51b3<\/strong><\/div>\n\u51fa\u73b0\u8fd9\u4e2a\u9519\u8bef\u7684\u57fa\u672c\u4e0a\u90fd\u662f\u51fa\u5b66\u8005: Linux \u6267\u884cshell\u811a\u672c\u6709\u4e24\u79cd\u65b9\u5f0f:<\/p>\n
\u4e00\u79cd\u662f\u5c06\u811a\u672c\u4f5c\u4e3ash\u7684\u547d\u4ee4\u884c\u53c2\u6570, \u5982sh remote_login.sh, \u6216sh \/data\/remote_login.sh;<\/p>\n
\u4e00\u79cd\u662f\u5c06\u811a\u672c\u4f5c\u4e3a\u5177\u6709\u6267\u884c\u6743\u9650\u7684\u53ef\u6267\u884c\u811a\u672c, \u5982.\/remote_login.sh, \u6216\/data\/remote_login.sh.<\/p><\/blockquote>\n
\u800c\u4f5c\u4e3ash\u547d\u4ee4\u884c\u53c2\u6570\u6765\u8fd0\u884c, \u90a3\u4e48\u811a\u672c\u7b2c\u4e00\u884c\u7684#!\/usr\/bin\/expect\u5c31\u4f1a\u5931\u6548, \u6240\u4ee5\u624d\u4f1a\u51fa\u73b0spawn not found\u3001send not found\u7b49\u9519\u8bef, \u6240\u6709\u4e0a\u9762\u7684automate_login.sh\u811a\u672c\u5fc5\u987b\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c:<\/p>\n
.\/automate_expect.sh<\/pre>\n","protected":false},"excerpt":{"rendered":"expect\u662f\u5efa\u7acb\u5728tcl\u57fa\u7840\u4e0a\u7684\u4e00\u4e2a\u81ea\u52a8\u5316\u4ea4\u4e92\u5957\u4ef6, \u5728\u4e00\u4e9b\u9700\u8981\u4ea4\u4e92\u8f93\u5165\u6307\u4ee4\u7684\u573a\u666f\u4e0b, \u53ef\u901a\u8fc7\u811a\u672c\u8bbe\u7f6e\u81ea\u52a8\u8fdb […]<\/p>\n","protected":false},"author":63,"featured_media":156482,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-156478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=156478"}],"version-history":[{"count":9,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156478\/revisions"}],"predecessor-version":[{"id":157073,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156478\/revisions\/157073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/156482"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=156478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=156478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=156478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}