连接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()
#!/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:
if isinstance(col, cx_Oracle.LOB):
print str(col)
'''
cursor_update.execute("commit")
cursor.close()
cursor_update.close()
con.close()
阅读全文
0 0
- 连接oracle数据库,执行ssh命令,然后把查询结果反写数据库-批量处理管理测试设备
- JAVA连接oracle数据库,并执行查询
- 使用ssh远程执行命令批量导出数据库到本地
- JAVA数据库处理(连接,数据查询,结果集返回)
- oracle数据库查询结果分页处理(Java +Jsp +Tag)
- PHP mysqul连接数据库,执行查询语句,打印返回结果集和断开数据库
- oracle数据库管理命令
- ssh批量登录机器并返回命令的执行结果
- 批量ssh执行命令
- 使用vb连接oracle数据库,执行多个命令
- Python实现ssh登录执行shell命令并将结果写入mysql数据库
- asp.net连接查询SQL数据库,把结果显示在网页上
- Java连接MySQL数据库并查询结果
- Java连接MySQL数据库并查询结果
- Oracle数据库中把一个查询结果插入到一张表中
- Oracle数据库中把一个查询结果插入到一张表中
- Oracle数据库sqlpls中批量执行
- python ssh 批量连接linux服务器执行命令
- Spring--AOP-前置通知
- IGMP协议
- (133)定向光源
- 000046:用带有标签的break跳出外层循环
- uint_32定义的说明
- 连接oracle数据库,执行ssh命令,然后把查询结果反写数据库-批量处理管理测试设备
- 算法细节系列(25):加减乘除
- 利用dropload.min.js实现下拉分页
- CentOS6.5搭建LNMP
- 使用JS 返回上一步上一个网页
- JSP九大内置对象
- ExtJS Date(日期)对象常用函数
- JDBC连接SqlServer2000 sp4以下版本
- js 高德地图根据后台坐标绘制轨迹