Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts

Thursday, February 4, 2010

Alpine, Gmail, and SSH. How to make your computer send emails when you're not there!

Out of the many things I've learned in the past half year about using Linux, I find the concept of remote access of my computer through SSH to be the coolest.  Periodically I will be away from home, and I'll realize that I need access to some file on my computer so that I can send it to someone.  I recently learned how to set up a Secure Shell Server on my Ubuntu Linux laptop that I can access from my Android cell phone using an Android app called ConnectBot.  Once I get into my computer, I can then open up a fantastic text-mode email program called Alpine and compose emails with files attached right from my computer's hard drive and then send them through my Gmail account.  Nothing makes me feel more like a huge nerd than this!

Here's how I set up an SSH server and then install Alpine with access to my Gmail account (my Gmail is setup to check and respond from 2 additional email addresses, so the instructions are tailored to that scenario):

Setting up openSSH

First you need to install the openSSH server.  Easily enough you just have to type in sudo apt-get install openssh-server into your terminal to get the server.  Amazingly, it's ready right away to be used!   I wanted the SSH server to use a different port than the default (port 22), so I opened up /etc/ssh/sshd_config and changed the port number on the 5th line to something that was wacky enough not to be predictable.

Next came two considerations: (1) I'm behind a router, and therefore needed to set-up port forwarding so that I'd be able to access my openSSH server remotely.  If you have a router and forget this step then prepare for lots of frustration!  (2) Even having port-forwarding set-up, who wants to remember and type in an IP address so that you can connect to your server?  So I needed to sign up for a free dynamic hosting service that would give me a constant host name regardless of my IP address.

Setting up Dynamic DNS hosting

I found a great tutorial on the Ubuntu Help Site for setting up Dynamic DNS hosting for your computer.  Basically, I went to DynDNS, set-up an account for my computer, and then followed the instructions on the above-mentioned Ubuntu Help Site to install and configure ddclient.  ddclient will periodically send updates to DynDNS so that the hostname they provide you with always points to your IP address.  When you're done configuring all of this, you will then have a host-name pointing to your computer such as tommy.gotdns.com.  That's a lot easier to remember than 40.831.391.33 right?

Initial installation and set-up of Alpine

Next I set-up Alpine.  Installing it was easy enough: sudo apt-get install alpineOnce it's installed, you have to set it up to work with your Gmail account.  Refer to this tutorial from the University of Virginia for a tutorial on how to do that.  Be sure that when you're in the Config screen of Alpine, look for the field named Inbox Path and enter in the following: imap.gmail.com/ssl/user=username@gmail.com .  Be sure to replace 'username' with your own Gmail account username.  This will make sure that when you start-up Alpine and go into your Message Index, you will see the emails in your Gmail Inbox right away, instead of having to navigate to that inbox.

Configuring Alpine to send emails using different 'From' email addresses

Now, when I reply to emails that I've picked up using my Gmail account, I usually want to reply using different email accounts.  When you're only using the web-based Gmail checker, it allows you to respond using the account that the email was downloaded from.  I wanted to have that same functionality in Alpine.  This is where the Alpine concept of a Role comes in handy.  From the main screen of alpine, type the following keys (don't type the triangular brackets): S > R > R > A .  This will bring you to a screen that allows you to set-up the conditions under which Alpine will let you send an email using a different From email address. 

First, set a nickname up for your role; maybe the name of your email provider (i.e. Sympatico).    Next, under Current Message Conditions and beside To Pattern put one of the (or the only) email address(es) that Gmail is checking for you (e.g. bill@sympatico.ca).  This creates an expectation that whenever this email address is in the the To field of an email, Alpine will do whatever you tell it in your role.

Then, under Actions Begin Here and beside Set From put your name and then email address that Gmail is checking for you (e.g. Bill Nye <bill@sympatico.ca>).

Finally, under Uses Begin Here you need to make the following changes (if necessary): (1) Under Reply Use = make sure to highlight and press enter on Without Confirmation and (2) Under Compose Use = make sure to highlight and press enter on With Confirmation.  This will make it so that if someone emails you at bill@sympatico.ca, when you reply to that email, Alpine will automatically put bill@sypmpatico.ca in the From email address and not bill@gmail.com.

When you're done, you can press E to exit the Role set-up.  If you check two email accounts through Gmail like me, press A once more when you're in the Setup Role Rules screen in alpine and repeat the above instructions to set-up a role for your other email account.

Saving your Gmail password in Alpine so that it doesn't prompt you all the time

Finally, you'll probably be annoyed with entering in your Gmail password every time you start up Alpine.  Thanks to a helpful user on Ubuntu Forums, I found out a simple way of making Alpine save your password.  Open up your terminal, change directories to your home directory, cd ~/, and then type in touch .pine-passfile.  After that, run Alpine.  It will ask you for your password on start-up, and on sending an email, only to save your password indefinitely so that you no longer have to type in your password all the time.
 


Conclusion


Wow, those were a lot of very little steps which, put together, seem mountainous.  However, now I can punch in my dynamic DNS hostname into ConnectBot on my Android cell phone, access Alpine and send emails with files attached right from my computer's hard drive!  I also like that I can update my databases on the fly with important personal info, manage, and even turn my computer off from afar.  It's quite neat and if you're into feeling nerdy, I highly recommend getting your own openssh server configuration.

Monday, December 21, 2009

Finding Files in Google Desktop and bash

    Sometimes I want to find a file and I have no clue where it is.  When I was using Windows Vista, this was a simple matter of using the native file searching utility, which dutifully searched for my lost file using the index it created of my hard drive.  File indexing is an amazing thing to take advantage of.  When your computer searches a file index, it gets results FAST! 

    If you don't have a file index (remember, it's literally a simple listing of every single file you've put on your computer in one relatively small database file) then your computer will have to loop through every single file on your computer, trying to match your search argument (e.g. 'Resume*.pdf') to the file names that it processes.

Today I'll talk about three linux programs for indexing and/or finding your files:
  • Google Desktop (for easy indexing and finding using a GUI)
  • locate (for finding indexed files in bash/terminal)
  • find (for finding files in bash/terminal)



Google Desktop

Even if you are using windows, click on the link above and get this program.  It's VERY convenient to use!  Google Desktop will not only allow you to search for your files, but emails that you've sent and received as well!  Once you download it, you will want to make sure that it builds an index of the directories in which your important files will be stored.  As shown below, right click on the Google Desktop icon and click on Preferences.  On my computer I have it indexing my home user directory and my USB drive root directory.  You can of course add any extra folders you like by click on Add folder to search.


Preferences are opened in your default browser

You may want to modify settings in the other tabs shown above.  Go to the Gmail tab and follow the instructions there if you would like Google Desktop to index your gmail account.  Also, go to Display and click on the Change Hotkey button lower down on the page in order to set up a key combination that will bring up a quick search box (shown below).


My key combo:    Shift+Control+?


When you're sure that everything is set, click on Save Preferences and Google Desktop will start indexing your files!  Bear in mind that the process is slow, so you'll have to give it several hours until it's done indexing everything.  You can check on Google Desktop's progress in indexing by right-clicking on the Google Desktop icon, then on Index, then on Index Status.  You'll then see its progress in indexing all of your files and emails.

What I really like about Google Desktop is the simplicity in finding and opening anything on your computer.  Type your key combo, quick search box comes up, type in your search argument, click on the result matching the file you were looking for, then your file opens!



locate

As I understand it, Ubuntu 9.10 (and i'm sure basically every other distro of Linux) indexes all my files for me on some periodic basis.  This is what enables the locate program to work in the terminal.  locate searches through the generated file index database for a term of your choosing.  So let's say I want to search for the manual I recently downloaded for my Hammer Drill, but I have no clue where it went!  Here's how I would use the command to find my file:

inkhorn@inkhorn-laptop:/$ locate -i hammer

Here's what the arguments I used mean:

  • -i tells the locate program to ignore the case of your search term
  • hammer is simply the search term

Here is the output from the locate command:

/home/inkhorn/Documents/Hammer Drill Manual.pdf
/usr/share/pixmaps/pidgin/emotes/default/hammer.png
/usr/src/linux-headers-2.6.31-15/arch/avr32/boards/hammerhead
/usr/src/linux-headers-2.6.31-15/arch/avr32/boards/hammerhead/Kconfig
/usr/src/linux-headers-2.6.31-15/arch/avr32/boards/hammerhead/Makefile
/usr/src/linux-headers-2.6.31-16/arch/avr32/boards/hammerhead
/usr/src/linux-headers-2.6.31-16/arch/avr32/boards/hammerhead/Kconfig
/usr/src/linux-headers-2.6.31-16/arch/avr32/boards/hammerhead/Makefile

The first result is the one I'm looking for!  Now I know where to look to open my hammer drill manual.



find

The find command has to loop through each and every file in the directory you specificy in your search argument.  If you told it to search the root directory '/' for a file with the word 'Hammer' in it, your computer would probably take an intolerably long time finding it.  Happily enough, you don't need to do that and probably have an idea generally where it is.  To use my hammer drill example:

inkhorn@inkhorn-laptop:/$ find /home/inkhorn -iname 'Hammer*.*'

Here's what the arguments I used mean:
  • -iname tells find to ignore case when comparing your search argument string to the file names that it loops through
  • 'Hammer*.*' tells it to look for a file of any extension (.*) starting with the word 'Hammer' and continuing with any other words after that (*)

Here is the output from the find command:

/home/inkhorn/Documents/Hammer Drill Manual.pdf

Cool thing is that the find command only outputs file results while the locate command outputs directory results!  In this instance, the results came on screen really quickly.

Now for something even cooler:  I can enter in a command that looks for a file that I'm interested in and opens it in a program that reads it!  Here is what I would enter in if I wanted to look for the hammer drill menu in terminal and automatically open it up in an excellent PDF reader named Okular:

inkhorn@inkhorn-laptop:/$ find /home/inkhorn -iname 'Hammer*.*' -type f -exec okular '{}' \;

You've already seen the first part of the find command in action.  The second part tells the find command to execute a program called 'okular' that should then take the find results as an argument.  In other words, okular opens up the file listed in the find output.

You can use this to open up other file types that you want to find.  If you're looking to open a word document, replace 'okular' with 'oowriter', a spreadsheet, replace 'okular' with 'oocalc'.

For more info on using find and locate, click here.