[精讚] [會員登入]
138

利用expect script 查詢ap數量,再送到google sheet及line群組通知

利用expect script 查詢ap數量

分享此文連結 //n.sfs.tw/16008

分享連結 利用expect script 查詢ap數量,再送到google sheet及line群組通知@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2022-12-23 13:11:34 最後編修
2022-12-09 13:57:32 By igogo
 

 

本段程式的流程:

1. 登入不同的CONTROLLER 取得三項資訊, AP數量, LICENSE USEAGE, 及EDUROAM數量,並將資料個別存成ip.item.log.txt

2.利用一段python程式,解析每一項資料再送到google sheet

3.google sheet 綁apps script, 等待送來的資料寫到google sheet及傳到line 群組中

 

1. 利用expect script 登入aruba ap controller 下指令查詢

AP數量,

#!/usr/bin/expect
match_max 100000

set machine [lindex $argv 0];
set path  [lindex $argv 1];

spawn ssh "USER@$machine"
        expect "*password:"
        send "PASSWORD\r"
        expect "*#"
        send "show ap database long | include Total\r"
        set timeout 30
        expect -re "Total APs:(.*)"
        set log "TotalAPs, $expect_out(1,string)"

set fid [open $path/logs/$machine.totalaps.log.txt w]
puts $fid $log
        send "exit\r"
expect eof

 

LICENSE USAGE

#!/usr/bin/expect
match_max 100000

set machine [lindex $argv 0];
set path [lindex $argv 1];

spawn ssh "USER@$machine"
        expect "*password:"
        send "PASSWORD\r"
        expect "*#"
        send "show license-usage client user\r"
        set timeout 1
        expect -re "User License Usage.*License Usage\\s+(\\d{1,6})"
        set usage "Usage, $expect_out(1,string)"

set fid [open $path/logs/$machine.usage.log.txt w]
puts $fid $usage
        send "exit\r"
expect eof

 

EDUROAM

#!/usr/bin/expect
match_max 100000

set machine [lindex $argv 0];
set path [lindex $argv 1];

spawn ssh "USER@$machine"
        expect "*password:"
        send "PASSWORD\r"
        expect "*#"
        send "show user-table essid eduroam unique | include \"User Entries\"\r"
        set timeout 15
        expect -re "User Entries:\\s+(\\d+)/"
        set usage "eduroam, $expect_out(1,string)"

set fid [open $path/logs/$machine.eduroam.log.txt w]
puts $fid $usage
        send "exit\r"
expect eof

 

 

getDataAndSend.py

#!/usr/bin/python3

import requests
import json
import os.path
import time
import sys


class Item:

    def __init__(self, ip, totalaps, usage, eduroam):
        self.ip = ip
        self.totalaps = totalaps
        self.usage = usage
        self.eduroam = eduroam


machines = []
items = []


def getTarget(target): 
    filename = "/root/apscript/logs/" + machine + "." + target + ".log.txt"

    if (os.path.isfile(filename)):
        file = open(filename)
        rows = file.readlines()
        return (rows[0].split(",")[1].strip())
    else:
        return 0

machine = sys.argv[1]
machines.append(machine)



for machine in machines:
    totalaps = getTarget("totalaps")
    usage = getTarget("usage")
    eduroam = getTarget("eduroam")
    item = Item(machine, totalaps, usage, eduroam)
    items.append(item)

url = 'google apps script 部署的網址'

for item in items:
    params = {
        "ip": item.ip,
        "totalaps": item.totalaps,
        "usage": item.usage,
        "eduroam": item.eduroam
    }
    response = requests.get(url, params=params)
    print(response.text)
    time.sleep(2)

 

main.sh

#!/bin/bash

path="/root/apscript";

machines=("IP");
machines+=("IP");


/bin/rm  $path/logs/*.txt

for machine in "${machines[@]}"
do
        "$path/scripts/totalaps-expect.sh" $machine $path
        "$path/scripts/usage-expect.sh" $machine $path
        "$path/scripts/eduroam-expect.sh" $machine $path
         $path/getDataAndSend.py $machine
         sleep 1

done


 

 

google apps script

function doGet(e) {

  Logger.log("get method is called");
  Logger.log(JSON.stringify(e));

  machine = e.parameter;
  update(machine);
  send(machine);
  return ContentService.createTextOutput(JSON.stringify(machine));

}

let spreadSheet = SpreadsheetApp.openByUrl("google sheet url");

date = Utilities.formatDate(new Date(), "GMT+8", "yyyy/MM/dd EEEE")

function update(machine) {

  let sheetName = "排程" + machine.ip.split(".")[3];
  let sheet = spreadSheet.getSheetByName(sheetName);
  let lastRow = sheet.getLastRow() + 1;


  sheet.getRange(lastRow, 1, 1, 1).setValue(date);
  sheet.getRange(lastRow, 2, 1, 1).setValue(machine.totalaps);
  sheet.getRange(lastRow, 3, 1, 1).setValue(machine.usage);
  sheet.getRange(lastRow, 4, 1, 1).setValue(machine.eduroam);

}

function send(machine) {
  let token = "TOKEN";

  let message = "\n機器資訊\n";
  message = message +
    "IP: " + machine.ip + "\n" +
    "AP數量:" + machine.totalaps + "\n" +
    "裝置數: " + machine.usage + "\n" +
    "eduroam人數: " + machine.eduroam + "\n";

  var option = {
    method: 'post',
    headers: { Authorization: 'Bearer ' + token },
    payload: {
      message: message
    }
  };
  UrlFetchApp.fetch('https://notify-api.line.me/api/notify', option);
}

 

END

你可能感興趣的文章

資料表更改為多個primary key, MariaDB [database]> describe TABLENAME; 想由本來是兩個PRIMARY KE

臺中市校務雲端系統與Windows AD帳號整合(1) active directory,ldaps,雲端校務系統

對照mac address 批次更改電腦名稱 讀取mac address 對照表, 還原電腦後,自動更改密碼 'show logged on user Dim

proxmox zfs 建立異地備份與還原 zfs最吸引人的地方就是可以建立快照並傳輸到遠端机器, 達到增量備份, 並且隨時從任一備份檔還原 完全不怕勒索病毒 將v

shell scirpt 批次建立samba使用者 建立一csv檔, 兩個欄位如下 5101,532 5102,592 5103,321 第一欄為帳號使用班級加座號, 第二

proxmox lxc 救援 今天突然接到一名強者我朋友的臨時求援 他說他把pve 從5.1升到5.2後, kvm的虛擬机器都沒問題 , 但是lxc的

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

centos 7 移机出現 dracut-initqueue timeout centos 7 移机出現 dracut-initqueue timeout 處理

vue.js component 在parent與child 傳值 component 在parent與child 傳值

java.time 時間 instant java.time

proxmox lxc 救援 今天突然接到一名強者我朋友的臨時求援 他說他把pve 從5.1升到5.2後, kvm的虛擬机器都沒問題 , 但是lxc的

臺中市雲端校務系統與Windows AD帳號整合(5) 安裝java環境