Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1700407
  • 博文数量: 636
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3950
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-06 21:58
个人简介

博客是我工作的好帮手,遇到困难就来博客找资料

文章分类

全部博文(636)

文章存档

2024年(5)

2022年(2)

2021年(4)

2020年(40)

2019年(4)

2018年(78)

2017年(213)

2016年(41)

2015年(183)

2014年(66)

我的朋友

分类: 系统运维

2017-02-27 16:50:35

下载并安装pexpect与ptyprocess模块并结合python2.7实现如下操作

tar -zxvf pexpect-4.0.1.tar.gz 

cd pexpect-4.0.1

python setup.py install

tar -zxvf ptyprocess-0.5.tar.gz 

cd /ptyprocess-0.5

python setup.py install


Py脚本

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

import pexpect 

#from getpass import getpass

#passwd = getpass()

def ssh_cmd(user,ip, cmd,passwd): 

        ssh = pexpect.spawn('ssh %s@%s "%s"' % (user,ip,cmd)) 

        try: 

                i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5) 

                if i == 0 : 

                        ssh.sendline(passwd) 

                elif i == 1: 

                        ssh.sendline('yes') 

                        ssh.expect('password: ') 

                        ssh.sendline(passwd) 

        except pexpect.EOF: 

                print "EOF" 

        except pexpect.TIMEOUT:

                print "TIMEOUT"

        else:

                r = ssh.read() 

                print r

        ssh.close()


if __name__ == '__main__':

        file=open("/mess/filelist",'r')

        a = file.read()

        file.close()

        for host in a.split("\n"):

                if host:

                        user,ip,cmd,passwd = host.split("::")

                        print "-- %s run:%s --" % (ip, cmd)

                        ssh_cmd(user,ip,cmd,passwd)

-------------------------------------------------------------------------------------------

/mess/filelist

root::192.168.100.100::ls -l::password

root::192.168.100.100::ps -ef|grep ssh|awk '{print $8}'::password


阅读(1080) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~