You are not logged in.

#3226 2019-01-27 23:48:27

teckk
Member
Registered: 2013-02-21
Posts: 300

Re: Post your handy self made command line utilities

A little bit better scripts for that device.

#! /usr/bin/bash

#Firewall log printer for Arris BGW210-700
#Prints log to terminal and logs to file continuously until Ctrl C
#Needs curl, awk, comm

#BGW210 Gateway LAN IP
url="http://172.16.0.1"

#Firewall log
fw_log="f.txt"

#Retrieve interval for log 
sleep_time=60

#Print char x times
str=$(printf "%90s")

#Get log data from BGW210, get data between td tags with awk
Get_Log() {
    curl -s "$url"/cgi-bin/logs.ha | 
        awk -v OFS='\t' -F '[<|>]' '/td/{
            print $9, $13, $17, $21, $25}' | 
                tail -n 30 #| grep 'Policy (filtersets, etc.)'
                            #| grep '172.16.0.30'
                            #| sort -k 2,2 -k 1,1
                            #| grep 'UDP'
                            #| grep 'TCP
}

#Get active devices
Active() {
    curl -s "$url"/cgi-bin/home.ha |
        awk -v OFS='\t' -F '[<|>]' '/td scope="row" class="col2"/{
            print $5, $9, $13, $17, $19}'
}

#Main loop. Get logs, compare, print diff.
b=""
while :; do
    a=$(Get_Log) 
    echo "Tail of firewall log:"    
    comm --nocheck-order -13 <(echo "$b") <(echo "$a") | tee -a fw_log
    echo "Update in "$sleep_time" seconds:"
    echo "${str// /─}"
    sleep "$sleep_time"
    
    act=$(Active)
    echo "Active devices on network:"
    echo "$act" | awk '{gsub(/&nbsp;/,""); print}'
    echo "Update in "$(($sleep_time * 2))" seconds:"
    echo "${str// /─}"
    
    b=$(Get_Log) 
    echo "Tail of firewall log:"  
    comm --nocheck-order -13 <(echo "$a") <(echo "$b") | tee -a fw_log
    echo "Update in "$sleep_time" seconds:"
    echo "${str// /─}"   
    sleep "$sleep_time"  
done
#! /usr/bin/python

#Firewall log printer for Arris BGW210-700
#Prints log to terminal continuously until Ctrl C

import urllib.request
import lxml.html
from time import sleep

#BGW210 Gateway LAN IP
url = 'http://172.16.0.1'

#Retrieve interval for log 
sleep_time = 60

class GetBGW210Log():
    def __init__(self):
        #Get log data from BGW210 with urllib
        def Get_Log():
            req = urllib.request.Request((url)+'/cgi-bin/logs.ha')
            html = urllib.request.urlopen(req).read()
            root = lxml.html.fromstring(html)
            
            #Get data between td tags with lxml, append to list
            log = []
            for i in root.xpath('//td'):
                log.append(i.text_content())
                
            #Make rows out of items in log, append to new log
            logb = []
            def rows(data, rowsize): 
                while data:
                    row, data = data[:rowsize], data[rowsize:]
                    yield row
            for row in rows((log), 6):
                logb.append(row)
                
            #Return last 30 items in list
            return logb[-30:]
            
        #Get devices on LAN
        def Device():
            dlog = []
            req = urllib.request.Request((url)+'/cgi-bin/home.ha')
            html = urllib.request.urlopen(req).read()
            root = lxml.html.fromstring(html)
            
            log = []
            for i in root.xpath('//td[@class="col2"]'):
                log.append(i.text_content())
                
            def rows(data, rowsize): 
                while data:
                    row, data = data[:rowsize], data[rowsize:]
                    yield row
            for row in rows((log), 5):
                print (*row, sep='\t')
                
        #Main loop. Get logs, compare, print diff.
        blog = []        
        while True:
            alog= Get_Log()
            new1 = ([x for x in alog+blog if (x not in blog)])
            print ('Tail of firewall log:')
            for i in new1:
                print (*i, sep='\t')
            print ('Update in', (sleep_time), 'seconds:')
            print('─' * 90)
            sleep(sleep_time)
            
            print ('Active devices on network:')
            Device()
            print ('Update in', ((sleep_time) * 2), 'seconds:')
            print('─' * 90)
            
            blog = Get_Log()
            new2 = ([x for x in alog+blog if (x not in alog)])
            print ('Tail of firewall log:')
            for i in new2:
                print (*i, sep='\t')
            print ('Update in', (sleep_time), 'seconds:')
            print('─' * 90)   
            sleep(sleep_time)
        
if __name__ == "__main__":
    GetBGW210Log()

Offline

#3227 2019-02-12 16:14:42

bedtime
Member
Registered: 2019-02-12
Posts: 14

Re: Post your handy self made command line utilities

Automatically run YouTube video in VLC after copying the link.

Install xclip:

# pacman -S xclip

Run the script:

#!/bin/sh

# xclip needs the clipboard to be filled or it complains:
echo makexcliphappy | xclip -i -selection clipboard

while :;do
	
	# '-selection' parameter make take primary, secondary, or clipboard
	clip=$(xclip -o -selection clipboard)
	
	[ "$clip" != "$prevclip" ] && [ $(echo $clip | awk '/watch\?v=/||/youtu\.be/') ] && (vlc --play-and-exit $clip)&

	prevclip=$clip

	sleep 1

done

Now, when you want to watch a YouTube video in VLC just highlight and copy the main link, and it will load up in VLC player. smile

*** EDIT ***
Added code so xclip doesn't complain at startup due to empty clipboard.

Last edited by bedtime (2019-02-12 20:47:31)

Offline

Board footer

Powered by FluxBB