{"id":172289,"date":"2020-10-25T21:08:32","date_gmt":"2020-10-25T13:08:32","guid":{"rendered":"http:\/\/4563.org\/?p=172289"},"modified":"2020-10-25T21:08:32","modified_gmt":"2020-10-25T13:08:32","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e7%9a%84-subprocess-%e6%a8%a1%e5%9d%97%e8%b0%83%e7%94%a8%e7%b3%bb%e7%bb%9f%e5%91%bd%e4%bb%a4-scp","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=172289","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u7684 subprocess \u6a21\u5757\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4 scp?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u4f7f\u7528 Python \u7684 subprocess \u6a21\u5757\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4 scp?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : smallpython <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u5728\u4f7f\u7528 scp \u547d\u4ee4\u7684\u65f6\u5019, \u9700\u8981\u5728\u4fe1\u4efb\u5bf9\u65b9\u4e3b\u673a\u7684\u65f6\u5019\u8f93\u5165 yes, \u4e14\u4e4b\u540e\u8fd8\u8981\u8f93\u5165\u5bc6\u7801<\/p>\n<p>\u8bf7\u95ee python \u80fd\u591f\u6a21\u62df\u952e\u76d8\u8f93\u5165\u8fd9\u4e24\u4e2a\u4fe1\u606f\u5417?<\/p>\n<p>\u6bd4\u5982\u9700\u6c42\u662f\u628a a.txt \u590d\u5236\u5230 100 \u53f0\u670d\u52a1\u5668\u4e0a, \u80fd\u591f\u505a\u6210\u81ea\u52a8\u5316\u7684\u811a\u672c\u5417? \u7a0b\u5e8f\u81ea\u5df1\u8f93\u5165 yes \u53ca\u5bc6\u7801      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3874564\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gnozix <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u4f60\u9700\u8981\u7684\u662f ansible                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874565\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zydxn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u524d\u7528 paramiko \u505a\u8fc7\u7c7b\u4f3c\u7684\u4e8b\u60c5<\/p>\n<p>client = paramiko.SSHClient()<br \/>client.set_missing_host_key_policy(paramiko.AutoAddPolicy())<br \/>client.connect(hostname, ssh_port, username=username, password=password)<br \/>transport = client.get_transport()<br \/>sftpClient = paramiko.SFTPClient.from_transport(transport)<br \/># \u4f20\u8f93<br \/>sftpClient.put(localpath, remotepath, callback=your_call_back)<br \/>client.close()                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874566\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : MoYi123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 expect \u5c31\u884c\u4e86 <\/p>\n<p>#!\/usr\/bin\/expect<br \/>spawn ssh [email&#160;protected]{host} -p {port}<br \/>expect &#8220;{host}&#8217;s password: &#8220;<br \/>send &#8220;{password}&#8221;<br \/>interact<\/p>\n<p>\u8fd9\u662f ssh \u7684\u4f8b\u5b50                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : huangmingyou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ssh \u6709\u53c2\u6570\u5ffd\u7565\u4e3b\u673a key \u8ba4\u8bc1\uff0c\u53e6\u5916\u7528\u5bc6\u94a5\u6587\u4ef6\u66ff\u4ee3\u53e3\u4ee4\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874568\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wwqgtxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f\u53ef\u4ee5\u7684\uff0c\u4e0d\u8fc7\u4f60\u9700\u8981\u7528 subprocess.Popen \u53bb\u5b8c\u6210\u4ea4\u4e92                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874569\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : smallpython <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @MoYi123 <br \/>@huangmingyou <br \/>\u6211\u60f3\u95ee\u7684\u662f\u6709\u6ca1\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u81ea\u5df1\u8f93\u5165, \u800c\u4e0d\u662f\u7ed5\u5f00\u8f93\u5165, \u56e0\u4e3a\u5982\u679c\u7ed5\u5f00\u7684\u8bdd, \u867d\u7136\u529f\u80fd\u89e3\u51b3\u4e86, \u4f46\u662f\u8fd8\u662f\u4e0d\u77e5\u9053\u600e\u4e48\u8ba9 Python \u81ea\u5df1\u53bb\u586b\u5199\u8fd9\u4e2a\u4fe1\u606f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874570\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : smallpython <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wwqgtxx \u80fd\u7ed9\u4e2a\u5177\u4f53\u4f8b\u5b50\u5417?\u6211\u81ea\u5df1\u6d4b\u8bd5 stdin.write()\u65b9\u6cd5\u53ea\u6709\u5728\u672c\u5730\u547d\u4ee4\u624d\u4f1a\u751f\u6548, \u6d89\u53ca\u5230\u8fd9\u79cd\u4e24\u53f0\u673a\u5668\u4ea4\u4e92\u7684\u60c5\u51b5\u5c31\u6ca1\u7528\u4e86, \u8fd8\u662f\u4f1a\u8ba9\u6211\u624b\u52a8\u8f93\u5165\u5bc6\u7801                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tony9413 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             paramiko \u6b63\u89e3\uff0c\u5982\u679c\u4e0d\u4f1a\uff0c\u53ef\u4ee5\u8bd5\u8bd5 robot \u5c01\u88c5\u7684[SSHLibrary]( http:\/\/robotframework.org\/SSHLibrary\/)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874572\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bairdshi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e4b\u524d\u7814\u7a76\u4e86\u8bb8\u4e45 fanric \u662f\u6700\u4f73\u89e3 \u5176\u4ed6\u90fd\u662f\u5783\u573e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874573\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bairdshi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @bairdshi fabric \u4e0d\u5c0f\u5fc3\u6253\u9519\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874574\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bairdshi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             from fabric import task, Connection<br \/>from invoke.tasks import call<\/p>\n<p># r represents raspberry pi<br \/>name_ip_mapping = {&#8220;r1&#8221;: &#8220;192.168.122.3&#8221;, &#8220;r2&#8221;: &#8220;192.168.122.2&#8221;,<br \/> &#8220;r3&#8221;: &#8220;192.168.122.7&#8221;, &#8220;camera&#8221;: &#8220;192.168.122.55&#8221;}<\/p>\n<p>ip_name_mapping = {v: k for k, v in name_ip_mapping.items()}<\/p>\n<p>@task<br \/>def scp1(c):<br \/> &#8220;&#8221;&#8221;<br \/> copy scripts to raspberry pi 1<br \/> &#8220;&#8221;&#8221;<br \/> c = Connection(name_ip_mapping[&#8220;r1&#8221;], user=&#8217;pi&#8217;, connect_kwargs={&#8216;password&#8217;: &#8216;xxxx&#8217;})<br \/> c.put(&#8216;r1\/main.py&#8217;, &#8220;r1&#8221;)<br \/> c.put(&#8216;r1\/config.py&#8217;, &#8220;r1&#8221;)<br \/> c.put(&#8216;r1\/part.py&#8217;, &#8220;r1&#8221;)<br \/> c.put(&#8216;r1\/logger.py&#8217;, &#8220;r1&#8221;)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874575\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangyzj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             paramiko                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874576\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : css3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a a.txt \u590d\u5236\u5230 100 \u53f0\u670d\u52a1\u5668\u4e0a, \u8fd9\u8fd8\u4e0d\u7528 ansible \uff1f\uff1f\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3874577\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CallMeReznov <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             paramiko                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u7684 sub&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/172289"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=172289"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/172289\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172289"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}