uptime conversion

Discuss Programming
Post Reply
X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

uptime conversion

Post by X11 »

I want to get a script that gets your uptime and converts it into minutes, then it will use netcat to send it to the uptimes project (uptimes.wonko.com). I understand the protocol and how it works.

My problem is that I dont even know where to start with manipulating strings in sh. I guess it would have to do the following...

Get the uptime in days multiply it by 24
get the uptime in hours add the above's result and multiply it by 60
get the uptime in minutes add the above's result and send it to the uptimes server
(one day i hope programming is this easy).

Thanks in advance.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

I think this should work to get your uptime into minutes:

Code: Select all

#!/bin/sh

# Get up days hours and minutes
updays=`uptime | tr -s ' ' | cut -f4 -d' '`
uphours=`uptime | tr -s ' ' | cut -f6 -d' ' | cut -f1 -d':'`
upminutes=`uptime | tr -s ' ' | cut -f6 -d' ' | cut -f2 -d':' | cut -c1-2`

# Use expr command to get total minutes system has been up
totalupminutes=`expr $updays \* 24 \* 60 + $uphours \* 60 + $upminutes`

# or use bc command to get total minutes system has been up
bctotalupminutes=`echo "$updays * 24 * 60 + $uphours * 60 + $upminutes" | bc`

# spit out answers to all of the above
echo $updays $uphours $upminutes $totalupminutes $bctotalupminutes
I don't know anything about wonko so.... Also, the above should work on Linux but it may need to be adjusted for other OSs that may format the uptime output slightly differently.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

http://uptimes.wonko.com/protocol.php - Theres the protocol, you can use netcat, telnet to cheat, or get the uptime-chaos client as its written in perl and easy to change. As is the Linup client program.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Client program...

Post by X11 »

Code: Select all

#!/bin/sh
# uptime script

authkey=""

updays=`uptime | tr -s ' ' | cut -f4 -d' '`
uphours=`uptime | tr -s ' ' | cut -f6 -d' ' | cut -f1 -d':'`
upminutes=`uptime | tr -s ' ' | cut -f6 -d' ' | cut -f2 -d':' | cut -c1-2`

totalupminutes=`expr $updays \* 24 \* 60 + $uphours \* 60 + $upminutes`

load="0"
idle="0"
os=`uname -s`
oslevel=`uname -r`
cpu=`uname -p`

client="upscript-0.1"

uptimestring="$authkey|$totalupminutes|$load|$idle|$os|$oslevel|$cpu|$client"

echo "$uptimestring" | nc uptimes.wonko.com 49153

Post Reply