- 论坛徽章:
- 2
|
本帖最后由 ning_lianjie 于 2012-09-19 22:04 编辑
增加了主机检测函数 socket_host,避免定义了不存在的主机,或者主机名错误,导致脚本退出.
代码如下:- #!/usr/bin/env python
- #-*- coding: UTF-8 -*-
- # author:
- # changetime: 20120329
- import pexpect
- import getpass, os
- import sys, getopt
- import socket
- import traceback
- from ConfigParser import ConfigParser
- def usage():
- print '\nUsage: qrsync.py '
- print '编辑.list配置文件'
- sys.exit(0)
- def rsync_input():
- try:
- #files = raw_input('Flies: ')
- #hosts = raw_input('Hostname: ')
- #hosts = 'hostname'
- #user = raw_input('User: ')
- #user = 'lianjie.ning'
- password = getpass.getpass('Please enter password: ')
- except:
- print '\nERROR: input error,please try agent run script\n'
- usage()
- return password
- def load_config():
- cfg_path = os.path.join(os.environ["HOME"], "bin/qrsync.list")
- cfg_temp = '''[connects]
- user =
- hosts =
- # 跳板机存放authorized_keys文件,用ssh登录仍然需要输入token密码
- # 但是应用rsync命令,则可以直接传送.
- files =
- rpath =
- '''
- #将cfg_temp写入文件
- # user 用户
- # hosts 将要传输文件的远程主机列表
- # files 需要传输的文件
- # rpath 远程主机的目录,传输的用户要有写入权限.
- if not os.path.exists(cfg_path):
- open(cfg_path, 'w').write(cfg_temp)
- #加载stat模块,修改文件属性.
- #os.chmod(cfg_path, stat.S_IREAD)
- cfg = ConfigParser()
- cfg.read(cfg_path)
- user = cfg.get('connects', 'user')
- hosts = cfg.get('connects', 'hosts')
- files = cfg.get('connects', 'files')
- rpath = cfg.get('connects', 'rpath')
- return (user,hosts,files,rpath)
- # 检测列表中的主机
- def socket_host(host):
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(3)
- sock.connect((host, 22))
- sock.close()
- return 0
- except:
- print 'ping: unknown host %s' % (host)
- return 1
- #sys.exit(0)
- def rsync_core(user, host, files, rpath, password):
- #password = rsync_input()
- #print command
- #print 'exec command: /usr/bin/rsync -tvzrpc %s %s@hostname:.ssh/' % (files, user)
- #print host
- #check = raw_input('Are you sure transmitted to the tools(yes/no): ')
- check = 'yes'
- if check == 'yes':
- rsync_newkey = 'Are you sure you want to continue connecting'
- child = pexpect.spawn('/usr/bin/rsync -tvzrpc %s %s@%s:%s' % (files, user, host, rpath))
- try:
- i = child.expect([pexpect.TIMEOUT, rsync_newkey, 'password: ', "id_rsa': "])
- except:
- print '出现问题的原因:'
- print '1.该主机不存在'
- print '2.命令不需要交互,请检查主机直接是否做了授信'
- print 'please checkout host and host...'
- sys.exit(0)
- if i == 0: # Timeout
- print 'ERROR!'
- print child.before, child.after
- return None
- if i == 1: # SSH does not have the public key. Just accept it.
- child.sendline ('yes')
- child.expect ('password: ')
- i = child.expect([pexpect.TIMEOUT, 'password: '])
- if i == 0: # Timeout
- print 'ERROR!'
- print child.before, child.after
- return None
- child.sendline(password)
- child.expect(pexpect.EOF)
- print child.before # Print the result of the command
- print child.after
- else:
- sys.exit(0)
- def main():
- # 读取密码文件
- password = rsync_input()
- # load file
- (user,hosts,files,rpath) = load_config()
- if user == None:
- sys.exit(0)
- # print user, password
- filter_hosts = []
- hosts = hosts.strip().split(' ')
- for host in hosts:
- print host
- i = socket_host(host)
- if i == 0:
- #print '111'
- filter_hosts.append(host)
- else:
- #print '222'
- pass
- #for i in filter_hosts:
- #print i
- #sys.exit(0)
- for i,host in enumerate(filter_hosts):
- print i,"rsync -tvzrp %s %s@%s:%s" % (files,user,host,rpath)
- #sys.exit(0)
-
- #确认
- check = raw_input('Are you sure transmitted to the host(yes/no): ')
- if check == 'yes':
- for i,host in enumerate(hosts):
- rsync_core(user, host, files, rpath, password)
- print i,host
- else:
- sys.exit(0)
- if __name__ == '__main__':
- try:
- main()
- except Exception, e:
- print str(e)
- traceback.print_exc()
- os._exit(1)
复制代码 |
|