{"id":156478,"date":"2019-09-07T09:22:36","date_gmt":"2019-09-07T01:22:36","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=156478"},"modified":"2019-08-26T09:23:45","modified_gmt":"2019-08-26T01:23:45","slug":"linux-expect-auto","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-expect-auto.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u5728Linux\u4e2d\u901a\u8fc7expect\u5de5\u5177\u5b9e\u73b0\u811a\u672c\u7684\u81ea\u52a8\u4ea4\u4e92"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nexpect\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\u884c\u4ea4\u4e92\u901a\u4fe1. <\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1 \u5b89\u88c5expect\u5de5\u5177<\/strong><\/div>\n

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\u884c\u4ea4\u4e92\u901a\u4fe1. \u5176\u4ea4\u4e92\u6d41\u7a0b\u662f:<\/p>\n

spawn\u542f\u52a8\u6307\u5b9a\u8fdb\u7a0b -> expect\u83b7\u53d6\u6307\u5b9a\u5173\u952e\u5b57 -> send\u60f3\u6307\u5b9a\u8fdb\u7a0b\u53d1\u9001\u6307\u5b9a\u6307\u4ee4 -> \u6267\u884c\u5b8c\u6210, \u9000\u51fa.<\/strong><\/pre>\n

\u7531\u4e8eexpect\u662f\u57fa\u4e8etcl\u7684, \u6240\u4ee5\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86tcl:<\/p>\n

# \u68c0\u67e5\u662f\u5426\u5b89\u88c5\u4e86tcl:\r\n[root@localhost ~]# whereis tcl\r\ntcl: \/usr\/lib64\/tcl8.5 \/usr\/include\/tcl.h \/usr\/share\/tcl8.5\r\n\r\n# \u5982\u679c\u6ca1\u6709\u5b89\u88c5, \u4f7f\u7528yum\u5b89\u88c5tcl\u548cexpect:\r\n[root@localhost ~]# yum install -y tcl\r\n[root@localhost ~]# yum install -y expect\r\n\r\n# \u67e5\u770bexpect\u7684\u5b89\u88c5\u8def\u5f84:\r\n[root@localhost ~]# command -v expect\r\n\/usr\/bin\/expect<\/pre>\n
2 expect\u7684\u5e38\u7528\u547d\u4ee4<\/strong><\/div>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u547d \u4ee4<\/th>\n\u8bf4 \u660e<\/th>\n<\/tr>\n<\/thead>\n
spawn<\/td>\n\u542f\u52a8\u65b0\u7684\u4ea4\u4e92\u8fdb\u7a0b, \u540e\u9762\u8ddf\u547d\u4ee4\u6216\u8005\u6307\u5b9a\u7a0b\u5e8f<\/td>\n<\/tr>\n
expect<\/td>\n\u4ece\u8fdb\u7a0b\u4e2d\u63a5\u6536\u4fe1\u606f, \u5982\u679c\u5339\u914d\u6210\u529f, \u5c31\u6267\u884cexpect\u540e\u7684\u52a8\u4f5c<\/td>\n<\/tr>\n
send<\/td>\n\u5411\u8fdb\u7a0b\u53d1\u9001\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n
send exp_send<\/td>\n\u7528\u4e8e\u53d1\u9001\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<\/td>\n<\/tr>\n
exp_continue<\/td>\n\u5728expect\u4e2d\u591a\u6b21\u5339\u914d\u5c31\u9700\u8981\u7528\u5230<\/td>\n<\/tr>\n
send_user<\/td>\n\u7528\u6765\u6253\u5370\u8f93\u51fa \u76f8\u5f53\u4e8eshell\u4e2d\u7684echo<\/td>\n<\/tr>\n
interact<\/td>\n\u5141\u8bb8\u7528\u6237\u4ea4\u4e92<\/td>\n<\/tr>\n
exit<\/td>\n\u9000\u51faexpect\u811a\u672c<\/td>\n<\/tr>\n
eof<\/td>\nexpect\u6267\u884c\u7ed3\u675f, \u9000\u51fa<\/td>\n<\/tr>\n
set<\/td>\n\u5b9a\u4e49\u53d8\u91cf<\/td>\n<\/tr>\n
puts<\/td>\n\u8f93\u51fa\u53d8\u91cf<\/td>\n<\/tr>\n
set timeout<\/td>\n\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
3 \u4f5c\u7528\u539f\u7406\u7b80\u4ecb<\/strong><\/div>\n
3.1 \u793a\u4f8b\u811a\u672c<\/strong><\/span><\/div>\n

\u8fd9\u91cc\u4ee5ssh\u8fdc\u7a0b\u767b\u5f55\u67d0\u53f0\u670d\u52a1\u5668\u7684\u811a\u672c\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e, \u5047\u8bbe\u6b64\u811a\u672c\u540d\u79f0\u4e3aremote_login.sh:<\/p>\n

#!\/usr\/bin\/expect\r\n\r\nset timeout 30\r\nspawn ssh -l root 172.16.22.131\r\nexpect \"password*\"\r\nsend \"123456\\r\"\r\ninteract<\/pre>\n
3.2 \u811a\u672c\u529f\u80fd\u89e3\u8bfb<\/strong><\/span><\/div>\n
(1) #!\/usr\/bin\/expect<\/pre>\n

\u4e0a\u8ff0\u5185\u5bb9\u5fc5\u987b\u4f4d\u4e8e\u811a\u672c\u6587\u4ef6\u7684\u7b2c\u4e00\u884c, \u7528\u6765\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf, \u6b64\u811a\u672c\u9700\u8981\u4f7f\u7528\u7cfb\u7edf\u7684\u54ea\u4e2a\u811a\u672c\u89e3\u6790\u5f15\u64ce\u6765\u6267\u884c.<\/p>\n

\u5177\u4f53\u8def\u5f84\u53ef\u901a\u8fc7command -v expect\u547d\u4ee4\u67e5\u770b.<\/p>\n

\u6ce8\u610f:<\/p>\n

\u8fd9\u91cc\u7684expect\u548cLinux\u7684bash\u3001Windows\u7684cmd\u7b49\u7a0b\u5e8f\u4e00\u6837, \u90fd\u662f\u4e00\u79cd\u811a\u672c\u6267\u884c\u5f15\u64ce.<\/p>\n

\u811a\u672c\u9700\u8981\u6709\u53ef\u6267\u884c\u6743\u9650(chmod +x remote_login.sh, \u6216chmod 755 auto_login.sh), \u7136\u540e\u901a\u8fc7\u547d\u4ee4.\/remote_login.sh\u8fd0\u884c\u5373\u53ef;<\/p>\n

\u5982\u679c\u8f93\u5165sh remote_login.sh, \u610f\u4e49\u5c31\u4e0d\u4e00\u6837\u4e86: \u660e\u786e\u8c03\u7528sh\u5f15\u64ce\u53bb\u6267\u884c\u6b64\u811a\u672c, \u6b64\u65f6\u9996\u884c\u7684#!\/usr\/bin\/expect\u5c31\u5931\u6548\u4e86.<\/p><\/blockquote>\n

(2) set timeout 30<\/pre>\n

\u8bbe\u7f6e\u8fde\u63a5\u7684\u8d85\u65f6\u65f6\u95f4\u4e3a30\u79d2.<\/p>\n

(3) spawn ssh -l root 172.16.22.131<\/p>\n

spawn\u3001send\u7b49\u547d\u4ee4\u662fexpect\u5de5\u5177\u4e2d\u7684\u5185\u90e8\u547d\u4ee4, \u5982\u679c\u6ca1\u6709\u5b89\u88c5expect\u5de5\u5177, \u5c31\u4f1a\u51fa\u73b0\"spawn not found\"\u7b49\u9519\u8bef.<\/p>\n

\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>\n

expect\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>\n
4.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>\n
4.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>\n
5 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}]}}