Now that I’ve joined the hipster gang and have a MacBook Pro, I’ve also had to adjust my habits in terms of terminal program. On Windows I’ve been using PuTTY for years as my default telnet/ssh client, partly because it’s a stunningly good, lightweight program (huge tip of the hat to Simon Tatham here, along with anybody else who had contributed to that project). But what to use on the Mac? I started off using the built in Terminal which actually isn’t bad at all, but in the end I was encouraged to try iTerm2 and I am very happy with it for my needs.
So with that said, here are a few tips for iTerm2 that I find valuable as a network engineer.
Session Logging
In PuTTY I didn’t set up logging by default in any of my profiles. I don’t know if you can automatically create unique filenames to stop conflicts when you have two sessions using the same profile, but then for some reason I never tried. In iTerm2 though, there’s an option to enable logging of sessions by default. Each one is date stamped and is unique for each session, so six open windows means six session files. The down side is that this means lots of files, but on the other hand it means I am always keeping a record of what I saw, what commands I have issued, and so forth, and there’s never a conflict. Especially if you make network changes and need to keep a session log, this is a real win, because it means you never forget to log the session!
It’s easy to enable; go to the iTerm menu > Preferences. Choose Default (or whichever profile you want to edit), and click on the Session tab on the right hand side. Check the option under Miscellaneous and choose a file path to store your log files:
We aren’t running machines with 64KB of memory any more nor with meagre storage, so why PuTTY’s default profile still only has a teeny tiny scrollback history, I just don’t know. iTerm falls into the same trap; perhaps I’m unusual in wanting to be able to scroll back through an entire configuration after I’ve viewed it? So while you’re adding session logging, I suggest you pop to the Terminal tab and select Unlimited Lines for your scrollback buffer:
You’ll thank me eventually. Yes, you can easily log on demand (Shell menu > Log > Start, then give it a file name), but this is much simpler in my opinion.
Tracking iTerm2 Log Files
As I mentioned above, logging every session by default is great but does create a log of files, so how are you going to find the one you want? Each file is dated, so that’s a good starting point, but if like me you keep terminal windows open over a period of days and weeks (simply because OSX makes it so gosh darned easy to just shut the lid and walk away then open it the next day and carry on where you left off) then you may have files whose start date is not the same as the data you are looking for. It’s important then to be able to search for what you’re looking for.
For example, let’s say that you executed a network change and two days later a problem arises that you think might have started during your change, and you’d like to check your session file to see what you might have missed at the time. Well, if you know the change reference, perhaps you could use that as your search term. But how does that get into the change?
For me, it’s simple. When executing a change, add a comment including the change name. If connected to a Cisco IOS device, you can do this at a prompt without any harm:
router# ! CHG-12345 - adding int e1-3 as OSPF interfaces
Or in Junos:
john@router> ## CHG-12345 - adding int e1-3 as OSPF interfaces
Or in the shell before you connect to the router:
MacBook:~ john$ echo CHG-12345 - adding int e1-3 as OSPF interfaces
On Junos – for bonus points – you could also (should also) add the change number when you commit the configuration:
john@router# commit comment "CHG-12345 - adding int e1-3 as OSPF interfaces"
Now I can search for the change number and find all of the relevant session files quickly and easily.
Searching Log Files
How do I search the files for a specific change? Unexpectedly perhaps, the easiest way is to use the OSX Spotlight search! Command-[SPACE] to open search, then just enter the change number. All the files should be listed under Other, with a note that the filenames are pure evil (logical evil, but evil nonetheless), and are formatted along the lines of:
DATE_TIME.ProfileName.terminal.ParentProcessID.uniqueid.log
…or something like that. They won’t look nice, but they will be searched. If you’re worried that Spotlight won’t have indexed your file yet, you can jump into the shell and use grep for a immediate search, but there’s a catch. If you use iTerm to grep for a search term, you will also get back the session log for the terminal from which you are searching! In fact, if you search more than once, you will get the logging in a loop with grep where as it prints the matching lines back into your terminal, they are logged, and grep finds them again, and, well, it loops in a rather unpleasant manner.
The easiest way around that is to create a profile with logging disabled, and perhaps call it “No Logging” or similar, then open a new window using that profile. Search from that one and you won’t have any issues. For example:
john$ grep CHG-12345 ./*
./20140412_122930.Default.w0t0p0.5462.dc87fe57.log:Johns-MacBook-Pro:~ john$ echo CHG-12345 BEGIN
./20140412_122930.Default.w0t0p0.5462.dc87fe57.log:CHG-12345 BEGIN
./20140412_122930.Default.w0t0p0.5462.dc87fe57.log:Johns-MacBook-Pro:~ john$ echo CHG-12345 END
./20140412_122930.Default.w0t0p0.5462.dc87fe57.log:CHG-12345 END
john$
I did say it wasn’t pretty. Still, from this we should now be clear that the file 20140412_122930.Default.w0t0p0.5462.dc87fe57.log
contains the log for CHG–12345. Simples.
Splitting Windows
I often work on pairs of routers (router1A
and router1B
for example) and it’s helpful to have both windows up on screen at the same time, neatly sharing the screen. Windows 7 introduced the “Snap” feature, which made it very easy to drag a PuTTY window to each side of the screen and effectively split the screen. While I do use the awesome BetterSnap Tool in OSX to replicate the similar (in fact enhanced) functionality, you don’t need that in order to get the same benefit from iTerm2. Instead you can open windows that split the existing tab in half, vertically or horizontally automatically.
In the menus, you can go to Shell menu > Split Vertically With Current Profile, but more simply if you can memorize it, just do Command-D (⌘D):
The ‘active’ window (the one your typing will go into) is black, the inactive window is grayed out. You could also use the split terminal to have your change executed in one window while monitoring an interface or system logs in the other. Whatever rocks your boat, really. Splitting doesn’t stop there mind you. iTerm2 allows you an absurd amount of control. Try hitting ⌘D again?
How about horizontal splits? That’s Shift-Command-D (⇧⌘D), or you can choose it from the Shell menu again. This splits the currently active window space and opens a new terminal for you:
And there’s no stopping you (at least, not that I’ve found). If your screen is large enough you can just keep on going:
Quite why you’d do that is another questions, but the fact is that you can. And that’s what matters.
Saving Window Arrangements
By the way, you can save any window arrangement you create (Window > Save Window Arrangement) and give it a name. To open a set of terminals using your saved arrangement, simply use Window > Restore Window Arrangement and choose your preferred arrangement from the pop up list.
Controlling Multiple Windows
Ever have a change where you needed to commit the change simultaneously on more than one device? I have had quite a few of those, and iTerm2 makes it easy. Before you jump in, it’s not the only software to do this, but since I’m on the topic of iTerm, it’s going to get a mention. So, the solution here is, let’s say with Junos OS, to be able to commit your changes at the same time. In our split windows above, all windows are grayed out except for the active (black) terminal. To make your typing go to multiple windows, go to the Shell menu again and this time to Broadcast Input, and select Broadcast Input To All Panes in Current Tab, or hit Shift-Command-I (⇧⌘I):
You’ll get a warning that you’ll be sending to multiple sessions; heed this carefully. If you choose the option above it to Broadcast Input To All Panes in All Tabs, iTerm really will do what it says and will send your input to terminals on the other tabs; terminals that you can’t even see. So use this with care. Once selected, both terminals are black and have a tiny image of the Apple/Command key in the top right hand corner of each terminal, to remind you that you are broadcasting.
Now anything you type is mirrored in all sessions:
To cancel broadcast, do the finger-bendingly inconvenient Shift-Option-Command-I (⇧⌥⌘I) or go to the Shell menu > Broadcast Input and choose Send Input to Current Session Only. You might turn this on just long enough to type commit command "CHG-12345 add interfaces to OSPF"
and hit ENTER in order to get a simultaneous commit, then turn broadcast mode off again. It’s also great if you need to execute the same (preferably simple) command(s) on multiple servers; bring up 9 server logins, then execute a command once and watch it happen 9 times in parallel.
Lab Connectivity
It’s easy to get confused when jumping between terminals connected to lab and production devices. If you are disciplined though, you can create and use a profile just for lab connections. My lab profile is about as subtle as a brick; I edited the background and foreground colors on the Colors tab in the profile editor to make it obvious that this is not a normal session:
Ever get annoyed when you connect to a ScreenOS device and the backspace key doesn’t delete text? It’s easy to create a profile for Netscreen connectivity:
Need to let somebody look over your shoulder but they can’t make out what’s in your terminal? It’s easy to zoom in and out:
As with many other OSX programs, Command+ (⌘+) and Command- (⌘-) are your friends for instance zoom in (⌘+) and out (⌘-). Command-Zero (⌘0) resets to the default text size. This is so much easier than having to edit the font size in your PuTTY session. Zooming takes effect only in the active terminal, so you can scale each terminal session as you desire.
Just One More Thing
There’s much more to iTerm2 than I’ve touched on here, but these are certainly some of the more helpful things in my job. When I mentioned this on Twitter recently, I was asked whether iTerm support scripting of logins and the like. Apparently it does, but it had never struck me to make iTerm do that when I have a bash shell (and all the associated utilities) available; my auto-login scripts are all expect based!
Do you have any good tips for iTerm2?
Putty is a good one, but I prefer Poderosa, the use of tabs and multiple windows at the same time is quite useful.
iterm2 also supports tmux integration.
check it out, it’s pretty cool.
I’ll look into that. I’ve not used tmux before, but it sounds a little like ‘screen’ in what it does?
It’s pretty much the same thing but with some extra features I’ve never bothered to use. It’s also hipster and since you’ve made the move to the mac… 😉
Great tips! I would add the ssh config file is great to organize sessions and switching to zsh w/ oh-my-zsh make a huuuuge difference. Definitely check out the latter if you haven’t. Per-directory and per-command history is worth it alone. This coming from a guy who just switched to mac a few months ago. so glad i did.
I’m trying oh-my-zsh now! The git repo awareness is already showing coolness to me… 🙂 Thanks!
Very helpful, thanks! I’ve been on iTerm for a while, don’t know why I haven’t upgraded!
Awesome tips!
I’ve logging issues with iTerm2.
I usually ssh to a linux server and then telnet to my routers,
when I log the sessions,
All the up arrow strikes are logged as commands in cascade, all the delete key strikes are logged as spaces.
Any solutions?
TIA,
Yathish