Pages

Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

Tuesday, June 15, 2010

Ubuntu Tricks: Text File Manipulation

You've got a text file that is several million lines. Each line corresponds to a filename and each file needs to either be downloaded or uploaded somewhere.

Assuming you have a script that iterates and reads in serial from the input text file, what happens when the system kicks out an error? Start from the beginning? Thats what happened initially for me while I looked for solutions. Yeah, just letting it go this way exponentially meant waiting three(3) hours and more each time. Thats not very optimal.

Since at the point of error it was possible to see the lines of text corresponding to the filename already or currently being processed, these should be the resume point. Noted this down.

What do we need to do? Trim the file from the top using sed and pipe out to a secondary file.

How? 

sed knows about line numbers, I haven't dug enough to make it do a string search in parallel, yet. The best way so far that worked for me is to use grep. Such that: cat | grep -n
             Example: 
             $ cat files2009.txt | grep -n meatloaf.bin
                4236321:meatloaf.bin 

So, we need to cut until line 4,236,321 (or perhaps a few lines before that, your choice). In case you haven't been tracking your percentage done that also tells us that we've just got less than 800K files to go before we're done.

Time to use sed which understands regular expressions. For our puposes we'll be using: sed -e ',d'  
             Example: 
             $ sed -e '1,4236321d' files2009.txt > new2009.txt

The above means to start trimming at line "1" up to "4236321" from files2009.txt and pipe the rest of the original contents to new2009.txt

You can now imagine how to trim blocks of lines from the middle, just cat for the starting line of text and so on.

Easy enough, and there you have it.


Friday, April 23, 2010

Ubuntu Tricks: Use Static DNS with DHCP

This re-post from noob12 works perfect.

Edit /etc/dhcp3/dhclient.conf

Add or replace a line
prepend domain-name-servers your.name.server.ipaddr;

where your.name.server.ipaddr is replaced by your desired name server. You can list multiple servers in sequence by using a comma-separated list. Don't forget the semi-colon at the end of the line.

This tells dhclient to prepend these servers to whatever list it gets from the DHCP service when writing the /etc/resolv.conf. You'll see them (listed before the others) in the /etc/resolv.conf after DHCP completes. Normal resolution semantics then apply to this list, namely these servers will be used first; if they fail to respond, the client will proceed to following ones, but subsequent servers are not used if any returns an actual result, even if it is NXDOMAIN (i.e. not found).

You can entirely supersede the servers provided by the DHCP service using
supersede domain-name-servers your.name.server.ipaddr;

Ubuntu Tricks: Extract Attachments from Gmail

This is a repost from lovinglinux.

After a lot of research I finally figure out a simple way of doing this using IMAP and a shell script.

1. Install offlineimap and mpack
sudo apt-get install offlineimap mpack

2. Create a folder ~/mail for storing the Gmail messages

3. Create a text file and save it as ~/.offlineimaprc

This is the configuration file for offlineimap which will sync your Gmail with local maildir files, using IMAP. Add the following code to it:

[general]
accounts = GMail


ui = Noninteractive.Basic


[Account GMail]
localrepository = GMailLocalMaildirRepository
remoterepository = GMailServerRepository


[Repository GMailLocalMaildirRepository]
type = Maildir
localfolders = ~/mail/


[Repository GMailServerRepository]
type = IMAP
remotehost = imap.gmail.com
remoteuser = yourgmailaccount@gmail.com
remotepass = yourgmailpassword
ssl = yes

Then run the following command on a terminal:

offlineimap && munpack /home/yourusername/mail/LABEL/new/*

The offlineimap command will sync Gmail with local files and munpack will extract any new messages and attachments in the "LABEL" folder, where each folder (label) correspond to Gmail labels.

Sunday, November 08, 2009

Ubuntu Tricks: Discover Network Connections

There will be many reasons you may want to get a map of connection points in a network. In my case I'd setup several VM's to use DHCP and wasn't sure which to connect to and work on from off-site. Enter nmap, a security auditing tool that does exactly what we want as just a tiny part of its functions.

First you'll need to have it installed:
$ sudo apt-get install nmap

Next, issue a scan and use a filter to get the relevant info, for example:
$ sudo nmap -sP 192.168.1.0/24 grep up
Host 192.168.1.1 appears to be up.
Host 192.168.1.100 appears to be up.
Host main (192.168.1.102) appears to be up.
Host main3 (192.168.1.103) appears to be up.
Host 192.168.1.168 appears to be up.
Nmap done: 256 IP addresses (5 hosts up) scanned in 2.548 seconds

In the above example:
192.168.1.1 is a router
192.168.1.168 is another router*
192.168.1.102 is a WinXP machine
192.168.1.103 is this machine

[*] Machines on the other end of .168 aren't shown as they are on a different subnet and we only scanned the 192.168.1.0/24 in this example. Explaining how to scan beyond the initial subnet would be beyond the scope of this tip. Use your favorite search engine for this. Always be extra careful to put a scope when you do a scan as this could be flagged as a precursor to a network attack by many of today's firewalls.

You can actually get a list of details (including MAC addresses, etc) using the '-vv' option. This would be perfect in locking down a network:
$ sudo nmap -vv -sP 192.168.1.0/24

If you're like me and would prefer a nice working GUI then look no further than zenmap. Similarly, download and install via:
$ sudo apt-get install zenmap

Have fun and safe discoveries!

Friday, October 09, 2009

Ubuntu Tricks: Multi-boot and Grub Errors

I've been doing alot of Linux installations lately while figuring out how to get LinuxMCE and MythTV (Mythbuntu) installed. This is on top of pre-installed versions of Windows XP and Windows 7 RC.
One time saving tip is to know how to help Linux figure out what happened to the boot records. This is all done via a Live-CD installation and an open terminal window:

sudo grub
find /boot/grub/stage1

This will tell you what partition Ubuntu is, so assuming it said: (hd2,0)

root (hd2,0)
setup (hd2)

The system will then tell you it found the partition and is reconfiguring everything.

quit

Thats it. Time to reboot.

sudo reboot now

If you're still getting errors, then you'll need more than this small bag of tricks to recover. Good luck!


Update: The above instructions do not work for Grub 2. 

Thursday, September 17, 2009

Phoronix Test Suite on Ubuntu Jaunty 9.04 (draft in progress)

A bunch of Dell desktops landed in my possession about last week. That is now a total of four-(4) machines that I can pick and choose from for my ultimate gaming and workhorse :-)

Machines:
Dell Dimension XPS Gen4
Dell Dimension 9200 (2)
Dell Precision 470

Now these machines are about four-(4) years old and I have absolutely no plans on upgrading them except for possibly RAM and hard disk space. My main interest is which of them houses a better graphics card; secondary would be the

Graphics Cards:
ATI Radeon
nVidia 7900 GS
...

Processors:
Intel Xeon
Intel Core 2 Duo
...

My initial tests using Windows were inconclusive, and most of the tests I saw required a purchase. I don't plan to test for the rest of my life or monetize this, so a free version is preferred. This is were I stumbled upon PTS.

After burning the Desktop Live version which was noted to be a pre-release, the final decision was since Ubuntu Jaunty desktop was already installed then just install PTS from the repositories. Boy was I wrong, it is now taking a little more than just a few clicks and root password! The sizes of the test packages are enormous particularly since the test we are after is phoronix-test-suite install gaming. Detailed below is my progress to get this going.

Package
The repository package for PTS is outdated at version 1.0. Thats not what we need so need to install from Phoronix-Test-Suite.com downloads particularly the Ubuntu .deb package.

Dependencies:
Make sure that the system is prepped to have all the compiling environments just in case: sudo apt-get install build-essential

We'll need the basic PHP packages and extensions that will allow us to save the results in the future as .PDF or .PNG
sudo apt-get install php5-cli php5-gd php-fpdf php5-ming

The Demo Live DVD has all the GUI front-ends needed, it doesn't seem to be the case with the downloadable v2.0 update. We'll need to install PHP-GTK, There is an unofficial .DEB installation and that also should work according to some reports. You can also try: sudo apt-get install php5-gtk2

If all else fails you can install PHP-GTK by hand. NOTE: It may be needed to run Step #5 and edit php.ini to get the everything working after all.

In case that doesn't work there's also a good install flow here, just make sure to also visit the official download to fix the login issues when using CVS.

OK, so far this is where I am at and will update as things progress.

One issue that seemed to pop out was that due to the huge amount of downloads of the same stuff for several systems it may make sense to consider having a cacheing proxy installed in your network path. This led me down an alternate idea of speeding up and securing my network with Squid Proxy.

So far the downloads have taken 24-hours using AT&T UVerse speeds, 256-bit AES PLC, and an old 3Com hub.

Monday, June 15, 2009

Ubuntu Tricks: Copy Directory Structure

Looking to create a download staging directory that mimics the actual target after a 2nd phase move, I was at a loss of how to easily do this. Google to the rescue led me to this page. Its not a super elegant solution but it works after a bit of cleanup and is better than having to type it all myself:

# cd /new/dir
# (cd /old/dir; find -type d ! -name .) | xargs mkdir


Nice.

Sunday, June 14, 2009

Ubuntu Tricks: Lock Screen Button

Saw this button some time ago in one of our internal systems. At first I thought it was an application that needed to be installed but after some searching and hindsight it didn't make any sense to actually need one more running program just to call the lock screen feature.

Fortunately, Ubuntu has this to say:

You can add a Lock Screen button to a panel for easy access. Right-click a panel (for example, the panel at the top of the screen), select Add to Panel... and drag the Lock Screen item to the desired location.

Works on my Jaunty, too.

Saturday, April 25, 2009

Ubuntu Tricks: Compile, Run or be Executed

There will be instances that you may need to protect your shell script contents from being read, such as for instance if they contained log-in or other account details and you wanted to share the script for automation. I was able to find a simple tool that could do this, shc.

Its not part of the usual distribution, so you'll need to have this installed via the package manager or via the terminal using apt-get:
sudo apt-get install shc

After which, simple call the function using this format: shc -f
Example: shc -f mydownloads.sh

What shc does is translate your script into C, then compile it and make it executable. If we ran the example this will result in two-(2) additional files: source and executable
Example:
-rw-r--r-- 1 me me 354 2009-04-25 13:11 mydownloads.sh
-rwx--x--x 1 me me 10948 2009-04-25 13:11 mydownloads.sh.x
-rw-r--r-- 1 me me 11299 2009-04-25 13:11 mydownloads.sh.x.c

The file mydownloads.sh.x is automatically set as executable. Make sure to test it and compare the results with the original script. In my tests you can even ranema the file to something else more descriptive.

But what if in your changing and sharing the executable bit gets unset? Well, that is simple. Just run chmod.

sudo chmod +x

In the example above you'll see the bit permission change from
-rw------- to -rwx--x--x.


Friday, January 23, 2009

Ubuntu Tricks: No Special Characters Here

The limited capabilities of FAT16 and FAT32 has to do with file names. Not so in Linux where almost any character is accepted and files are treated executable by their attributes and not file extension. You could say that Linux runs by true file type.

The rename command in combination with find is one of the best ways to go through all files in a directory and fix naming problems before moving files onto a Windows File Share.

Best line so far:
find . -iname "*" -exec rename -v 's/\:|\*|\%|\&|\?|\\|\^|\=|\"//g' "{}" \;

winmail.dat makes you Fentun

The unfortunate thing about another operating system platform is compatibility. In particular, emails at work (or play). Another thing is the use of standards.

If you get an attachment thats called winmail.dat, thats not the actual attachment but a wrapped up version. It probably is a Microsoft Word Document or some other.

A tool called Fentun is available that can help unwrap this shell and help extract the contents. Instructions are here.

Wednesday, January 21, 2009

Ubuntu Tricks: Disk Space Usage and Mount

Disk Usage and Size
Its simple, and I got it even working via ssh onto one of my work servers to check how much space was being used by a mounted USB drive. Just type the shell command: df

Of course this is going to give you the default output in bytes. This is more human readable using the switch -h provides the output in megabytes, while uppercase as -H shows the output in gigabytes.

Example #1: $ df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/foo--bar-root
3.2T 2.5T 604G 81% /
tmpfs 1012M 0 1012M 0% /lib/init/rw
varrun 1012M 408K 1011M 1% /var/run
varlock 1012M 0 1012M 0% /var/lock
udev 1012M 2.9M 1009M 1% /dev
tmpfs 1012M 0 1012M 0% /dev/shm
lrm 1012M 2.0M 1010M 1% /lib/modules/2.6.27-9-generic/volatile
/dev/sda1 236M 52M 172M 24% /boot
//192.168.0.1/usbdrive
2.7T 2.3T 430G 85% /home/foo/storage


Example #2: $df -H
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/foo--bar-root
3.5T 2.7T 648G 81% /
tmpfs 1.1G 0 1.1G 0% /lib/init/rw
varrun 1.1G 418k 1.1G 1% /var/run
varlock 1.1G 0 1.1G 0% /var/lock
udev 1.1G 3.0M 1.1G 1% /dev
tmpfs 1.1G 0 1.1G 0% /dev/shm
lrm 1.1G 2.1M 1.1G 1% /lib/modules/2.6.27-9-generic/volatile
/dev/sda1 247M 55M 181M 24% /boot
//192.168.0.1/usbdrive
3.0T 2.6T 461G 85% /home/foo/storage


Subdirectory Size
What if you just wanted to see the size of a particular directory and subdirectories underneath it in the tree? Simply use the the command: du

Naturally, similar to df its going to default to bytes so just use the -h switch and you'll be fine.


Mount Shares
There are also several ways to mount a network share. Whats important is that the share needs to be configured for the permissions you need whether read/write/both/etc. Make sure that you've installed smbfs as using smbclient is rather the old way to do this:

mount -t smbfs //ip_address/path1/path2/... -o username=yourusername,password=yourpassword

Example: mount -t smbfs //192.168.0.2/foo/bar -o username=foo,password=bar

If you're trying to mount an external USB drive, then there's no better way than as explained here.

Of course, what comes on should be easy to come off. Enter umount:

umount -lfr /mount_point/samba_share

Example: umount -lfr /dev/sda1

The above syntax switches lets you remove the drive or whatever share no matter if its "busy" or if a network share that's "requiring administrative privilege" aka offline.

Have fun!

Wednesday, November 12, 2008

Ubuntu Tricks: Empty Directories

Have you ever used a tool that would go through subdirectories and delete the empty ones? One Windows I was able to do this with Heatsoft Clone Cleaner Pro (trial). But really, there has to be an easier way to do this without adding more software glut.

For those who like to manage their files, I do as part of work, here's a tip. If you can mount/map your main subdirectory onto Linux, all you have to do is issue this shell command:

find /path/you/specify -type d -empty -delete

It doesn't get any simpler than that. I did find however that in some instances when mixing symbolic links that it won't take. So you can try to change the target to a deeper directory level and issue the command from there. You can also go to the directory itself whereupon the simple format is to do: find ./ -type d -empty -delete

Simple and done. Happy!

Friday, November 07, 2008

Kubuntu Ibex: How to make it VMWare Aware

It happens, you upgrade to a new version number and suddenly the painstakingly learned knowledge gets dumped out with the trash. Before upgrading to 8.10, my 8.04 was happily chugging along with an old port of VMWare Server. Note that there are issues with VMWare on Ubuntu and its relied on a patched script to fix compile errors and such.

After upgrading I thought it would make sense that somehow old programs would get rebuilt, recompiled, or be backward compatible. Take anyone action, like most my expectation is that it runs and works. After all, this is supposed to be smarter operating system with thousands of eyeballs on the code. Right?

Wrong.

Thanks to Google search, I stumbled upon these instructions. Hey, here's one other reason not to be hasty to upgrade. After all, Intrepid has been out there for a little while and some brave soul more code compile savvy than I am would surely have already risked and ventured. :-)

Thursday, October 23, 2008

KUbuntu: LVM Horror and Suspense

... I get by with a little help from my friends ... - The Beattles

So the song says and pretty apt for a general Linux newbie like myself. I've got 4 book references and the whole Internet library but it really comes down to situational experience that matches what you basically want to do to get it done in the most cost-effective (and I'm talking about avoiding loss of data) time frame.

Apart from the new dual WAN router and the upgrades for the WHS there was the bit about two-(2) Seagate Barracuda 1.5 TB drives that needed to get added onto the /root directory of my Kubuntu installation.

Now the idea of logical and physical mapping gets quite fuzzy when dealing with disk drives in Linux. Probably as fuzzy as making sure you don't have packet storms or route loops while configuring your router -- I'll get to that separately. From a MS Windows world I've already been initiated to Dynamic Disks as far back 1999~2002 when larger drive sizes started to come out, Disk Expansion on the WHS (Server 2003 based) is also interesting.

Though lacking the pictures, I was basically saved by and old guide. But before jumping into using the terminal commands the Windows User in me certainly went straight out looking for a GUI implementation ... that got me to porting system-config-lvm from Fedora. Yes EVMS is evil, and by that I'm saying it was weeks before I up and gave up on it some time ago while trying to configure LinuxMCE and Mythbuntu.

One key thing to note is that system-config-lvm gui actually lets you do everything easily! However some work does need to be done in terms of pre-formatting the disk with fdisk and setting the disk type to LVM Linux (type: 8e). After that, assuming like me you just want to get more general disk space and haven't configured fancy parititions , extending the /root partition will required a live CD distro that has LVM packages pre-built. For me this was an earlier copy of KnoppMyth versus Knoppix in the guide. Then its simply going to be a disk check using e2fsck and then a call to resize2fs and get all that disk space registered for actual use (if you don't run it I almost guarantee you'll be running into some horrific data loss issues, so just do it so everything gets tagged and registered where they need to be on the system).

My thanks to Andy P. and Morton S. who were kind enough to offer assistance -- I'm going to get back to you guys on other stuff (such as making sense of PGP vs GnuPG) soon don't you worry :-)

But for now, time to decompress archives and hunt for a couple of mobile pests for some tests ...

Wednesday, June 25, 2008

Kubuntu: the Alternative CD

I had Unbuntu Hard Heron version installed on my test machine previously and just barely got it to doing RAID 5. Several books later and a run-in with LinuxMCE and MythTV had me reconsidering to move to the KDE gui though.

So I've just burned Kubuntu 8.-4 Hardy Heron using the alternative CD as it seems to be the only way to get any Linux distribution going right out on install and not as a post-install chore. In preparation I've got the the instructions all laid out for me. Wish me luck.

I figured a perfect way to recycle the PATA (IDE) drives from the older FreeNAS by putting it in a better looking case.... Enter the AMS T4U (DS-2340UBK) that I bought off half-price at eBay. This is a 4-bay unit with built-in JBOD capability which I've re-purposed into a RAID-5 via the independent drive setting. Combined with the Dell XPS its going to be extra storage hopefully for a bring-anywhere system. My original plans were to finally install Mythbuntu on it, but that project's taken a short break for now.

Tuesday, June 26, 2007

Windows Vista, Worth My Time?

I recently purchased Windows Vista Ultimate for a planned installation. At this time, there are three-(3) machines that are physically available for this install -- none of these I own, but they I personally use for work primarily.

Knowledge of the ins and outs of Windows has been, for me, more of an on-the-job-training (OJT). At the moment I do not hold any MCP or MCSE. Not saying I have not tried to do so, but like so many things that could ultimately lead to self and professional advance ... work gets in the way. I digress. Let me say that since 1998 I have at least re-installed Windows (from '95 to XP) at least twice a year.

Why the reinstall? Thats easy enough. So many software companies, so many lacking features, so many new things to try. At that point years ago the use of VMware or Virtual PC was yet unheard of or costly. My initial foray into emulation was with BOCHS, by the way. :-)

Thankfully the company I work for is a Gold Partner. That said, as a researcher/analyst operating under limited resources it will happen sometimes that your only test machine is on production -- so be it. And if it crashes, thank goodness for the Volume License.

After almost a decade, here we are. I've purchased my own copy for my own use. Still procrastinating on installing it finally as I may want to test the performance on either of the machines I have -- Intel Mobile, Dual Xeon, or Core 2 Duo. Thats when I saw this problem.

Its unbelievable and it actually has pushed my conviction to make space for the learning curve of using Ubuntu. I also swear my next laptop is going to be a Macbook Pro with at least 4GB of RAM -- regardless of the OS. And shall I say, learning *BSD is not such a bad idea, anyhow.

Returning from a conference at Redmond last week, I'm still thinking. This new link shows promise. Perhaps I may install Windows Vista Ultimate after all. Why not?

But mind you, if at some point it doesn't allow me to reinstall when I really need to (ie. upgrade for mobo, chip, other hardware) then its "bye, bye, Blue Screens" and "hello Orange startup window!"