{"id":294804,"date":"2024-12-06T04:07:53","date_gmt":"2024-12-05T20:07:53","guid":{"rendered":"https:\/\/gulass.cn\/srjxegjjytdz.html"},"modified":"2024-12-06T04:07:53","modified_gmt":"2024-12-05T20:07:53","slug":"srjxegjjytdz","status":"publish","type":"post","link":"https:\/\/gulass.cn\/srjxegjjytdz.html","title":{"rendered":"\u6df1\u5165\u89e3\u6790Expect\u5de5\u5177\uff1a\u57fa\u4e8eTcl\u7684\u81ea\u52a8\u5316\u4ea4\u4e92\u4efb\u52a1\u5b9e\u73b0\u6307\u5357"},"content":{"rendered":"

expect\u662f\u6784\u5efa\u5728tcl\u57fa\u7840\u4e0a\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u5b83\u62ff\u6765\u8ba9\u4e00\u4e9b\u987b\u8981\u4ea4\u4e92\u7684\u4efb\u52a1\u624b\u52a8\u5316\u5730\u5b8c\u6210\u3002<\/p>\n

\u7531\u4e8eexpect\u662f\u57fa\u4e8etcl\u7684linux\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u6240\u4ee5\u987b\u8981\u4f60\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u6709tcl<\/p>\n

\u7528\u6cd51.\uff3b#!\/usr\/bin\/expect\uff3d<\/p>\n

\u8fd9\u4e00\u884c\u544a\u8bc9\u64cd\u4f5c\u7cfb\u7edf\u811a\u672c\u91cc\u7684\u4ee3\u7801\u4f7f\u7528\u90a3\u4e00\u4e2ashell\u6765\u6267\u884c\u3002\u8fd9\u513f\u7684expect\u4f3c\u4e4e\u548clinux\u4e0b\u7684bash\u3001windows\u4e0b\u7684cmd\u662f\u4e00\u7c7b\u4e1c\u897f\u3002<\/p>\n

***\u6ce8\u610f\uff1a\u8fd9\u4e00\u884c\u987b\u8981\u5728\u811a\u672c\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n

2.\uff3bsettimeout30\uff3d<\/p>\n

\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u8ba1\u65f6\u5355\u4f4d\uff1a\u79d2\u3002timeout-1\u4e3a\u6c38\u4e0d\u8d85\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ctimeout\u662f10\u79d2\u3002<\/p>\n

3.\uff3bspawnssh-lusername192.168.1.1\uff3d<\/p>\n

spawn\u662f\u6b65\u5165expect\u73af\u5883\u540e\u624d\u53ef\u4ee5\u6267\u884c\u7684expect\u5185\u90e8\u547d\u4ee4<\/a>\uff0c\u5047\u5982\u6ca1\u6709\u88c5expect\u6216\u5219\u76f4\u63a5\u5728\u9ed8\u8ba4\u7684SHELL\u4e0b\u6267\u884c\u662f\u627e\u4e0d\u5230spawn\u547d\u4ee4\u7684\u3002\u6240\u4ee5\u4e0d\u8981\u7528\u201cwhichspawn\u201c\u4e4b\u7c7b\u7684\u547d\u4ee4\u53bb\u627espawn\u547d\u4ee4\u3002<\/p>\n

\u4e3b\u8981\u529f\u80fd\u662f\u62ff\u6765\u5f00\u59cb\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u3002<\/p>\n

spawn\u524d\u9762\u52a0\u4e0a\u987b\u8981\u6267\u884c\u7684shell\u547d\u4ee4\uff0c\u4f8b\u5982\u8bf4spawnsudotouchtestfile.<\/p>\n

4.\uff3bexpect\u201cpassword:\u201d\uff3d<\/p>\n

\u8fd9\u513f\u7684expect\u4e5f\u662fexpect\u7684\u4e00\u4e2a\u5185\u90e8\u547d\u4ee4\uff0c\u529f\u80fd\u662f\u5224\u5b9a\u5148\u524d\u8f93\u51fa\u7ed3\u679c\u91cc\u662f\u5426\u5305\u542b\u201cpassword:\u201d\u7684\u5b57\u7b26\u4e32\uff0c\u5047\u5982\u6709\u5219\u7acb\u523b\u8fd4\u56de\uff0c\u5426\u5219\u5c31\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u8fd4\u56de\uff0c\u8fd9\u513f\u7b49\u5f85\u65f6\u957f\u901a\u8fc7settimeout\u6765\u8bbe\u7f6e\u3002<\/p>\n

5.\uff3bsend\u201cispassr\u201d\uff3d<\/p>\n

\u8fd9\u513f\u5c31\u662f\u6267\u884c\u4ea4\u4e92\u52a8\u4f5c\uff0c\u4e0e\u624b\u5de5\u8f93\u5165\u5bc6\u7801\u7684\u52a8\u4f5c\u7b49\u6548\u3002<\/p>\n

***\u6e29\u60c5\u63d0\u793a\uff1a\u547d\u4ee4\u5b57\u7b26\u4e32\u7ed3\u5c3e\u522b\u5fd8\u6389\u52a0\u4e0a\u201cr\u201d\uff0c\u5018\u82e5\u51fa\u73b0\u5f02\u5e38\u7b49\u5f85\u7684\u72b6\u6001\u53ef\u4ee5\u6838\u67e5\u4e00\u4e0b\u3002<\/p>\n

6.\uff3binteract\uff3d<\/p>\n

\u6267\u884c\u5b8c\u6210\u540e\u4fdd\u6301\u4ea4\u4e92\u72b6\u6001\uff0c\u628a\u63a7\u5236\u6743\u4ea4\u7ed9\u63a7\u5236\u53f0\uff0c\u8fd9\u4e2a\u65f6\u4faf\u5c31\u53ef\u4ee5\u624b\u5de5\u64cd\u4f5c\u4e86\u3002\u5047\u5982\u6ca1\u6709\u8fd9\u4e00\u53e5\u767b\u9646\u5b8c\u6210\u540e\u4f1a\u9000\u51fa\uff0c\u800c\u4e0d\u662f\u7559\u5728\u8fdc\u7a0b\u7ec8\u7aef\u4e0a\u3002<\/p>\n

7.[expecteof]<\/p>\n

\u4e0espawn\u5bf9\u5e94\uff0c\u8868\u793a\u6355\u6349\u7ec8\u7aef\u8f93\u51fa\u4fe1\u606f\u4e2d\u6b62\uff0c\u7ed3\u675f\u4ea4\u4e92\u3002<\/p>\n

8.send\u548csend_user<\/p>\n

send\u4f1a\u5c06expect\u811a\u672c\u4e2d\u987b\u8981\u7684\u4fe1\u606f\u53d1\u9001\u7ed9spawn\u542f\u52a8\u7684\u54ea\u4e2a\u8fdb\u7a0b\uff0c\u800csend_user\u53ea\u662f\u56de\u663e\u7528\u6237\u53d1\u51fa\u7684\u4fe1\u606f\uff0c\u7c7b\u4f3c\u4e8eshell\u4e2d\u7684echo\u800c\u5df2\u3002<\/p>\n

9.exp_continue\u7684\u7528\u6cd5<\/p>\n

\u4f7f\u7528exp_continue\u540e\uff0c\u4f1a\u91cd\u65b0\u4ece\u5f53\u524dexpect\u5757\u7684\u5f00\u59cb\u91cd\u65b0\u6267\u884c\uff0c\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3awhile\u5faa\u73af\u7684continue.<\/p>\n

\n

expect<\/span> {<\/span>\n      \u3000\u3000  -re \"Permission denied, please try again.\"<\/span> {<\/span>\n      \u3000\u3000   send_user \"Error:Permission denied.n\"<\/span>\n      \u3000\u3000    exit<\/span>\n      \u3000\u3000\u3000\u3000}<\/span>\n      \u3000\u3000\u3000-re \"Are you sure you want to continue connecting (yes\/no)?\"<\/span> {<\/span>\n      \u3000\u3000\u3000\u3000\u3000\u3000send \"yesr\"<\/span>;<\/span>exp_continue\n      \u3000\u3000\u3000\u3000\u3000 \u3000 }<\/span>\n      \u3000\u3000\u3000-re \"assword:\"<\/span> {<\/span>\n      \u3000\u3000\u3000send \"$loginpass<\/span>r\"<\/span>;<\/span>exp_continue\n        \u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000}<\/span>\n

linux set\u547d\u4ee4\u4f8b\u5b50_linux\u547d\u4ee4\u4f7f\u7528\u4f8b\u5b50_\u5217\u4e3e10\u4e2alinux\u547d\u4ee4<\/p>\n \u3000\u3000-re \"Connection refused\"<\/span> {<\/span>\n \u3000\u3000\t\t\texit<\/span>\n\t\t \u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 }<\/span>\n \ttimeout<\/span> {<\/span>\n \t\t exit<\/span>\n \t\t\t}<\/span>\n \teof {<\/span>\n \texit<\/span>\n \t\t\t}<\/span>\n\t\t}<\/span>\n<\/code><\/pre>\n<\/p>\n

\u4e8b\u4f8b#1<\/p>\n

viexpectExample.sh\u540e\u8f93\u5165\u4e0b\u8fb9\u7684\u811a\u672c\u5185\u5bb9\u4fdd\u5b58\u540e\uff0c\u4f7f\u7528chmod+xexpectExample.sh\u547d\u4ee4\u4e3a\u811a\u672c\u8d4b\u4e8e\u53ef\u6267\u884c\u6743\u9650\u3002<\/p>\n

\n

#!\/usr\/bin\/expect -f<\/span>\n#--- set the variable,you can modify the value<\/span>\nset<\/span> ipaddr [<\/span>lrange $argv<\/span> 0 0]<\/span>\nset<\/span> port [<\/span>lrange $argv<\/span> 1 1]<\/span>\nset<\/span> loginuser [<\/span>lrange $argv<\/span> 2 2]<\/span>\nset<\/span> loginpass [<\/span>lrange $argv<\/span> 3 3]<\/span>\nset<\/span> cmd_prompt \"]#|~]?\"<\/span>\n#\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\uff08\u79d2)<\/span>\nset<\/span> timeout<\/span> 3600\n#--- login by ssh<\/span>\nspawn ssh<\/span> -p $port<\/span> $loginuser@<\/span>$ipaddr<\/span>\nexpect<\/span> {<\/span>\n        -re \"Permission denied, please try again.\"<\/span> {<\/span>\n                send_user \"Error:Permission denied.n\"<\/span>\n                exit<\/span>\n        }<\/span>\n                -re \"Connection refused\"<\/span> {<\/span>\n                send_user \"Error:Connection refused.n\"<\/span>\n                exit<\/span>\n        }<\/span>\n        timeout<\/span> {<\/span>\n                exit<\/span>\n        }<\/span>\n        eof {<\/span>\n                exit<\/span>\n        }<\/span>\n        -re \"Are you sure you want to continue connecting (yes\/no)?\"<\/span> {<\/span>\n                send \"yesr\"<\/span>\n                exp_continue\n        }<\/span>\n        -re \"assword:\"<\/span> {<\/span>\n                send \"$loginpass<\/span>r\"<\/span>\n                exp_continue\n        }<\/span>\n        -re $cmd_prompt<\/span> {<\/span>\n                send \"r\"<\/span>\n        }<\/span>\n}<\/span>\n#--- now,we do some commands<\/span>\nexpect<\/span> {<\/span>\n        -re $cmd_prompt<\/span> {<\/span>\n                send \"df -hr\"<\/span>\n        }<\/span>\n}<\/span>\nexpect<\/span> {<\/span>\n        -re $cmd_prompt<\/span> {<\/span>\n                send \"free -mr\"<\/span>\n        }<\/span>\n}<\/span>\nexpect<\/span> {<\/span>\n        -re $cmd_prompt<\/span> {<\/span>\n                send \"uptimer\"<\/span>\n        }<\/span>\n}<\/span>\nsend \"cd \/optr\"<\/span>\nsend \"mkdir shanhy123r\"<\/span>\nsend \"cd shanhy123r\"<\/span>\nsend \"echo 123 > 123.txtr\"<\/span>\nsend \"\/opt\/xs.shr\"<\/span>\nsend \"exitr\"<\/span>\nexpect<\/span> eof\n#interact<\/span>\n<\/code><\/pre>\n<\/p>\n

linux set\u547d\u4ee4\u4f8b\u5b50_\u5217\u4e3e10\u4e2alinux\u547d\u4ee4_linux\u547d\u4ee4\u4f7f\u7528\u4f8b\u5b50<\/p>\n

\u5176\u4e2dxs.sh\u4e3a\u4e00\u4e2ashell\u811a\u672c\uff0c\u4e0a\u9762\u505a\u4e86\u4e00\u4e2asleep\u6a21\u62df\u5386\u65f6\u64cd\u4f5c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\n

#!\/bin\/bash<\/span>\nsleep<\/span> 10\necho<\/span> abc ><\/span> \/opt\/abc.txt\n<\/code><\/pre>\n<\/p>\n

#2<\/p>\n

\n

#!\/usr\/bin\/expect<\/span>\nset<\/span> passwd<\/span> \"mypasswd\"<\/span>\nset<\/span> timeout<\/span> 60\nif<\/span> {<\/span>$argc<\/span> !=<\/span> 1}<\/span> {<\/span>\n    send \"usage .\/account.sh $newaccount<\/span>n\"<\/span>\u3000\n    exit<\/span>\n}<\/span>\nset<\/span> user [<\/span>lindex $argv<\/span> [<\/span>expr $argc<\/span>-1]<\/span>]<\/span>\nspawn sudo<\/span> useradd<\/span> -s \/bin\/bash -g mygroup -m $user<\/span>\nexpect<\/span> {<\/span>\n    \"assword\"<\/span> {<\/span>\n        send_user \"sudo nown\"<\/span>\n        send \"$passwd<\/span>n\"<\/span>\n        exp_continue\n    }<\/span>\n    eof\n    {<\/span>\n        send_user \"eofn\"<\/span>\n    }<\/span>\n}<\/span>\nspawn sudo<\/span> passwd<\/span> $user<\/span>\nexpect<\/span> {<\/span>\n    \"assword\"<\/span> {<\/span>\n

\u5217\u4e3e10\u4e2alinux\u547d\u4ee4_linux set\u547d\u4ee4\u4f8b\u5b50_linux\u547d\u4ee4\u4f7f\u7528\u4f8b\u5b50<\/p>\n send \"$passwd<\/span>n\"<\/span>\n exp_continue\n }<\/span>\n eof\n {<\/span>\n send_user \"eof\"<\/span>\n }<\/span>\n}<\/span>\nspawn sudo<\/span> smbpasswd -a $user<\/span>\nexpect<\/span> {<\/span>\n \"assword\"<\/span> {<\/span>\n send \"$passwd<\/span>n\"<\/span>\n exp_continue\n }<\/span>\n eof\n {<\/span>\n send_user \"eof\"<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n<\/p>\n

\u7b2c3\u884c\uff1a\u5bf9\u53d8\u91cf\u5f62\u53c2\u7684\u6280\u5de7\uff1b<\/p>\n

\u7b2c4\u884c\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ctimeout\u662f10\u79d2\uff1b<\/p>\n

\u7b2c6\u884c\uff1a\u53c2\u6570\u7684\u6570\u91cf\u53ef\u4ee5\u7528argc\u5f97\u5230\uff1b\u7b2c11\u884c\uff1a\u53c2\u6570\u5b58\u5728argc\u5f97\u5230\uff1b\u7b2c11\u884c\uff1a\u53c2\u6570\u5b58\u5728argc\u5f97\u5230\uff1b\u7b2c11\u884c\uff1a\u53c2\u6570\u5b58\u5728argv\u5f53\u4e2d\uff0c\u4f8b\u5982\u53d6\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f[lindex$argv0]\uff1b\u800c\u4e14\u5047\u5982\u987b\u8981\u4f30\u7b97\u7684\u8bdd\u5fc5\u987b\u7528exprlinux set\u547d\u4ee4\u4f8b\u5b50<\/strong>\uff0c\u5982\u4f30\u7b972-1\uff0c\u5219\u5fc5\u987b\u7528[expr2-1]\uff1b<\/p>\n

\u7b2c13\u884c\uff1a\u7528spawn\u6765\u6267\u884c\u4e00\u6761shell\u547d\u4ee4\uff0cshell\u547d\u4ee4\u4f9d\u7167\u5177\u4f53\u60c5\u51b5\u53ef\u81ea\u884c\u8c03\u6574\uff1b\u6709\u6587\u7ae0\u8bf4sudo\u8981\u52a0-S\uff0c\u7ecf\u8fc7\u5b9e\u9645\u6d4b\u8bd5\uff0c\u65e0\u9700\u52a0-S\u4ea6\u53ef\uff1b<\/p>\n

\u7b2c15\u884c\uff1a\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5047\u5982\u8fde\u7eed\u505a\u4e24\u4e2aexpect\uff0c\u8fd9\u4e48\u5b9e\u9645\u4e0a\u662f\u4e32\u884c\u6267\u884c\u7684\uff0c\u7528\u4e8b\u4f8b\u4e2d\u7684\u7ed3\u6784\u5219\u662f\u5e76\u884c\u6267\u884c\u7684\uff0c\u4e3b\u8981\u662f\u770b\u5339\u914d\u5230\u4e86\u54ea\u4e00\u4e2a\uff1b\u5728\u8fd9\u4e2a\u4e8b\u4f8b\u4e2d\uff0c\u5047\u5982\u4f60\u5199\u6210\u4e32\u884c\u7684\u8bdd\uff0c\u5373<\/p>\n

\n

expect<\/span> \"assword\"<\/span>\nsend \"$passwd<\/span>n\"<\/span>\nexpect<\/span> eof\nsend_user \"eof\"<\/span>\n<\/code><\/pre>\n<\/p>\n

\u8fd9\u4e48\u7b2c\u4e00\u6b21\u5c06\u4f1a\u6b63\u786e\u8fd0\u884c\uff0c\u7531\u4e8e\u7b2c\u4e00\u6b21sudo\u65f6\u987b\u8981\u5bc6\u7801\uff1b\u5e76\u4e14\u7b2c\u4e8c\u6b21\u8fd0\u884c\u65f6\u56e0\u4e3a\u5bc6\u7801\u65e9\u5df2\u8f93\u8fc7\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0bsudo\u5bc6\u7801\u518d\u5ea6\u8f93\u5165\u65f6\u95f4\u4e3a5\u5206\u949f\uff09\uff0c\u5219\u4e0d\u4f1a\u63d0\u793a\u7528\u6237\u53bb\u8f93\u5165\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2aexpect\u5c06\u672a\u80fd\u5339\u914d\u5230assword\uff0c\u4f46\u662f\u5fc5\u987b\u6ce8\u610f\u7684\u662f\u5047\u5982\u662fspawn\u547d\u4ee4\u51fa\u73b0\u4ea4\u4e92\u5f0f\u63d0\u95ee\u7684\u5e76\u4e14expect\u5339\u914d\u4e0d\u4e0a\u7684\u8bdd\uff0c\u8fd9\u4e48\u7a0b\u5e8f\u4f1a\u6839\u636etimeout\u7684\u8bbe\u7f6e\u8fdb\u884c\u7b49\u5f85\uff1b\u800c\u4e14\u5047\u5982spawn\u76f4\u63a5\u53d1\u51fa\u4e86eof\u4e5f\u5c31\u662f\u672c\u4f8b\u7684\u60c5\u51b5\uff0c\u8fd9\u4e48expect\"assword\"\u5c06\u4e0d\u4f1a\u7b49\u5f85\uff0c\u800c\u76f4\u63a5\u53bb\u6267\u884cexpecteof\u3002\u8fd9\u65f6\u90fd\u4f1a\u62a5expect:spawnidexp6notopen\uff0c\u7531\u4e8e\u6ca1\u6709spawn\u5728\u6267\u884c\uff0c\u65c1\u8fb9\u7684expect\u811a\u672c\u4e5f\u5c06\u4f1a\u7531\u4e8e\u8fd9\u4e2a\u7f18\u7531\u800c\u4e0d\u518d\u6267\u884c\uff1b\u6240\u4ee5\u5bf9\u4e8e\u7c7b\u4f3csudo\u8fd9\u4e9b\u547d\u4ee4\u5206\u652f\u4e0d\u5b9a\u7684\u60c5\u51b5\uff0c\u6700\u597d\u662f\u4f7f\u7528\u5e76\u884c\u7684\u5f62\u5f0f\u8fdb\u884c\u5904\u7406\uff1b<\/p>\n

\u7b2c17\u884c\uff1a\u4ec5\u4ec5\u662f\u4e00\u4e2a\u7528\u6237\u63d0\u793a\u800c\u5df2\uff0c\u53ef\u4ee5\u5220\u6389\uff1b<\/p>\n

linux\u547d\u4ee4\u4f7f\u7528\u4f8b\u5b50_\u5217\u4e3e10\u4e2alinux\u547d\u4ee4_linux set\u547d\u4ee4\u4f8b\u5b50<\/p>\n

\u7b2c18\u884c\uff1a\u5411spawn\u8fdb\u7a0b\u53d1\u9001password\uff1b<\/p>\n

\u7b2c19\u884c\uff1a\u4fc3\u4f7fspawn\u8fdb\u7a0b\u5728\u5339\u914d\u5230\u4e00\u4e2a\u540e\u518d\u53bb\u5339\u914d\u63a5\u51fa\u6765\u7684\u4ea4\u4e92\u63d0\u793a\uff1b<\/p>\n

\u7b2c21\u884c\uff1aeof\u662f\u5fc5\u987b\u53bb\u5339\u914d\u7684\uff0c\u5728spawn\u8fdb\u7a0b\u7ed3\u675f\u540e\u4f1a\u5411expect\u53d1\u9001eof\uff1b\u5018\u82e5\u4e0d\u53bb\u5339\u914d\uff0c\u6709\u65f6\u4e5f\u80fd\u8fd0\u884c\uff0c\u4f8b\u5982sleep\u591a\u5c11\u79d2\u540e\u518d\u53bbspawn\u4e0b\u4e00\u4e2a\u547d\u4ee4\uff0c\u5e76\u4e14\u4e0d\u8981\u4f9d\u8d56\u8fd9\u4e9b\u884c\u4e3a\uff0c\u5f88\u6709\u53ef\u80fd\u660e\u5929\u8fd8\u53ef\u4ee5\uff0c\u4eca\u5929\u5c31\u4e0d\u80fd\u7528\u4e86\uff1b<\/p>\n

#3<\/p>\n

\u4e0b\u8fb9\u8fd9\u4e2a\u4e8b\u4f8b\u6bd4\u8f83\u7279\u6b8a\uff0c\u5728\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u5c31\u4e0d\u80fdexpecteof\u4e86\uff1a<\/p>\n

\n

#!\/usr\/bin\/expect<\/span>\nset<\/span> timeout<\/span> 30\nspawn ssh<\/span> 10.192.224.224\nexpect<\/span> \"password:\"<\/span>\nsend \"mypasswordn\"<\/span>\nexpect<\/span> \"*$\"<\/span>\nsend \"mkdir tmpdirn\"<\/span>\nexpect<\/span> \"*$\"<\/span>\n<\/code><\/pre>\n<\/p>\n

timeout\u6307expect\u7b49\u5f85\u547d\u4ee4\u81f3\u591a\u7b4930seconds<\/p>\n

\u8fd9\u4e2a\u4e8b\u4f8b\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7ssh\u53bb\u767b\u9646\u8fdc\u7a0b\u673a\u5668\uff0c\u4f46\u662f\u5728\u8fdc\u7a0b\u673a\u5668\u4e0a\u521b\u4f73\u4e00\u4e2a\u76ee\u5f55\uff0c\u6211\u4eec\u770b\u89c1\u5728\u6211\u4eec\u8f93\u5165\u5bc6\u7801\u540e\u5e76\u6ca1\u6709\u53bbexpecteof\uff0c\u8fd9\u662f\u7531\u4e8essh\u8fd9\u4e2aspawn\u5e76\u6ca1\u6709\u7ed3\u675f\uff0c\u5e76\u4e14\u81ea\u52a8\u64cd\u4f5c\u65f6ssh\u5b9e\u9645\u4e0a\u4e5f\u4e0d\u4f1a\u81ea\u5df1\u7ed3\u675f\u9664\u975e\u4f60exit\uff1b\u6240\u4ee5\u4f60\u53ea\u80fdexpectbash\u7684\u63d0\u793a\u7b26\u5982\u4f55\u5b89\u88c5LINUX\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u662f\u673a\u5668\u540d\u7b49linux set\u547d\u4ee4\u4f8b\u5b50<\/strong>\uff0c\u8fd9\u6837\u624d\u53ef\u4ee5\u5728\u8fdc\u7a0b\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u3002<\/p>\n

\u6ce8\u610f\uff0c\u8bf7\u4e0d\u8981\u7528spawnmkdirtmpdir\uff0c\u8fd9\u6837\u4f1a\u4fc3\u4f7f\u4e0a\u4e00\u4e2aspawn\u5373ssh\u7ed3\u675f\uff0c\u8fd9\u4e48\u4f60\u7684tmpdir\u5c06\u5728\u672c\u673a\u6784\u5efa\u3002<\/p>\n

\u5176\u5b9e\u5b9e\u9645\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u8981\u4f60\u786e\u8ba4sshkey\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u7684expect\u8fdb\u884c\u5904\u7406\uff0c\u4e0d\u591a\u8d58\u8a00\u3002<\/p>\n

#4<\/p>\n

\u6700\u540e\u4e00\u4e2ademo\u662f\u81ea\u5df1\u5c1d\u8bd5\u5199\u7684\uff0c\u529f\u80fd\u662f\u5b89\u88c5python3.6\uff0e\u6bd4\u8f83\u7b80\u5355\u7c97\u9c81\uff0c\u5c31\u662f\u7528spawn\u6765\u5f00\u542f\u4e0d\u540c\u7684\u8fdb\u7a0b\uff0c\u5e76\u59cb\u7ec8\u4fdd\u6301\u4ea4\u4e92\u72b6\u6001\uff0e\u8fd9\u6837\u505a\u7684\u7f18\u7531\u662f\u4e4b\u524d\u6269\u5199\u7684\u65f6\u4faf\uff0c\u4f7f\u7528send\u6765\u6267\u884c\u6307\u4ee4\u603b\u662f\u6709\u4e00\u4e9b\u95ee\u9898\uff0e\u540e\u6765\u53d1\u89c9\u8fd9\u6837\u5199\u51fa\u6765\u4e5f\u80fdwork\uff0e\u6682\u65f6\u4e0d\u6653\u5f97\u5f02\u540c\u70b9\uff0c\u5148\u8d34\u4e0a\u6765\u8bf7\u8bf8\u4f4d\u6307\u6559\uff0e<\/p>\n

\n

#!\/usr\/bin\/expect<\/span>\nset<\/span> timeout<\/span> 30\nspawn apt-get<\/span> install<\/span> -y software-properties-common\ninteract\nspawn add-apt-repository ppa:deadsnakes\nexpect<\/span> \"ENTER\"<\/span>\nsend \"n\"<\/span>\ninteract\nspawn apt-get<\/span> update\ninteract\nspawn apt-get<\/span> install<\/span> python3.6 -y\ninteract\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"

expect\u662f\u5efa\u7acb\u5728tcl\u57fa\u7840\u4e0a\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u5b83\u7528\u6765\u8ba9\u4e00\u4e9b\u9700\u8981\u4ea4\u4e92\u7684\u4efb\u52a1\u81ea\u52a8\u5316\u5730\u5b8c\u6210\u3002\u8fd9\u91cc\u5c31\u662f\u6267\u884c\u4ea4\u4e92\u52a8\u4f5c\uff0c\u4e0e\u624b\u5de5\u8f93\u5165\u5bc6\u7801\u7684\u52a8\u4f5c\u7b49\u6548\u3002<\/p>\n","protected":false},"author":1,"featured_media":294805,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[4706,4710,4708,4709,4707],"class_list":["post-294804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-expect","tag-exp_continue","tag-send","tag-send_user","tag-spawn"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/294804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=294804"}],"version-history":[{"count":0,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/294804\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/294805"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=294804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=294804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=294804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

\u4e0b\u8fb9\u7684\u811a\u672c\u4e8b\u4f8b\uff0c\u5b9e\u73b0\u4e86\uff1a\u767b\u9646\u4e00\u4e2aLinux<\/a>\u670d\u52a1\u5668\uff0c\u6267\u884cdf-h\u7b49\u547d\u4ee4\u3002<\/p>\n