导入paramiko
import paramiko
获取远程ip
class Excel(object):
def __init__(self, local_path, sheet_name):
'''
:param local_path: 文件本地路径
:param sheet_name: 工作册名称
'''
self.local_path = local_path
self.sheet_name = sheet_name
def read_excel(self):
excel = xlrd.open_workbook(self.local_path)
table = excel.sheet_by_name(self.sheet_name)
L = []
for rowNum in range(0, table.nrows):
list = {'地址': None, '用户名': None, 'kylin密码': None, 'root密码': None}
for colNum in range(table.ncols):
if colNum == 0:
list['地址'] = table.cell_value(rowNum, colNum)
elif colNum ==1:
list['用户名'] = table.cell_value(rowNum, colNum)
elif colNum == 2:
list['kylin密码'] = table.cell_value(rowNum, colNum)
elif colNum == 3:
list['root密码'] = table.cell_value(rowNum, colNum)
L.append(list)
return L
连接物理机
class Check():
def __init__(self, Server_ip, Server_port, username, password, local_path, remotepath):
'''
:param Server_ip: 服务器ip
:param Server_port: 服务器端口
:param username: 用户名
:param password: 密码
:param local_path: 上传文件的本地路径
:param remotepath: 目标路径
:param DB_ip: 数据库ip
:param DB_user: 用户名
:param DB_password: 密码
:param DB_db: 库
'''
self.Server_ip = Server_ip
self.Server_port = Server_port
self.username = username
self.password = password
self.local_path = local_path
self.remotepath = remotepath
self.DB_ip = DB_ip
self.DB_user = DB_user
self.DB_password = DB_password
self.DB_db = DB_db
self.transport = paramiko.Transport((self.Server_ip, self.Server_port))
self.transport.connect(username=self.username, password=self.password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
self.ssh = paramiko.SSHClient()
self.ssh._transport = self.transport
self.conn = mysql.connector.connect(host=ip, user=username, password=password, database='system')
def do_toll_check(self):
stdin, stdout, stderr = self.ssh.exec_command("df -Th")
print(str(stdout.channel.recv(1024), encoding='utf-8'))
return str(stderr)
def conf_check(self, ssh):
print("开始检查程序")
stdin, stdout, stderr = ssh.exec_command("df -Th")
print(str(stdout.channel.recv(1024), encoding='utf-8'))
return str(stderr)
def update_file(self):
self.sftp.get(self.local_path, self.remotepath)
def do_DB_check(self):
try:
cur = self.conn.cursor()
sql1 = 'SELECT * FROM system.mc_allroadinfo_version'
print("检查最短路径费率版本表:{}:".format(sql1))
cur.execute(sql1)
self.print_sql_result(cur.fetchall())
except ProgrammingError as e:
print('检查最短路径费率版本表失败')
def print_sql_result(listall):
for sql_result in listall:
print(sql_result)