连接oracle数据库,执行ssh命令,然后把查询结果反写数据库-批量处理管理测试设备

来源:互联网 发布:中国武术是花架子知乎 编辑:程序博客网 时间:2024/06/01 23:37
#-*- coding: utf-8 -*-
#!/usr/bin/python


import cx_Oracle
import paramiko
import threading
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


cmdStr = ['cat /proc/meminfo |grep MemTotal ','dmidecode | grep "Product Name" | grep -v "filled"','fdisk -l |grep Disk | grep -v identifier']#你要执行的命令列表


def test(ip,username,passwd,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,22,'root','iptvtest',timeout=5)
        strResult = ""
        for com in cmd:
            #print com
            stdin, stdout, stderr = ssh.exec_command(com)
            out = stdout.readlines()
            #print out
            strTemp = str(out[0])
            strResult = strResult.strip() + strTemp.strip()+"##"
            #print "strResult:",strResult
        strResult = strResult + '\r\n'
        return strResult
    except :
        print 'Error'
        return 'Error'




def ssh2(ip,username,passwd,cmd):
    try:
      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('10.40.110.87', 22, 'root', 'iptvtest', timeout=5)
      # ssh.connect(ip,22,username,passwd,timeout=5)
      print cmd
      strResult=""
      for com in cmd:
          print com
          stdin, stdout, stderr = ssh.exec_command(com)
          out = stdout.readlines()
          # stdin, stdout, stderr = ssh.exec_command(com)
          # out = stdout.readlines()
          strResult = strResult + out[0]
          print strResult
      ssh.close()
      return strResult
    except :
      print 'Error'
      return 'Error'


username = "root" #用户名
passwd = "iptvtest"  #密码


con = cx_Oracle.connect( "zxiptv", "iptvtest","zxin")
print con.version
cursor = con.cursor()
cursor_update = con.cursor()


#print "test:",test()


cursor.execute("select * from tdm_device where note not like \'@@%\' and status like \'UP\'")
#cursor.execute("select * from tdm_device where host like \'172.16.105.225\'")
for row in cursor:
    print row[3]
    str1 = ""
    str1 = test(row[3],username,passwd,cmdStr)
    print "strResult:",str1
    if str1 != "Error":
        print "str1:",str1
        str1 = str1[:-4]
        #dictInfo =  dict(i.split(':') for i in str1[:-2].split('##'))
        listStr = []
        for i in str1.split('##'):
            listStr.append(i.split(":"))
        listTemp = listStr[2][1].strip().split(',')
        listStr[2][1] = listTemp[0]
        str1 = '@@内存:%s 硬盘:%s 型号:%s@@'%(listStr[0][1].strip(),listStr[2][1].strip(),listStr[1][1].strip())
        strSql = 'update tdm_device set note = \'' + str1 + str(row[14]) + '\'' +  ' where host = \'' + row[3] + '\''
        print strSql
        print "sql execute:",cursor_update.execute(strSql)
'''
    for col in row:
        print
        if isinstance(col, cx_Oracle.LOB):
            print str(col)


'''
cursor_update.execute("commit")
cursor.close()
cursor_update.close()
con.close()





阅读全文
0 0
原创粉丝点击