python이paramiko를 이용하여 교환기 순찰 검사를 실현하는 예시

직접 코드

#-*- coding:UTF-8 -*-
import paramiko
import time
starttime = time.strftime('%Y-%m-%d %T')
start_info = " :"+str(starttime)
cmd_filepath = r"d:\Python\py\xunjian\cmd.txt"
cmd_file = open(cmd_filepath,"r")
cmds = cmd_file.readlines()
dev_filepath = r"d:\Python\py\xunjian\device_info.txt"
dev_file = open(dev_filepath,"r")
while 1: 
  dev_info = dev_file.readline()
  if not dev_info :
    break
  else :
    devs = dev_info.split(',') 
    ip = devs[0]
    username = devs[1]
    password = devs[2].strip()
    password = password.strip('
') ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname = ip,username = username,password = password) print(" ",ip) command = ssh.invoke_shell() time.sleep(3) command.send('N
') # command.send('screen-length 0 temporary
') # for cmd in cmds: command.send(cmd+'
') time.sleep(5) output = command.recv(65535).decode() log = open(r"d:\Python\py\xunjian\\"+ip+".txt",'a') endtime = time.strftime('%Y-%m-%d %T') end_info = " :"+str(endtime) log.write(start_info+'

'+output+'

'+end_info) log.close() dev_file.close() # cmd.txt display device display environment display alarm urgen display memory-usage display cpu-usage display logbuffer level 0 display logbuffer level 1 display logbuffer level 2 display logbuffer level 3 display logbuffer level 4 # device_info.txt 192.168.10.11,admin,Huawei@123 192.168.10.12,admin,Huawei@123 192.168.10.13,admin,Huawei@123 192.168.10.14,admin,Huawei@123
이상은python이paramiko를 이용하여 교환기 순찰을 실현하는 예시의 상세한 내용입니다. 더 많은paramiko 교환기 순찰에 관한 자료는 저희 다른 관련 글을 주목해 주십시오!

좋은 웹페이지 즐겨찾기