码迷,mamicode.com
首页 > 其他好文 > 详细

利用sshpass和ssh编写脚本远程执行命令

时间:2016-08-01 23:16:00      阅读:553      评论:0      收藏:0      [点我收藏+]

标签:sshpass ssh subprocess

import subprocess

import os


(rfd, wfd) = os.pipe()

arg = "-d%d" % rfd

try:

    p = subprocess.Popen(["sshpass",arg,"ssh","-l","haohzhang","phxaishdc9dn1447.stratus.phx.ebay.com","ls","/tmp/"], stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    os.close(rfd)

    os.write(wfd,"PASSWORD\n")

    os.close(wfd)

    stdout, stderr = p.communicate()

    print stdout, stderr

except OSError:

    print("to use the ‘ssh‘ connection type with passwords, you must install the sshpass program")


本文出自 “Linux运维” 博客,转载请与作者联系!

利用sshpass和ssh编写脚本远程执行命令

标签:sshpass ssh subprocess

原文地址:http://haohaozhang.blog.51cto.com/9176600/1833107

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!