script ยท bash

Script to emulate 'tail -f' over http/ftp

Problem

You need to get file over http and get it updates in realtime, like tail -f does it. Very useful if you have access to you logs only over http or ftp.

Solution

Script tail-via-http.sh
Emulate tail -f over http, to get updatable data via http.

Usage

./tail-via-http.sh http://server.io/access.log OFFSET UPDATE_TIME

Exapmle

You have log on server http://server.io/access.log

./tail-via-http.sh http://server.io/access.log 30000 1
Where

http://server.io/access.log - file address

30000 - offset in 30000 bytes

1 - file will update every 1 second

Result

You viewing file updates in realtime. Like you use tail -f command

Source

#!/bin/bash
url=$1  
offset=$2  
interval=$3

function getLength() {

    url=$1
    ret=`curl -s -I -X HEAD $url | awk '/Content-Length:/ {print $2}'`
    echo $ret | sed 's/[^0-9]*//g'
}

function print() {

    url=$1
    offset=$2
    length=$3

    curl --header "Range: bytes=$offset-$length" -s $url
}

length=`getLength $url`  
off=$((length - offset))


until [ "$off" -gt "$length" ]; do  
    len=`getLength $url`

    if [ "$off" -eq "$len" ]; then
        sleep $interval
    else
        print $url $off $len
    fi

    off=$len
done  

You can always download it and other script from my GitHub Repository

Published:
comments powered by Disqus