#-*- coding:utf8 -*- import paramiko import re def check_active_ac(intervals=1): client = paramiko.Transport(('主备的虚拟ip', 22)) # paramiko模块提供了ssh及sft进行远程登录服务器执行命令和上传下载文件的功能 client.connect(username='username', password='password') # 打开一个通道 chan = client.open_session() chan.settimeout(5) # 获取终端 chan.get_pty() # 激活终端,这样就可以登录到终端了,就和类似于xshell登录系统一样 chan.invoke_shell() chan.recv(65535) # 设置缓冲区大小 chan.send('show high-availability status \n') # 执行'show high-availability status'命令,查看高可用状态 output = chan.recv(65535).decode() print(output) # 退出终端 chan.send('exit \n') client.close() search_string = output.split('\n')[7] '''print('\n')''' '''print("4位:", search_string)''' if re.search(r'primary', search_string, flags=re.IGNORECASE): '''re.IGNORECASE忽略大小写''' active_ac = '主ip' '''print("主ip: Primary active")''' elif re.search(r'secondary', search_string, flags=re.IGNORECASE):
active_ac = '备ip' '''print("备ip: Secondary active")''' else: #print("0") return test = check_active_ac()