January 6th, 2009, by mlambie
For as long as I can remember I’ve used vim (or vi) as my editor of choice when on Linux or UNIX systems. I’ve also used bash as my shell, except in circumstances where it wasn’t available. We’ve been using Macs for a long time now, and one of the things that I only recently learnt was that you can use CTRL-a to take you to the beginning of a line in a terminal. This meant I no longer leant on the left arrow key to get me back to the beginning of a long command.
I use screen to maintain remote, active connections to our various servers, and with my setup the CTRL-a trick didn’t work. I’ve just found that setting vi mode in bash will allow me to hit ESC then shift-I and take me to the beginning of a line. ESC engages vi mode, and you can navigate around the command as you would inside vi. For example, shift-I or 0 takes you to the start of the line, shift-a or $ to the end and h, l, k and j act as cursor keys.
You can engage vi mode by executing the following code, or adding it to your ~/.profile (or any other dot file that is executed upon login).
set -o vi
I always like finding new shortcuts, even if there’s some minor annoyance at my former lack of awareness. It’s like how we discovered syntax highlighting in vim after completing our uni degrees… very bittersweet.
We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.
Tags: bash, linux, Mac, terminal, vi, vim
Posted in Tips and Tricks | No Comments »
December 9th, 2008, by mlambie
Further to my previous foray into the world of Applescript, I’ve modified my server management script to now prompt me for a sudo password. Previously I would have to tab between each Terminal window and enter my sudo password, but now I enter it once and a dynamic command is generated that looks like this:
echo <password> | sudo -S clear && sudo aptitude update && sudo aptitude dist-upgrade && sudo aptitude clean'"
I don’t like that my sudo password is displayed on the screen. I could get around this by manually editing /etc/sudoers to allow for password-less aptitude. Alternatively, perhaps I could encrypt my password inside the Applescript and send it, pre-encrypted, to sudo. They’re options I guess.
You’ll notice that the first thing I do is clear the screen, but when there’s a second or so lag it means my password is bare for all to see. I’ll consider that when I run the script.
Below is an Applescript snippet which shows you how to open a dialog box and take some simple text input:
set my_password to display dialog "Please enter your password:" ¬
with title "Password" ¬
with icon caution ¬
default answer "" ¬
buttons {"Cancel", "OK"} default button 2 ¬
giving up after 295 ¬
with hidden answer
if length of (text returned of my_password) is not 0 then
display dialog "Running the application!" buttons ["OK"] default button 1
else
display dialog "You didn't enter a sudo password!" buttons ["OK"] default button 1
end if
Having spent a bit of time with Ruby lately, I don’t like the syntax of Applescript very much, though it gets the job done.
We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.
Tags: applescript, automation, management, password, server, sudo, terminal
Posted in Code, Tips and Tricks | 2 Comments »
November 16th, 2008, by mlambie
Like most programmers, I don’t like to repeat manual tasks, well, manually. We use Ubuntu Server almost exclusively on our servers and whilst package management is very simple (login, aptitude update, aptitude dist-upgrade, all done) when you have to look after many (fourteen and expanding) servers it can get repetitive.
I use Terminal (or is it called Terminal.app?) on Mac OS X as my terminal emulator, and I’ve messed around with using Applescript to open multiple tabs before, so I figured this was a problem worthy of a Sunday-morning.
What I envisaged was iterating over a list of servers and having Applescript manage a Terminal instance, opening new tabs where appropriate, and executing the upgrade command in each tab for each server. Sounds simple enough.
And it was. I used Martin Ström’s excellent article as a base for my script, and ended up with the following:
set tab_count to 0
set servers to {"astrotrain", "bumblebee", "grimlock", "ironhide", ¬
"jazz", "laserbeak", "mirage", "prime", ¬
"prowl", "ratchet", "ravage", "rumble", ¬
"soundwave", "wheeljack"}
-- Update the package list, dist-upgrade and remove the downloaded
-- packages so they're not included in the backups
set dist_upgrade to ¬
" 'sudo aptitude update &&
sudo aptitude dist-upgrade &&
sudo aptitude clean'"
-- Make our settings globally available
global tab_count, servers, dist_upgrade
-- Mainline
on main()
tell application "Terminal"
activate
repeat with server in servers
set cmd to "ssh -t " & server & dist_upgrade & " && exit"
my open_tab(cmd)
end repeat
end tell
end main
on open_tab(cmd)
tell application "Terminal" to activate
my create_new_window_or_tab()
tell application "Terminal" to ¬
do script with command (cmd) in last tab of window 1
end open_tab
on create_new_window_or_tab()
if tab_count ≤ 0 then
tell application "Terminal" to do script ""
set tab_count to tab_count + 1
else
tell application "System Events" to ¬
tell process "Terminal" to ¬
keystroke "t" using command down
end if
end create_new_window_or_tab
-- Run the mainline
main()
Apologies for the highlighting – the plugin doesn’t understand Applescript.
We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.
Tags: applescript, automation, dist-upgade, Mac, server, terminal, ubuntu
Posted in Code, Tips and Tricks | 4 Comments »
Follow Us