And now for a bit of self taught nerdy goodness…
I just managed to get something kind of cool working.
We have a cool tool in the pipeline, but in the meantime my hacked together python efforts will have to do! 😉
A few months ago I created a python script for building ‘job’ directories – for new jobs that came into the building – a script that runs in a shell.
I then learnt about wxpython and turned the script into a GUI app.
This was cool to run on my machine, but I needed a way for it to be used by other artists / producers.
We got an xServe and some disk storage so I put the script onto the xServe. I was able to log in via VNC and run the script to create the job directories that everyone could access. This gave us all a place to put files in a structured manner and not let things get too messed up (thanks to file permissions).
But the problem was that for me to execute the script, I’d have to VNC or SSH into the server. I didn’t mind doing it, but it was not so useful when I wasn’t around.
So back to this ‘new job’ script….
I just worked out how to setup a producer’s mac to be able to “remotely execute the python ‘new job’ script on our xServe” using SSH and python.
All a producer has to do is type into Terminal:
and it all starts up. The producer is asked the ‘name of the job’ and ‘the number of shots’ and the script goes ahead and builds all the directories.
How I did it
Getting into the xServe.
I realised that I would not be able to do too much to the file structure of the xServe’s file system if I was to simply mount it on a Desktop – due to permissions etc… and I also didn’t want ‘the user’ to have to mount anything manually. It needed to brainless. SSH seemed the best way.
I found out pretty quickly that there wasn’t an easy way to get out of entering the ‘admin password’ for the xServe to remotely execute the script (you have to enter a password when you ssh into another machine).
I didn’t want to give away our admin password for the xserve. So I read up on SSH and discovered SSH-Key’s.
A cool way to authenticate your machine to the remote machine without having to enter the ‘login password’ for the remote user. Instead you give the SSH connection a ‘passphrase’. Initially you have to login to the remote machine with the remote password, but after that it’s all ‘ about ‘enter your passphrase for key’.
To set up the RSA key for your local machine I followed this easy to read tutorial from IBM.
Basically you need to generate a public key and add that to the list of ‘known computers’ on the remote machine.
To generate the public key (according to the IBM article) run this command in a shell:
$ ssh-keygen -t rsa
and follow the prompts.
You can choose to not have a passphrase, but that seemed a little too much of a security risk for our setup.
Next I got a bit confused, the command spat out 2 files into a ‘.ssh’ directory in your home folder:
These are the default names and location if u DON’T enter a name for the key.
To get the public key (the .pub file) into the list of known machines on the xServe I mounted the xServe’s Admin’s home folder on the local machine.
I got into the .ssh directory in the home folder of the xserve, and went looking for the file:
but couldn’t see it.
So i created a file called ‘authorized_keys’ using the following command:
Then to add the public key I ran a ‘cat’ command to append the public key file to the ‘authorized_keys’ file on the server.
It worked! I was able to login the the xServe using the passphrase. Shweet!
I found this post on an apple forum also very helpful.
Executing the script
On the remote machine / xServe is where the python script lives – in a folder called ‘scripts’ in the home folder.
To run this script in a shell on the xServe I would type:
So I figured from the local machine the command to execute using ssh would be:
ssh firstname.lastname@example.org python /Path/to/the/script/newjob.py
And that did work.
But that was wayyyyyy too long for or our Producer to have to type up.
So I made bash alias in the bash_profile:
alias newjob ssh email@example.com python /Path/to/the/script/newjob.py
i tried the alias, and it connected, but it didn’t seem the script was doing anything. But it was. I couldnt see the interactive text prompting me to enter a ‘jobname’ or ‘amount of shots’, but it WAS asking for it.
Why couldn’t I see the text? I was stumped. Why wasn’t the shell showing me the feedback from the xServe?
I read the man pages of SSH and found out a bunch of options you can pass the ssh command. Somehow I came across this:
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
As soon as I put that into the command it all worked!
ssh -t firstname.lastname@example.org python /Path/to/the/script/newjob.py
The last thing I did to make this even easier was to create a ‘.command’ file in the OSX Finder, that can just be double clicked and it will open Terminal.app and run the above command.
2 thoughts on “some Python & SSH remote machine coolness”
Man, you are one smart cookie.
Wow, you should start writing your own “* for Dummies” series! 🙂