安装openssh 出现POSIX:operation timeout的解决办法:http://www.111cn.net/sj/170/67712.htm
mac端python2.7操作ssh命令 需要使用一个paramiko模块 要安装这个模块就需要先安装 PyCrypto
参考:http://www.cnblogs.com/tungwoo/p/3780685.html
pip install pycrypto
pip install paramiko
python要解析plist文件 需要安装biplist库 sudo easy_install biplist
import paramiko
class SSHCmd(paramiko.SSHClient):
def runCmd(self, command, bufsize=-1):
chan = self._transport.open_session()
chan.exec_command(command)
stdin = chan.makefile('wb', bufsize)
stdout = chan.makefile('rb', bufsize)
stderr = chan.makefile_stderr('rb', bufsize)
status = chan.recv_exit_status()
return stdin, stdout, stderr, status
def runSSHCmd(cmd,ip = '192.168.202.24',username='root',password=''):
SSH = SSHCmd()
SSH.load_system_host_keys()
SSH._policy = paramiko.AutoAddPolicy()
SSH.connect(ip,22, username='root', password='')
stdin, stdout, stderr,status = SSH.runCmd(cmd)
print stdout.read()
print status
SSH.close()
return [stdin, stdout, stderr,status]
SSHCmd.py
或者采用这个函数
defrunSSHCmd(cmd,ip ='192.168.202.172',username='root',password=''):
ip ='root@'+ ip
ssh = subprocess.Popen(["ssh","%s"% ip, cmd],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
ifresult == []:
error = ssh.stderr.readlines()
print>>sys.stderr,"ERROR: %s"% error
else:
printresult
returnresult