Sign In

    Enjoy FOSSwire's content? Have it delivered! Subscribe

    Count Lines of Code with Cloc

    While it might seem a little of a mundane task, counting the lines of code in your programming projects can be a useful thing to do, and provides you with interesting statistics.

    Cloc is a Perl script written to allow you to do just that. Download the script, and then from a command line, you simply pass it a directory of source code, or a single file if you wish.

    $ perl cloc.pl /path/to/code

    Cloc is more than just a tool for counting the number of lines in the file, it is more clever than that. Cloc detects the programming language if it can, and then shows you the number of blank lines, code and comments of each file.

    With a single file, Cloc might give output similar to this:

    Cloc screenshot 1

    As I've mentioned, however, Cloc's real strong point is when you point it at a directory or several directories of source code.

    Cloc screenshot 2

    Quite frankly, I think this is a pretty indispensable tool for any programmer interested in getting statistics on their code, especially when you want an overview of the whole project.

    Cloc can be downloaded from here. The Perl script version should work on any operating system where you have a working installation of Perl. Windows users can download the pre-built exe as well.


    Using SSH as an Ad-Hoc VPN

    Laptop - source http://www.sxc.hu/photo/888008

    Many of us have been in this scenario - you're on the move, using a random WiFi connection that you can get. You want to browse around to all your favourite sites, including ones where you log in over normal HTTP, but you're not entirely convinced of the security of the connection, so you don't.

    If you have access to pretty much any server running SSH where you can log in, you actually can set up a secure tunnel to route all of your data through using nothing more than what you already have. Kind of like a very simple VPN, that you can do on the fly.

    This assumes your client/laptop is running Linux, Mac OS X or another Unix-like OS where ssh is installed and on the command line. We'll also be configuring Firefox as the browser to route traffic through the tunnel. Windows users can use PuTTY to achieve the same effect.

    Set Up the Tunnel

    From your terminal, log in to your SSH server with the normal command, but add -D and a port number, to set up your tunnel on that local port. Something like this:

    $ ssh -D 1080 user@host

    You'll be logged in as normal, and the prompt should come up. What has also happened, however, is that your SSH client is now listening on local port 1080. Anything you tunnel through there will go securely to the SSH server, and responses sent back through that tunnel.

    Before we move on to configuring Firefox to route through this, here are a few more options for that command. If you don't want a prompt to come up, i.e. you just want to set up the tunnel and don't need to actually interactively log in to the server, add the -N switch, like so:

    $ ssh -ND 1080 user@host

    Also, you can use gzip compression to speed up the transfer. Combined with no login, that is:

    $ ssh -CND 1080 user@host

    Configuring Firefox

    Our tunnel is now up and running, and should be accepting traffic. To configure Firefox to use this tunnel, first go to Edit > Preferences and choose the Advanced section.

    Firefox Proxy configuration screenshot

    Under Network, click the Settings button. Choose a Manual proxy configuration.

    Under SOCKS Host, put localhost and port 1080. Leave it on SOCKS v5.

    Firefox Connection Settings screenshot

    Say OK to that, close Preferences and now use something like Check IP to confirm that your IP address now appears to the outside world as your SSH server's IP address. This tells you that your traffic is being tunnelled and you can now do anything you wish without fear of snooping (provided you trust your SSH server and its connection, of course).

    Finishing Up

    When you're back home or you've closed the SSH session, make sure to go back to that dialogue in Firefox and choose No proxy (or whatever it was set to before). Otherwise, you won't be loading any pages any time soon.

    Speaking of which, when you're done, go back to that SSH session in your terminal window and hit Ctrl+C to drop the connection and close your tunnel down.

    This is a really simple way to securely browse on untrusted connections and the fact that it doesn't require any special setup on the SSH server makes it particularly easy when you just happen to find a moment when you need to use it.

    Quick, easy and gets the job done. You can't ask for much more than that.

    [image source]


    Getting Started with GNU Privacy Guard

    Padlock

    Keeping information safe and away from eyes that shouldn't see it can be pretty important. Here in the UK we've had several instances where the government has lost personal information. Since that information wasn't encrypted, it could be in anyone's hands by now.

    One of the most popular ways to encrypt information is using the free GNU Privacy Guard software. In this short tutorial, I'll show you how to get up and running to use GPG to encrypt and decrypt some information. For now, I'm only going to cover encrypting information that only you want to get access to later, but I may follow up with how to transfer information securely to others in a future tutorial.

    In this tutorial, I'll be trying to keep things as cross-platform as possible, so this guide should be easy to follow on Windows, Linux/Unix and Mac OS X. At FOSSwire we want to try and reach out and help users of non-Linux/BSD platforms to use free software too. Let us know how you think this tutorial works to achieve that and any thoughts on this issue in the comments of this post, or you can email me - peter@ this domain.

    This will all be done on the command line, regardless of your operating system, but I'll show you more on how to get into that in a moment, once we are installed and ready.

    Installation

    First things first, install GNU Privacy Guard.

    Linux/Unix

    Most Linux users should already have this installed, if not then use Add/Remove Applications (or similar) and search for gpg or gnupg.

    Each time you need to use GPG, open a Terminal program (it might also be called Konsole).

    Mac OS X

    Pre-built GPG packages are available from the Mac GNU Privacy Guard site. Download and install a package appropriate to your setup.

    Each time you want to use GPG, you'll need to open Terminal, from /Applications/Utilities. Type the commands in this tutorial into Terminal to use the program.

    Windows

    A Windows version is available on the official download page. It is slightly difficult to spot, so here's a direct link to the current version at the time of writing.

    Once you're installed, you'll need to open Command Prompt, from Accessories. Then type:

    cd "C:\Program files\GNU\GnuPG"

    That will allow you to follow the commands in this tutorial. Replace that path if you install GPG somewhere else, and remember that this will need to be typed in each time you load up Command Prompt afresh to use the program.

    Setting up GPG

    Once you have the software installed and your command line terminal open, let's get started.

    The first thing you have to do in order to use the application is to create a set of keys. These keys are used to encrypt and decrypt information and they keep your data safe. Your keys are also protected by a passphrase that you must type in to use them (so that even if you lose your key and someone else gets a hold of it, it won't be any use without the passphrase as well).

    gpg --gen-key

    You'll see a load of weird messages, but you can just ignore most of them. The important question here is the kind of key you want. For now, we want to stick with choice 1.

    Just type 1 and press Enter.

    The next question you get asks you about key length. Just accept the default and press Enter here.

    GPG\'s GenKey

    Next up, expiry. GPG keys are designed so that it is possible after a period of time to force your key to expire, for security reasons. For this tutorial, I'm not planning to distribute this key, so I'm going to choose 0, for key does not expire.

    The penultimate set of questions ask you for some information to identify this key - your name and an optional comment. Once you've entered and are happy with that info, type O and press Enter to move on.

    GPG GenKey Information stage

    Finally, type a passphrase. This should be something secure and that isn't used elsewhere. The entire security of your encrypted data basically depends upon this being unguessable.

    Note the emphasis on passphrase, this needn't and shouldn't be just a single word that is in the dictionary.

    When typing in your passphrase, you won't be shown the typing in the terminal. This can be confusing if you haven't experienced this behaviour before. Don't worry - it is typing in properly, it just isn't letting you see it.

    Once you've set the passphrase, GNU Privacy Guard goes off and creates your key. This will take some time, depending on how fast your machine is.

    GPG Generating a Key

    Once it's finished, you'll be sent back to the prompt. Now you have a key, let's encrypt and decrypt something!

    Encrypting a File

    So, how do we go about encrypting a file, so we can securely store it and decrypt it ourselves later?

    Assuming you've still got that terminal window open from before, here's the command you need:

    gpg --encrypt --recipient 'Name' "file"

    Name - needs to be set to the name you gave in your key. This will allow you to decrypt it again later.

    file - the path to the file you want to encrypt.

    Once you've hit Enter to run that, your file will be encrypted. In the same folder as the original, there will be an encrypted copy with a .gpg extension.

    Decrypting a File

    OK, so now you need to get that same file back and read it. You securely erased the original, so you need to decrypt that version to get access to the data again.

    The command you need is:

    gpg --output "outputfile" --decrypt "file.gpg"

    outputfile - the path to the file you want to put the decrypted contents in.

    file - the path to the original, encrypted file you want to decrypt.

    When you decrypt a file, you'll be asked to enter that passphrase. Type it in, and outputfile should now be the original file, just as it was.

    Wrap Up

    We have literally just scratched the surface here. GNU Privacy Guard is an incredibly powerful solution for all sorts of encryption and data security problems and this is just a limited scenario and light introduction into it.

    This tutorial showed you how to use the command line program, however many of you may be more familiar and happier using a graphical program. There is a list of graphical frontends which does show you a few of these programs which might make using GPG a lot easier.

    However, having an understanding on how to use the command line interface never hurts, and allows you to transfer those skills over to any platform.

    Remember! Let us know how you think this tutorial works in branching FOSSwire out to support users of FOSS on other, non-Linux platforms.

    Also, feel free to share any thoughts on the issue of cross-platform support. You can leave a comment below.

    [image source]


    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. ...
    7. Go to