uptime conversion

Discuss Programming

uptime conversion

Postby X11 » Sat Jul 12, 2003 7:10 am

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.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Sat Jul 12, 2003 7:33 am

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.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Sat Jul 12, 2003 5:32 pm

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: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Client program...

Postby X11 » Thu Jul 17, 2003 12:46 am

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
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests