Linux Blog

APC Access Temperature Query and Conversion. (1 of 2)

Filed under: Shell Script Sundays — at 10:54 pm on Sunday, February 1, 2009

This week I present to you; dearest reader part of a script I wrote to monitor the temperature on APC brand UPS’s. The script requires the apcupsd package to be installed and properly configured.

Here is the script:

f () {
echo $(echo "($(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)*1.8)+32" | bc);
c () {
echo "$(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)"
case "$1" in
echo "$(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)" C
echo $(echo "($(/usr/sbin/apcaccess | grep ITEMP | cut -d : -f 2 | cut -d \  -f 2)*1.8)+32" | bc) F;
echo $"Usage $0 {f|c|b}"

The script uses basic bash, grep, cut and bc. It requires only one input, and that is f, c or b. f for Fahrenheit, c for Celsius and b for both. The second part of the script is a cron, with some more basic bash. It write a log and e-mails me if the temperature goes over a certain threshold, and it e-mails me again once the temperature has been resumed. I will post the cron script next week as it is a major portion. The tricky part of the cron was making it e-mail me only once.

Until next time, Happy Scripting!


Linux ipconfig

Filed under: General Linux,Linux Software — at 7:32 am on Thursday, December 13, 2007

I’m both proud and annoyed that there is no Linux ipconfig command:

owen@linux-blog:~$ ipconfig
-bash: ipconfig: command not found

I’m proud to be different but it annoys me because I like to see standards between operating systems. Luckily the Linux ipconfig utility is easy to remember. On most Linux distributions it is ifconfig. I’m not exactly sure why its called ifconfig but I remember it as the word “if” or “InterFace”. If you try to execute the command from your shell you might get this error:

owen@linux-blog:~$ ifconfig
-bash: ifconfig: command not found

If this happens don’t be alarmed the problem is probably that you are logged into your regular user account and not root. To run as root I would use sudo, or su to switch to the root user and try again. There is a trick to get ifconfig to work with a regular user but no changes to the configuration will be able to be made. The path to ifconfig and trick success rate varies by distribution, some high security distributions will not let you do this trick:

owen@linux-blog:~$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:10:B5:70:B0:79
          inet addr:  Bcast:  Mask:
          RX packets:1609257 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1640883 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:236511428 (225.5 Mb)  TX bytes:422972120 (403.3 Mb)
          Interrupt:11 Base address:0xee00
lo        Link encap:Local Loopback
          inet addr:  Mask:
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:34516 errors:0 dropped:0 overruns:0 frame:0
          TX packets:34516 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:13210707 (12.5 Mb)  TX bytes:13210707 (12.5 Mb)

For an IPConfig that is a lot of data. If you know the interface that you want the data for, you can just specify the name with the command like so:

owen@linux-blog:~$ /sbin/ifconfig eth1

There is a lot more to this command then first meets the eye. I’ll go into detail about networking and the Linux ipconfig utility in another blog post. Thanks for reading, show me some love by leaving me a comment!

Snippet: Keeping SSH Running

Filed under: Quick Linux Tutorials,Shell Script Sundays — at 12:53 am on Sunday, November 4, 2007

I wrote a post not so long back called Bringing The Internet Up After Failure that explained how I was restarting my network services after the internet went down.

Shortly after this I was remotely working when I thought it would be a good idea to restart my SSHD to enable X11 forwarding. After running the script that normally restarts the service I tried to reconnect. Unfortunately the service never restarted after being stopped. This is not a good situation for any one to be in so I added something like this to my cron along at the end of my network services script:

ps ax | grep \usr\/sbin\/sshd | grep Ss && echo "SSHD Running" || echo "Starting SSH"; sudo /etc/rc.d/rc.sshd start

The only difference between the version I am running and the version above is that I don’t echo anything out. All the command does is use the ps command and grep for usr/sbin/sshd then grep for the STAT field of sS. I do this because sshd shows up in the process list as shown below:

owen@the-linux-blog$ ps ax | grep sshd
3463 pts/0    R+     0:00 grep sshd

The rest is self explanitory, it echo’s “SSHD Running” or sudo starts the SSHD.

If SSHD ever terminates on me or those of us who run this snippet we will be able to rightfully regain access to our systems, Hurray!