执行一次
import os
import pymssql
f1 = open('ddd.txt','r')
a1 = f1.read().split(',')
connect = pymssql.connect(a1[0], a1[1], a1[2], a1[3]) #建立连接
cursor = connect.cursor() #创建一个游标对象,python里的sql语句都要通过cursor来执行
if connect:
print("数据库连接成功!")
f = open('sss.txt','r')
a = f.read().split(',')
num= len(a)
for i in range(num):
ds = os.system('ping -n 1 '+a[i]+'')
print(ds)
print(a[i])
if ds==1:
sql = "UPDATE [dbo].[TEST] SET ServerState = 0 WHERE IP = "+"'"+a[i]+"'"
print(sql)
else:
sql = "UPDATE [dbo].[TEST] SET ServerState = 1 WHERE IP = "+"'"+a[i]+"'"
print(sql)
cursor.execute(sql) #执行sql语句
connect.commit() #提交
cursor.close()
connect.close()
os.system("pause")
一直执行
import os
import pymssql
f1 = open('数据库配置信息.txt','r')
a1 = f1.read().split(',')
connect = pymssql.connect(a1[0], a1[1], a1[2], a1[3]) #建立连接
cursor = connect.cursor() #创建一个游标对象,python里的sql语句都要通过cursor来执行
if connect:
print("数据库连接成功!")
f = open('IP.txt','r')
a = f.read().split(',')
num= len(a)
while 1 :
for i in range(num):
ds = os.system('ping -n 1 '+a[i]+'')
print(ds)
print(a[i])
if ds==1:
sql = "UPDATE [dbo].[TEST] SET ServerState = 0 WHERE IP = "+"'"+a[i]+"'"
print(sql)
else:
sql = "UPDATE [dbo].[TEST] SET ServerState = 1 WHERE IP = "+"'"+a[i]+"'"
print(sql)
cursor.execute(sql) #执行sql语句
connect.commit() #提交
数据库配置文件
(local),sa,admin12!,SCADA_Other
IP文件
10.29.170.130,10.29.170.131