Nov 25, 2011

Change IP using a script or command line in windows


Try This out:

@echo off

REM # Created by Thilina  Piyasundara.

set /P asda=Do you want to change the IP (y/n) : 

if "%1" == "yes" (
 ECHO Setting IP Address
 netsh int ip set address name = "Local Area Connection" source = static addr = 192.168.1.2 mask = 255.255.255.0

 ECHO Setting Gateway
 netsh int ip set address name = "Local Area Connection" gateway = 192.168.2.250 gwmetric = auto

 ECHO Setting Primary DNS
 netsh int ip set dns name = "Local Area Connection" source = static addr = 192.168.2.1
 
 GOTO SETSETTINGS
)

if not "%1" == "yes" (
 ECHO Nothing happened.
)

:SETSETTINGS
netsh int ip show config
:end
 
pause

Use the netsh commands to do the same thing in the command line.

Login error when installing MSSQL 2008

I try to instal MSSQL 2008 server on Windows XP SP3.
Installation is fine but at the end it prompts an error like this.

"           ' ' is not a valid login or you do not have permission.               " 

I use "NT Authentication\System" and I add the current user to the SQL server administrators section.

Then I read some forums and found my "user name" and "pchost name" are same. It appears like "myname\myname". It may be the issue. I created a new admin user with a different name and try to re-install the server again.

There were no errors. Finally I realize, if I try the default Administrator account to install SQL server it will not create an unnecessary account.

My opinion is :-

  • Remove the SQL server.
  • Log out from the user.
  • Then press two times Alt+Ctrl+Del.
  • Then type "administrator" as the username and press enter (If you have setup the password to the administrator account you need to give it.)
  • Start the installation.
  • Use NT Authentication\System.
  • Give a password to the SA (default) account.
  • Add current user.
  • And continue.

Nov 13, 2011

Socket Programming with Python - Echo server

This is a basic socket program. You need to run the server first and then run the client.

Echo server (pechoserver.py)
#!/usr/bin/python

import socket

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("",5200)) # server is running on Port 5200
s.listen(1)

while 1:
 conn, addr = s.accept()
 print 'Connected IP ' , addr
 data = conn.recv(1024)
 if not data:
  break
 conn.send(data)
 conn.close()


Echo client (pechoclient.py)
#!/usr/bin/python

import socket
import sys

if (len(sys.argv)<1):
        print 'usage : pechoclient.py "message"'
        exit(0)
else:
        msg=sys.argv[1]
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.connect(("localhost",5200))
        s.send(msg)
        data=s.recv(1024)
        s.close()
        print "Recived : " ,data


Run the server ;
$ pyhton pechoserver.py &

Run the client ;
$ python pechoclient "Hello World."

Run client and server in different terminals to get a clear understanding of the output.

For more : http://docs.python.org/library/socket.html
                 http://docs.python.org/howto/sockets.html

Nov 10, 2011

Log your Python script - Python Logging Hello World

This is a basic logging script used in python.




#!/usr/bin/python

import sys
import logging

if (len(sys.argv)==2):
 loglevel=sys.argv[1]
else:
 loglevel="WARNING"

logging.basicConfig(filename='./mylogging.log',\
 format='%(levelname)s : %(asctime)s - %(message)s',\
 level=loglevel)
logging.debug('Logging started.')
logging.warning('Logging started.')

print "Hello World."
logging.debug("Print :- Hello World.")

logging.debug("End.")
logging.warning("End.")
exit(0)


Here I'm using a library called "logging" to do the logging for me. You can statically give the log level without passing it as an argument. And also there are many log levels you can use according to your need.

For more details :-  http://docs.python.org/library/logging.html

Your suggestions are welcome.