Thursday, June 30, 2011

Google+ invites

My friends and I were trying to figure out how to send requests. We were kind of late enough to miss the "send invite" active phase. Thanks to Anjali, figured out what to do...
Add people you want to invite to a circle
Make a post and add the circle
OR
just make a post and share the post with the email-ids

Byes

Saturday, June 25, 2011

SSH client side



I started using ssh only few weeks back and slowly I realized what I had been missing all the time.

According to Wikipedia:


Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices. The two major versions of the protocol are referred to as SSH1 or SSH-1 and SSH2 or SSH-2. Used primarily on Linux and Unix based systems to access shell accounts, SSH was designed as a replacement for Telnet and other insecure remote shells, which send information, notably passwords, in plaintext, rendering them susceptible to packet analysis. The encryption used by SSH is intended to provide confidentiality and integrity of data over an unsecured network, such as the Internet.
SSH uses public-key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if necessary. Anyone can produce a matching pair of different keys (public and private). The public key is placed on all computers that must allow access to the owner of the matching private key (the owner keeps the private key in secret). While authentication is based on the private key, the key itself is never transferred through the network during authentication.
SSH only verifies if the same person offering the public key also owns the matching private key. Hence in all versions of SSH, it is important to verify unknown public keys before accepting them as valid. Accepting an attacker's public key without validation would simply authorize an unauthorized attacker as a valid user.


If you don't have OpenSSH installed, you can get it by
sudo apt-get install openssh-client


To use public keys with an ssh server, you'll first need to generate a public/private key pair:
ssh-keygen -t rsa
-t dsa for using DSA key


Now it is time to enter filename and passphrases
The following things will come up:
Generating public/private rsa key pair.
Enter file in which to save the key (//.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in //.ssh/id_rsa.
Your public key has been saved in //.ssh/id_rsa.pub.
The key fingerprint is:
bc:e7:ee:2c:5d:6c:b4:e9:af:e0:a3:58:26:eb:43:83 utsav@utb-desktop

The private key was saved in the read-only file .ssh/id_rsa. It is used to decrypt all correspondence encrypted with the public key, and so no one else should have access to that file. The public key is saved in .ssh/id_rsa.pub file.
Now, copy the public key onto a remote systems' .ssh/authorized_keys2 file and make the file permissions 0x600, so it is only read/writable by you. Without these permissions, ssh will refuse to use the key. And now you can SSH to the remote systems's account without using a password.
ssh-copy-id remotehost
or you can
ssh server "mkdir .ssh; chmod 0600 .ssh"
scp .ssh/id_rsa.pub server:.ssh/authorized_keys
(don't forget to replace your server name for server)
or you can
cat ~/.ssh/id_rsa.pub | ssh user@server “mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys”
(just in case the machine does not have ssh-copy-id)


Here is a trick: Keep the pass phrase empty. It will enable fast password-less login. However, it is unsafe. 
Otherwise, for safe password-less login
Start ssh agent
ssh-agent $SHELL
Then, load your private key into the ssh agent
ssh-add
The following will come up
Enter passphrase for //.ssh/id_rsa: < Enter passphrase here >
Identity added: //.ssh/id_rsa (//.ssh/id_rsa)
Other available ssh-add options are

ssh-add <key-file-name>: Load a specific key file.
ssh-add -l: List all the key loaded in the ssh agent.
ssh-add -d <key-file-name>: Delete a specificy key from the ssh agent
ssh-add -D: Delete all key


Some simple and useful commands:
to know the version of the ssh(OpenSSH or SSH2):
ssh -V
to connect to a server using ssh protocol
ssh user@server
to connect and redirect X11 protocol(to display windows) through ssh
ssh -X user@server
to execute command in the remote host
ssh user@server remote command
to execute same command in a number of hosts
for server in server1 server2 server3; do echo -n $server:; ssh $server uptime; done;
to connect using an intermediate server
ssh -t server_intermediate ssh -t server_final
to convert OpenSSH public key to SSH2 public key:
ssh-keygen -e -f ~/.ssh/id_rsa.pub > ~/.ssh/id_rsa_ssh2.pub
to connect to a specific port
ssh -p portnumber user@someserver
to start a tunnel from some machine’s port 80 to your local post 2012
ssh -N -L2012:localhost:80 user@someserver


For more information





Friday, June 24, 2011

Shoot the Bonkars (Simple 2-D FPS Game)







My aim with this tutorial is to share the experience i had of designing a 2-D FPS inPython. I will try and be as precise as i can and the the same time not go into theunnecessary details.Devel's Requirements Windows platform with python 2.4/2.5/2.6(I used Python 2.5) Pygame : this provides you with the basic functions to handle events like mouse click, key press. (I used Pygame 1.9.1) Text Editor: I used Notepad++. Basics Knowledge of coding in Python(FOR,WHILE,IF,Function Calling).

    Now (asuming you have all the Devel's Requirement mentioned above) we proceed to using Pygame to create a 2-D FPS.

    Opening A window

    1.)First thing that we need to do is create an open window in which the game would run.
    The following code opens a window in Python
    #!/usr/bin/python -tt import pygame
    from pygame.locals import *def main():  
    screen = pygame.display.set_mode((800, 600))
    if __name__ == '__main__':   
    main()

    CODE DESCRIPTION

    import pygame # this imports pygame in the code so that we can use the subroutines it provides(like those for images,sound etc)
    from pygame.locals import * # this imports a number of functions and constants into the top-level namespace. It isn’t essential to do this in order to use Pygame, but it is convenient because we don’t have to precede frequently used values with the pygame namespace.
    def main() # this creates the main function
    screen=...(800,600) # this is the only line in the above program that gives result . it opens a screen of size (800x600) as given.
    if __name......main() # this calls the main function.

    The screen closes as soon as the program terminates. Whats next required is a way to keep that window open until we want to close it. This can be doen by adding a while loop after displaying the screen. The while loop must be inatialise to true to keep the screen open and exit be made when we click on the close button of the screen.

    2.)The code given below does the required job. Add it after the screen=...(800,600) line
    while True:
      for event in pygame.event.get():
       if event.type == QUIT:    
          exit()

    CODE DESCRIPTION
    while True: # this line starts a continuous while loop to maintain the screen open.
    for .....event.get(): # this line iterates through all events in the code.
    if .....==QUIT # this line checks for the event QUIT (click on close button).
    exit() # this close the while and makes the exit.

    3.)Next thing we are going to add is image.
    Adding any image to the screen is a 3-step process.
    i)- Load the image hard drive as surface containing the image data and convert the image to the same format as our display.
    ii)-Load this converted surface on the screen at a desired position (x,y)
    iii)Flip the display

    the code performs the task stated above:
    background = pygame.image.load("image1.png").convert() # Does task i
    screen.blit(background, (x, y)) # Does task ii)
    pygame.display.flip() # does task iii)

    obviously the last 2 line (task needs to be inside the while loop if you are performing operations )
    if you rather prefer to color your screen background with some color, Type the code below
    screen.fill((RR,GG,BB))
    obviously (RR,GG,BB) is the color code.

    4.)using the above method i planted 4-images (small 50x50) on screen at random positions and made them move here and there at constant speed. the images also bounced back(changed there direction on colliding with any of the 4 walls or boundary).

    5.)Now you have got your bonkars . All you need now is a bullet to shoot and a cannon to shoot from. For this have the cannon's position fixed say at bottom center of the screen (at 400,550 on a 800x600 screen).Paste the picture of a cross-wire on the mouse position .Get the mouse position with command "pygame.mouse.get_pos()" and then write an equation to propagate the bullet from cannon's position to the mouse position at the shoot-time.

    6)If u want only 1 bullet then avoid shooting bullets until the previous one's co-ordinate has left the screen. Otherwise if you want more than one bullets keep an array of flags that indicates whether a bullet is in use or not together with its co-ordinates. If the co-ordinatesof the bullets have left the screen the particular bullets flag is again set as "not in use". If all bullets are in use then you may flash "overheat" as a signal to the user to stop for some time.


    using these ideas i developed my First Game in Python. the code for it is available on the link below............https://sourceforge.net/projects/shootthebonkars/

    Flashing keyboard LEDs










    Here is a simple script to make a keyboard's LED blink
    Copy the script below and save it as .wsf
    Rem blinking LED :vstsv
    <package>
       <job id="vbs">
          <script language="VBScript">
             set WshShell = WScript.CreateObject("WScript.Shell")
               For count=0 to 10
                 WshShell.SendKeys "{CAPSLOCK}"
                 WScript.Sleep 500
                 WshShell.SendKeys "{NUMLOCK}"
                 WScript.Sleep 500
                 WshShell.SendKeys "{SCROLLLOCK}"
                 WScript.Sleep 500
               Next
          </script>
       </job>
    </package>
    The other alternative for Windows user is to use SendKeys Python module. You can find it here. The page also contains good tutorial and documentation.


    import SendKeys
    SendKeys.SendKeys("""
        {CAPSLOCK}
        {PAUSE 1}
        {NUMLOCK}
        {PAUSE 1}
        {SCROLLLOCK}
        {PAUSE 1}
    """)


    JAVA can be used to get the same results.


    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    
    
    public class SendKeys {
    
    
     public boolean numlock(boolean s) {
      Toolkit tool = Toolkit.getDefaultToolkit();
      try {
       tool.setLockingKeyState(KeyEvent.VK_NUM_LOCK, s);
      } catch (Exception e) {
       return false;
      }
      return true;
     }
    
    
     public boolean capslock(boolean s) {
      Toolkit tool = Toolkit.getDefaultToolkit();
      try {
       tool.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, s);
      } catch (Exception e) {
       return false;
      }
      return true;
     }
    
    
     public boolean scrolllock(boolean s) {
      Toolkit tool = Toolkit.getDefaultToolkit();
      try {
       tool.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, s);
      } catch (Exception e) {
       return false;
      }
      return true;
     }
    
    
     public static void main(String[] args) throws Exception {
      SendKeys flasher = new SendKeys();
    
    
      for(int i=0;i<10;i++){
                       flasher.numlock(true);
                      Thread.sleep(500);
                      flasher.numlock(true);
                      Thread.sleep(500);
                      flasher.capslock(true);
                      Thread.sleep(500);
                      flasher.capslock(true);
                      Thread.sleep(500);
                      flasher.scrolllock(true);
                      Thread.sleep(500);
                      flasher.scrolllock(true);
                      Thread.sleep(500);
                }
     }
    }