free hit counter

Tuesday, May 25, 2010

finding IP addresses of all local adapters in Python


We've got an XmlRpcServer app written in Python, and when we wanted it to run on several different servers I thought I was being clever and rather than hardcode the IP address I'd just look it up; a quick search on the intertubes turned suggested:

serverIP= socket.gethostbyname(socket.gethostname())


This worked great on the original workstation we'd been running the script from, but when I put it on another machine it failed. Turns out, that machine has two network adapters, and you need to call the gethostbyname_ex() version (which the python docs don't have much to say about). That version of the call returns a list with all the adapters, and you need to search through them for the one you want. this is how I did it:


Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname(socket.gethostname())
'192.168.20.200'
>>> socket.gethostbyaddr(socket.gethostname())
('bigServer', [], ['192.168.20.200'])
>>> socket.gethostbyname_ex(socket.gethostname())
('bigServer.mydomain.com', [], ['192.168.20.200', '192.168.1.41'])
>>>
>>> def getIpAddrForSubnet(subnetStr):
... ipAddrs= socket.gethostbyname_ex(socket.gethostname())
... for value in ipAddrs[2]:
... if str(value).find(subnetStr) >= 0: return value
...
>>> getIpAddrForSubnet("192.168.1.")
'192.168.1.41'
>>> getIpAddrForSubnet("192.168.20.")
'192.168.20.200'

Labels: ,

Tuesday, May 18, 2010

exit(-1) in matlab *does* work

Experimentation shows that this matlab command wrapped in a perl script does do what you would expect it to do, contrary to documentation.

$status= system("matlab.exe -nosplash -nojvm -nodisplay -wait -r \"exit(1)\"");

print "\n\nmatlab exited with status: $status\n";

Why should I be surprised?

Matlab is a terrible hodge-podge and that's why I think people like it so much. Just like Perl, or labyrinthine city streets. Still, it's not my kind of city, even if I have to visit all the time.

If you're silly enough to read the documentation, the exit function doesn't take an integer argument for the process return value:

>> help exit
EXIT Exit from MATLAB.
EXIT terminates MATLAB after running finish.m, if finish.m exists.
It is the same as QUIT and takes the same termination options.
For more information, see the help for QUIT.

See also quit.

Reference page in Help browser
doc exit

Further reading through the help and doc results of quit show no numeric arguments for process return values.

Labels: ,

Wednesday, July 29, 2009

BLD_NUM via awk or perl

Trying to grab the build number out of my autogenerated file:

#define BLD_MAJOR 1
#define BLD_MINOR 5
#define BLD_STAMP 0
#define BLD_NUM 4541
#define BLD_STR "1.05.00.4541"
#define BLD_VAL 105004541L

awk is easy:

awk '/BLD_NUM/ {print $3}'

Perl runs outside of cygwin, so I went for this instead:

perl -lane 'if ($F[1]=~/BLD_NUM/){ print $F[2];}'

Labels: ,