oscartheduckin’ around

May 15, 2007

sometimes it takes a sledgehammer, sometimes it takes a

Filed under: python, SiReSSH — oscartheduck @ 1:57 am

I’ve been working on SiReSSH for a while, noticed a few bugs in it today, it’s nice. I like what I’ve done with it thus far, and I’m glad that it exists.

But.

Today at work I wanted a fast solution to the same problem. By which I mean, the version I have uploaded has bugs in that I didn’t want to have to fix it.

Sometimes the world becomes a simpler place when you want to start again. I wanted to just write a script to do the same thing, and I wanted it to be done within about five minutes. I started with a shell script and almost instantly didn’t want to write it that way, because I really like python’s syntax. So instead, I used my remaining four minutes and I wrote this:

————

#!/usr/bin/env python

import os
# a note: though it seems counter intuitive, remember that
# if the if statement evaluates to 1, the programming language considers
# the statement true and runs the first stuff, if it evaluates to 0
# the language thinks of the statement as falkse and runs the else
# so that a nice way to think of this is that the if statement reads
# “If false, do this, else do that”

if os.system(“sockstat -l | grep 2024”):
os.system(“/usr/sbin/sshd -p 2024”)
else:
pass

os.system(“su james -c ‘ssh -nNT -R 2024:localhost:2024 james@THE MACHINE I WAS CONNECTING TO.com‘”)

——–

Now, anyone who wants to can use this code. It’s such a simple thing that I think it’d take hubris for me to assume I had the right to license it, but if I do then consider it licensed under the FreeBSD license ( http://www.freebsd.org/copyright/freebsd-license.html ).

I was amazed, at the time and still am I guess. I have this monstrous piece of code in SiReSSH, because it’s for the general case, but here I have a few lines that work for a specific case. And I by far prefer the specific case.

I have it set to run as an @reboot cron job, so that’s cool. It implements a neat feature to check whether or not an ssh daemon is listening on an appropriate port, if it isn’t then make one otherwiseuse the current one. I could probably use the argv array and just make it a command line tool that takes a few arguments, but it does my job right now.

I wrote a shell script the other day, too. I wanted to update my ports collection, and use that to update all my software on my server. What I used was basically:

portsnap fetch

portsnap extract

portsnap update

portupgrade -ay

pkgdb -F

And that’s that. Sometimes, simple is so nice to look at, it’s nice.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: