Dec 30, 2013

CentOS Install SSH server and configure for VirtualBox testing

I have had some tasks to configure and install a CentOS distribution for my bug-tracking needs. Hope to help out someone with similar problems and or tasks.

I'll be using a fresh install of a CentOS Minimal 6.5.  And a Mac Book Pro with VirtualBox installed (as my testing environment).

First after install of a clean CentOS you need an SSH server to work in your usual environment. So to connect to a Linux, installed in a VirtualBox you will need basically 2 things:
  • openssh-server install
  • disable firewall
  • VirtualBox Port configuration/Access configuration

OpenSSH server install

To install server you need to run in your VirtualBox Guest system (CentOS VM):
yum -y install openssh-server
And start and add it to auto launch at system startup:
chkconfig sshd on
service sshd start
Also make sure port 22 is opened in your VM. type:
netstat -tulpn | grep :22

Firewall settings

Because it's a VM you can simply disable the firewall. But be sure not to do this at deployment/production server of any kind.

To disable firewall and remove from auto lunch at system startup run:
service iptables stop
chkconfig iptables off

VirtualBOX

To connect your Guest machine to your Host machine (basically up a network between virtual CentOS and your host Mac OS (In my case) you have several options.
I'll stop on a simple one. You may do a Bridged network connection and have your router/whatever assign your VM an IP address inside your network.
You may also select NAT and configure port mappings. But it's fairly more complex rather then this setting and exceeds the scope of this article.

Connect

Now you need to reboot your CentOS or either reboot network inside your VM. As a result, after reboot you should get assigned IP address. You can view it using command:
$ ifconfig
eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX  
          inet addr:192.168.1.104  Bcast:192.168.1.255  Mask:255.255.255.0
Mine got an IP ending with 104.
And so now you can ssh connect to your VM terminal using something like:
$ ssh root@192.168.1.104
root@192.168.1.104's password: 
Last login: ...
[root@localhost ~]# 
Tadaa! Your system is up and ready for experiments. Just be sure to save your state with VM snapshots ;)

Comments?

Dec 13, 2013

Python Generators explained simply.

I'm often confused by recent obsession of generators in python. I'll try to explain them as simply as possible. Say you have never used iterators but coded in python. BUT I'm sure you have used dictionaries (dict) and have met requirement to iterate through it's keys and/or values. TADA! You have used generators already ;).

So generator is a function in python. Except for it uses a keyword yield in it's code. Thus making it the iterator. So you could call this function in a sequence like you would probably do iterating over a dictionary already. E.g.:
# typical iteration through dictionary key: value set
for key, value in dict.iteritems():
   # do someth

# Usage of your own iterator
for item in iterator_functuon():
   # do something with function generated output
So this function returns an iterator generator like the dict() type has by default. And has a next() function, like usual iterators have.
So in attempt to using human language:
Generators are simple functions converted to iterators.
So when a generator function calls it's magic word yield, the "state" of the generator function is frozen; the values of all variables are saved and the next line of code to be executed is recorded until next() is called again. Once it is, the generator function simply resumes where it left off. If next() is never called again, the state recorded during the yield call is (eventually) discarded.
So. that was the theory. Better to look at example well commented out:
def gen_odd_nums():
 # executed once (first call) usually like __init__() of a generator
    odd_num = 1
    while True:
     # saves context and return from function
        yield odd_num
        odd_num = odd_num + 2

# Typical generator usage
generator_object = gen_odd_nums()  # initialising a generator once
for i in range(5):
 # calls generator_object.__next__()
    print next(generator_object)
Output will be like:
1
3
5
7
9
That's probably all the magic about the generators. Best way to get it is to open your console. NOW! And try to do it yourself.

Comments?