Ejemplo n.º 1
0
def ssh_check(conf):
    info = {"msgtype": "ERR"}
    try:
        username = conf["username"]
        password = conf["password"]
        port = conf["port"]
        ip = conf["ip"]
        loginmethod = conf["loginmethod"]
        info = {"msgtype": "ERR", "content": ""}
        import paramiko

        ssh = paramiko.SSHClient()
        if loginmethod == "KEY":
            keyfile = conf["keyfile"]
            KeyPath = key_resolv(keyfile)
            key = paramiko.RSAKey.from_private_key_file(KeyPath)
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, port, username, pkey=key)
        else:
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, int(port), username, password)
        info["msgtype"] = "OK"
    except Exception, e:
        print "检测错误", e
        info["content"] = str(e)
Ejemplo n.º 2
0
def main(cmd,ie_key,selectserver,Data,excutetype='cmd',hwtype='CPU'):
	Data.excutetype=excutetype
	Data.hwtype=hwtype
	Data.FailIP=[]
	Data.All_Servers_num_all=0
	Data.All_Servers_num=0
	Data.All_Servers_num_Succ=0
	Data.i=0
	all_conf=db_to_redis_allconf.allhostconf()['content']
	Data.conf=all_conf
	if selectserver=='all':
		selectserver=Data.conf.keys()
	else:
		selectserver=selectserver.split(',')
	print selectserver
	Data.All_Servers_num_all=len(selectserver)
	Data.i=len(selectserver)
	for id in selectserver:
		if Data.conf[id]['loginmethod']=='KEY':
			keyfile=key_resolv.key_resolv(Data.conf[id]["keyfile"],cache)
		else:
			keyfile='N'
		if Data.conf[id]["su"]=="Y" and excutetype=='cmd' :
			b=threading.Thread(target=cheungssh_su.Excute_suroot,args=(Data.conf[id]["ip"],Data.conf[id]["username"],Data.conf[id]["password"],Data.conf[id]["port"],Data.conf[id]["loginmethod"],keyfile,cmd,ie_key,Data.conf[id]["group"],Data.conf[id]["supassword"],Data))
		elif Data.conf[id]["sudo"]=="Y" and excutetype=='cmd':
			b=threading.Thread(target=cheungssh_sudo.Excute_sudo,args=(Data.conf[id]["ip"],Data.conf[id]["username"],Data.conf[id]["password"],Data.conf[id]["port"],Data.conf[id]["loginmethod"],keyfile,cmd,ie_key,Data.conf[id]["group"],Data.conf[id]["sudopassword"],Data))
		else:
			b=threading.Thread(target=SSH_cmd,args=(Data.conf[id]["ip"],Data.conf[id]["username"],Data.conf[id]["password"],Data.conf[id]["port"],Data.conf[id]["loginmethod"],keyfile,cmd,ie_key,Data.conf[id]["group"],Data))
		b.start()
	b.join()
Ejemplo n.º 3
0
def main(cmd,ie_key,selectserver,Data,tid,excutetype='cmd',hwtype='CPU'):
	Data.excutetype=excutetype
	Data.hwtype=hwtype
	Data.FailIP=[]
	Data.All_Servers_num_all=0
	Data.All_Servers_num=0
	Data.All_Servers_num_Succ=0
	Data.i=0
	all_conf=db_to_redis_allconf.allhostconf()['content']
	Data.conf=all_conf
	if selectserver=='all':
		selectserver=Data.conf.keys()
	else:
		selectserver=selectserver.split(',')
	print selectserver
	Data.All_Servers_num_all=len(selectserver)
	Data.i=len(selectserver)
	for id in selectserver:
		if Data.conf[id]['loginmethod']=='KEY':
			keyfile=key_resolv.key_resolv(Data.conf[id]["keyfile"],cache)
		else:
			keyfile='N'
		if Data.conf[id]["su"]=="Y" and excutetype=='cmd' :
			b=threading.Thread(target=cheungssh_su.Excute_suroot,args=(Data.conf[id]["ip"],Data.conf[id]["username"],Data.conf[id]["password"],Data.conf[id]["port"],Data.conf[id]["loginmethod"],keyfile,cmd,ie_key,Data.conf[id]["group"],Data.conf[id]["supassword"],Data,tid))
		elif Data.conf[id]["sudo"]=="Y" and excutetype=='cmd':
			b=threading.Thread(target=cheungssh_sudo.Excute_sudo,args=(Data.conf[id]["ip"],Data.conf[id]["username"],Data.conf[id]["password"],Data.conf[id]["port"],Data.conf[id]["loginmethod"],keyfile,cmd,ie_key,Data.conf[id]["group"],Data.conf[id]["sudopassword"],Data,tid))
		else:
			b=threading.Thread(target=SSH_cmd,args=(Data.conf[id]["ip"],Data.conf[id]["username"],Data.conf[id]["password"],Data.conf[id]["port"],Data.conf[id]["loginmethod"],keyfile,cmd,ie_key,Data.conf[id]["group"],Data,tid))
		b.start()
	b.join()
	ip_to_id(Data)
	cache.set(ie_key,Data.FailID,864000)
Ejemplo n.º 4
0
def ssh_check(conf):
    info = {"msgtype": "ERR"}
    try:
        username = conf['username']
        password = conf['password']
        port = conf['port']
        ip = conf['ip']
        loginmethod = conf['loginmethod']
        info = {"msgtype": "ERR", "content": ""}
        import paramiko
        ssh = paramiko.SSHClient()
        if loginmethod == 'KEY':
            keyfile = conf['keyfile']
            KeyPath = key_resolv(keyfile)
            key = paramiko.RSAKey.from_private_key_file(KeyPath)
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, port, username, pkey=key)
        else:
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip, int(port), username, password)
        info['msgtype'] = "OK"
        ssh.close()
    except Exception, e:
        print '检测错误', e
        info['content'] = str(e)
Ejemplo n.º 5
0
def UploadFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user):
	print "开始上传..........."
	socket.setdefaulttimeout(3)
	model="transfile_getfile_upload"
	info={"msgtype":"OK","content":"","status":"running","progres":"0"}
	translog=[]
	lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logline={"fid":fid,"action":"upload","time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),"ip":ip,"sfile":sfile,"dfile":dfile,"result":"ERR","user":user,"msg":"","size":"0KB","lasttime":lasttime}
	try:
		t=paramiko.Transport((ip,int(port)))
		if loginmethod=="key".upper():
			print "key登陆"
			keyfile=key_resolv.key_resolv(keyfile,cache)
			key=paramiko.RSAKey.from_private_key_file(keyfile)
			t.connect(username = username,pkey=key)
		else:
			print "密码登陆"
			t.connect(username = username,password = password)
		callback_info = functools.partial(set_progres,fid)
		sftp = paramiko.SFTPClient.from_transport(t)
		if dfile.endswith('/'):
			try:
				sftp.listdir(dfile)
			except Exception,e:
				raise IOError("%s 目录不存在" %(dfile))
		try:
			sftp.listdir(dfile)
			dfile=os.path.join(dfile,os.path.basename(sfile))
		except Exception,e:
			pass
Ejemplo n.º 6
0
def DownFile(dfile, sfile, username, password, ip, port, su, supassword, sudo,
             sudopassword, loginmethod, keyfile, fid, user):
    socket.setdefaulttimeout(3)
    dfile = os.path.basename(dfile)
    dfile = os.path.join(download_dir, dfile)
    model = "transfile_downfile"
    info = {"status": True, "content": "", "status": "running", "progres": "0"}
    translog = []
    lasttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    logline = {
        "fid": fid,
        "action": "download",
        "time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
        "ip": ip,
        "sfile": sfile,
        "dfile": dfile,
        "result": False,
        "user": user,
        "msg": "",
        "size": "0KB",
        "lasttime": lasttime
    }

    try:
        t = paramiko.Transport((ip, int(port)))
        if loginmethod == "key".upper():
            keyfile = key_resolv.key_resolv(keyfile, cache)
            key = paramiko.RSAKey.from_private_key_file(keyfile)
            t.connect(username=username, pkey=key)
        else:
            t.connect(username=username, password=password)
        callback_info = functools.partial(set_progres, fid, 1, 1, False)
        sftp = paramiko.SFTPClient.from_transport(t)
        try:
            sftp.listdir(sfile)
            cheungssh_sftp(fid, ip, username, sfile, dfile, set_progres, port,
                           loginmethod, password, keyfile)
            return
        except Exception, e:
            if e.errno == 2:
                pass
            else:
                raise IOError(e)
        sftp.get(sfile, "%s.%s" % (dfile, ip), callback=callback_info)
        log(model, True)
        info['status'] = 'True'
        logline["result"] = True
        cache_size_id = "fid:size:%s" % (fid)
        cache_size = cache.get(cache_size_id)
        if cache_size is None:
            cache_size = 0
        t_size = float(cache_size) / float(1024)
        logline['size'] = "%0.2fKB" % t_size
        cache_translog = cache.get("translog")
        if cache_translog:
            cache_translog.append(logline)
        else:
            translog.append(logline)
            cache_translog = translog
        cache.set("translog", cache_translog, 3600000000)
Ejemplo n.º 7
0
def UploadFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user):
	print "开始上传..........."
	socket.setdefaulttimeout(3)
	model="transfile_getfile_upload"
	info={"msgtype":"OK","content":"","status":"running","progres":"0"}
	translog=[]
	lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logline={"fid":fid,"action":"upload","time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),"ip":ip,"sfile":sfile,"dfile":dfile,"result":"ERR","user":user,"msg":"","size":"0KB","lasttime":lasttime}
	try:
		t=paramiko.Transport((ip,int(port)))
		if loginmethod=="key".upper():
			print "key登陆"
			keyfile=key_resolv.key_resolv(keyfile,cache)
			key=paramiko.RSAKey.from_private_key_file(keyfile)
			t.connect(username = username,pkey=key)
		else:
			print "密码登陆"
			t.connect(username = username,password = password)
		######################测试###################
		if os.path.isdir(sfile):
			#(sdir,ddir,username,password,ip,loginmethod,keyfile,port=22,force=True,callback_info):
			print " [%s]" % password
			sync_dir.UploadFile(sfile,dfile,username,password,ip,loginmethod,keyfile,fid,set_progres,port,True)
			pass
		else:
			
			sftp = paramiko.SFTPClient.from_transport(t)
			if dfile.endswith('/'):
				try:
					sftp.listdir(dfile)
				except Exception,e:
					raise IOError("%s 目录不存在" %(dfile))
			try:
				sftp.listdir(dfile)
				dfile=os.path.join(dfile,os.path.basename(sfile))
			except Exception,e:
				pass
			print sfile,dfile
			callback_info = functools.partial(set_progres,fid,1,1,False)
			sftp.put(sfile,dfile,callback=callback_info)
			log(model,"OK")
			logline["result"]="OK"
			info["status"]="OK"
			info["msgtype"]="OK"
			cache_size_id="fid:size:%s" %(fid)
			cache_size=cache.get(cache_size_id)
			if cache_size is None:
				cache_size=0
			t_size=float(cache_size)/float(1024)
			logline['size']="%0.2fKB"  %t_size
			cache_translog=cache.get("translog")
			if  cache_translog:
				print  888888888
				cache_translog.append(logline)
			else:
				print  9999999999
				translog.append(logline)
				cache_translog=translog
			cache.set("translog",cache_translog,3600000000)
Ejemplo n.º 8
0
def DownFile(dfile,sfile,username,password,ip,port,su,supassword,sudo,sudopassword,loginmethod,keyfile,fid,user):
	socket.setdefaulttimeout(3)
	dfile=os.path.basename(dfile)
	dfile=os.path.join(download_dir,dfile)
	model="transfile_downfile"
	info={"msgtype":"OK","content":"","status":"running","progres":"0"}
	translog=[]
	lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logline={"fid":fid,"action":"download","time":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),"ip":ip,"sfile":sfile,"dfile":dfile,"result":"ERR","user":user,"msg":"","size":"0KB","lasttime":lasttime}

	try:
		t=paramiko.Transport((ip,int(port)))
		if loginmethod=="key".upper():
			keyfile=key_resolv.key_resolv(keyfile,cache)
			key=paramiko.RSAKey.from_private_key_file(keyfile)
			t.connect(username = username,pkey=key)
		else:
			t.connect(username = username,password = password)
		callback_info = functools.partial(set_progres,fid)
		sftp = paramiko.SFTPClient.from_transport(t)
		print dfile
		sftp.get(sfile,dfile,callback=callback_info)
		log(model,"OK")
		info['status']='OK'
		logline["result"]="OK"
		cache_size_id="fid:size:%s" %(fid)
		cache_size=cache.get(cache_size_id)
		if cache_size is None:
			cache_size=0
		t_size=float(cache_size)/float(1024)
		logline['size']="%0.2fKB"  %t_size
		cache_translog=cache.get("translog")
		if  cache_translog:
			print  888888888
			cache_translog.append(logline)
		else:
			print  9999999999
			translog.append(logline)
			cache_translog=translog
		cache.set("translog",cache_translog,3600000000)
	except Exception,e:
		msg=str(e)
		print msg
		info["content"]=msg
		info["status"]='ERR'
		log(model,msg)
		cache.set("info:%s"%(fid),info,360000)
		logline["result"]="ERR"
		logline["msg"]=msg
		cache_translog=cache.get("translog")
		print "抓取到异常...",e
		logline["result"]=msg
Ejemplo n.º 9
0
def ssh_check(conf):
    info = {"status": False, "content": ""}
    try:
        username = conf['username']
        password = conf['password']
        port = conf['port']
        ip = conf['ip']
        loginmethod = conf['loginmethod']
        SSH = cache.get('%s.SSH' % (ip))
        if SSH is None:
            try:
                ssh = paramiko.SSHClient()
                if loginmethod == 'KEY':
                    keyfile = conf['keyfile']
                    KeyPath = key_resolv(keyfile)
                    key = paramiko.RSAKey.from_private_key_file(KeyPath)
                    ssh.load_system_host_keys()
                    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                    ssh.connect(ip, port, username, pkey=key)
                else:
                    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                    ssh.connect(ip, int(port), username, password)
                info['status'] = True
            except paramiko.ssh_exception.NoValidConnectionsError:
                info["content"] = "不能正常连接该主机"
            except socket.timeout:
                info["content"] = "连接端口超时"
            except socket.gaierror:
                info["content"] = "无法联系上这个主机"
            except paramiko.ssh_exception.AuthenticationException:
                info["content"] = "账号或者密码错误"
            except paramiko.ssh_exception.BadAuthenticationType:
                if loginmethod == 'KEY':
                    info["content"] = "认证类型应该是密码"
                else:
                    info["content"] = "认证类型应该是秘钥"
            except Exception, e:
                info['content'] = "未知错误类型"
            finally:
                ssh.close()
            cache.set("%s.SSH" % (ip), info, 600)  ###########10分钟检查一次链接状态
Ejemplo n.º 10
0
def ssh_check(conf):
	info={"msgtype":"ERR"}
	try:
		username=conf['username']
		password=conf['password']
		port=conf['port']
		ip=conf['ip']
		loginmethod=conf['loginmethod']
		info={"msgtype":"ERR","content":""}
		import paramiko
		ssh=paramiko.SSHClient()
		if loginmethod=='KEY':
			keyfile=conf['keyfile']
			KeyPath= key_resolv(keyfile)
			key=paramiko.RSAKey.from_private_key_file(KeyPath)
			ssh.load_system_host_keys()
			ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
			ssh.connect(ip,port,username,pkey=key)  
                else:
                        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                        ssh.connect(ip,int(port),username,password)
		info['msgtype']="OK"
	except Exception,e:
		info['content']=str(e)
Ejemplo n.º 11
0
def UploadFile(dfile, sfile, username, password, ip, port, su, supassword,
               sudo, sudopassword, loginmethod, keyfile, fid, user):
    print "开始上传..........."
    socket.setdefaulttimeout(3)
    model = "transfile_getfile_upload"
    info = {
        "msgtype": "OK",
        "content": "",
        "status": "running",
        "progres": "0"
    }
    translog = []
    lasttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    logline = {
        "fid": fid,
        "action": "upload",
        "time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
        "ip": ip,
        "sfile": sfile,
        "dfile": dfile,
        "result": "ERR",
        "user": user,
        "msg": "",
        "size": "0KB",
        "lasttime": lasttime
    }
    try:
        t = paramiko.Transport((ip, int(port)))
        if loginmethod == "key".upper():
            print "key登陆"
            keyfile = key_resolv.key_resolv(keyfile, cache)
            key = paramiko.RSAKey.from_private_key_file(keyfile)
            t.connect(username=username, pkey=key)
        else:
            print "密码登陆"
            t.connect(username=username, password=password)

        if os.path.isdir(sfile):
            #(sdir,ddir,username,password,ip,loginmethod,keyfile,port=22,force=True,callback_info):
            print " [%s]" % password
            sync_dir.UploadFile(sfile, dfile, username, password, ip,
                                loginmethod, keyfile, fid, set_progres, port,
                                True)
            pass
        else:

            sftp = paramiko.SFTPClient.from_transport(t)
            if dfile.endswith('/'):
                try:
                    sftp.listdir(dfile)
                except Exception, e:
                    raise IOError("%s 目录不存在" % (dfile))
            try:
                sftp.listdir(dfile)
                dfile = os.path.join(dfile, os.path.basename(sfile))
            except Exception, e:
                pass
            print sfile, dfile
            callback_info = functools.partial(set_progres, fid, 1, 1, False)
            sftp.put(sfile, dfile, callback=callback_info)
            log(model, "OK")
            logline["result"] = "OK"
            info["status"] = "OK"
            info["msgtype"] = "OK"
            cache_size_id = "fid:size:%s" % (fid)
            cache_size = cache.get(cache_size_id)
            if cache_size is None:
                cache_size = 0
            t_size = float(cache_size) / float(1024)
            logline['size'] = "%0.2fKB" % t_size
            cache_translog = cache.get("translog")
            if cache_translog:
                print 888888888
                cache_translog.append(logline)
            else:
                print 9999999999
                translog.append(logline)
                cache_translog = translog
            cache.set("translog", cache_translog, 3600000000)