Re: how to get python socket to use a specific interface

2015-10-28 Thread Cameron Simpson

On 28Oct2015 10:41, Robin Becker  wrote:

binding to the local IP seems to be a windows only thing.

No, it is a pretty standard BSD socket layer thing. (Windows got its original
TCP stack from there too). I just tested a Linux RHEL6 host binding to a
specific address just now using telnet:

 /usr/bin/telnet -b x.x.x.193 x.x.x.174 22

where the .193 is not the primary address - it is an additional local address.
The connection was correctly received by the target as from the alias address,
not the base address:

I don't think I'll be able to do all I need with telnet :(


Indeed:-( But it is very handy as a test for basic connection stuff in the 
field.



Please show me the exact code you're using. This really should work without
annoying "device" binding. [...]


Well originally I was hacking on miproxy to try and get it to use a specific 
ip address. I must have messed up somewhere there as when I try this more 
obvious code [...]



from socket import socket, SOL_SOCKET
BIND_DEVICE='eth0.0'
sock = socket()
sock.settimeout(10)
sock.bind(('xx.xx.xx.13', 0))
#sock.setsockopt(SOL_SOCKET, 25, BIND_DEVICE)
sock.connect(("int.hh.com", 80))
sock.send("GET / HTTP/1.0\r\n\r\n")
print sock.recv(20)
sock.close()


it does work as intended and I can see the .13 address hitting the 
remote server. I guess my hack of the miproxy code didn't work as 
intended.


That is reassuring to me. Thanks for checking. Reaching for a particular device 
is annoying and weird and possibly even pointless.


Anyhow my upstream provider has taken over the problem so hopefully I will get 
the address cleared at some point.


Ok.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get python socket to use a specific interface

2015-10-28 Thread Robin Becker

..


binding to the local IP seems to be a windows only thing.


No, it is a pretty standard BSD socket layer thing. (Windows got its original
TCP stack from there too). I just tested a Linux RHEL6 host binding to a
specific address just now using telnet:

  /usr/bin/telnet -b x.x.x.193 x.x.x.174 22

where the .193 is not the primary address - it is an additional local address.
The connection was correctly received by the target as from the alias address,
not the base address:

I don't think I'll be able to do all I need with telnet :(





Please show me the exact code you're using. This really should work without
annoying "device" binding.

The counter examples in the articules you cite are for particularly weird
circumstances, such as where the routing table cannot correctly deduce the
interface (distinct attached networks with the _same_ network numbering -
ghastly). They don't say "binding to the local IP seems to be a windows only
thing" that I can see.

Please post your failing code. I suspect you're missing something.

...
Well originally I was hacking on miproxy to try and get it to use a specific ip 
address. I must have messed up somewhere there as when I try this more obvious code



from socket import socket, SOL_SOCKET
BIND_DEVICE='eth0.0'
sock = socket()
sock.settimeout(10)
sock.bind(('xx.xx.xx.13', 0))
#sock.setsockopt(SOL_SOCKET, 25, BIND_DEVICE)
sock.connect(("int.hh.com", 80))
sock.send("GET / HTTP/1.0\r\n\r\n")
print sock.recv(20)
sock.close()


it does work as intended and I can see the .13 address hitting the remote 
server. I guess my hack of the miproxy code didn't work as intended.


Anyhow my upstream provider has taken over the problem so hopefully I will get 
the address cleared at some point.

--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get python socket to use a specific interface

2015-10-27 Thread Robin Becker

On 26/10/2015 22:29, Cameron Simpson wrote:

On 26Oct2015 12:33, Robin Becker  wrote:

.



$ ifconfig
eth0  Link encap:Ethernet  HWaddr 00:...4 GB)
 Interrupt:16

eth0:0Link encap:Ethernet  HWa...


Do you need to bind to the device itself? Using the correct IP address should
normally be sufficient. The system you're talking to won't know anything about
the device, only the address. Try skipping the device binding step.



According to the stackoverflow articles here

http://stackoverflow.com/questions/335607/how-do-i-make-an-outgoing-socket-to-a-specific-network-interface

http://stackoverflow.com/questions/8437726/can-python-select-what-network-adapter-when-opening-a-socket

binding to the local IP seems to be a windows only thing. I have tried just 
binding to the local IP, but my packets were from the default address (the one 
connected to eth0). After reading the 8437726 article again I think I may be on 
the wrong track anyway.




Cheers,
Cameron Simpson 



--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get python socket to use a specific interface

2015-10-27 Thread Cameron Simpson

On 27Oct2015 10:00, Robin Becker  wrote:

On 26/10/2015 22:29, Cameron Simpson wrote:

On 26Oct2015 12:33, Robin Becker  wrote:

.



$ ifconfig
eth0  Link encap:Ethernet  HWaddr 00:...4 GB)
Interrupt:16

eth0:0Link encap:Ethernet  HWa...


Do you need to bind to the device itself? Using the correct IP address should
normally be sufficient. The system you're talking to won't know anything about
the device, only the address. Try skipping the device binding step.


According to the stackoverflow articles here

http://stackoverflow.com/questions/335607/how-do-i-make-an-outgoing-socket-to-a-specific-network-interface

http://stackoverflow.com/questions/8437726/can-python-select-what-network-adapter-when-opening-a-socket

binding to the local IP seems to be a windows only thing.


No, it is a pretty standard BSD socket layer thing. (Windows got its original 
TCP stack from there too). I just tested a Linux RHEL6 host binding to a 
specific address just now using telnet:


 /usr/bin/telnet -b x.x.x.193 x.x.x.174 22

where the .193 is not the primary address - it is an additional local address.  
The connection was correctly received by the target as from the alias address, 
not the base address:


 Oct 28 10:28:18 HOSTNAME sshd[7531]: Connection from x.x.x.193 port 61621

An strace says:

 socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3 bind(3, {sa_family=AF_INET, 
sin_port=htons(0), sin_addr=inet_addr("203.166.218.193")}, 16) = 0

 setsockopt(3, SOL_IP, IP_TOS, [16], 4)  = 0

 connect(3, {sa_family=AF_INET, sin_port=htons(22), 
sin_addr=inet_addr("203.166.218.174")}, 16) = 0

so you can see it just binds the source address and goes. No device names 
involved.


I have tried just binding to the local IP, but my packets were from the 
default address (the one connected to eth0). After reading the 8437726 article 
again I think I may be on the wrong track anyway.


Please show me the exact code you're using. This really should work without 
annoying "device" binding.


The counter examples in the articules you cite are for particularly weird 
circumstances, such as where the routing table cannot correctly deduce the 
interface (distinct attached networks with the _same_ network numbering - 
ghastly). They don't say "binding to the local IP seems to be a windows only 
thing" that I can see.


Please post your failing code. I suspect you're missing something.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get python socket to use a specific interface

2015-10-26 Thread Cameron Simpson

On 26Oct2015 12:33, Robin Becker  wrote:

.

device? --
Robin Becker


Using eth0:0 is normally a method to setup eth0 to respond to a 2nd
IPV4/IPV6 address.  Have you done the ifconfig steps to enable that?  If
its been done, you will see it's 2nd address in an ifconfig query.  Man
pages are wunnerful things.
yes I have done that and the address is responding, problem is it has 
been email blacklisted



$ ifconfig
eth0  Link encap:Ethernet  HWaddr 00:...4 GB)
 Interrupt:16

eth0:0Link encap:Ethernet  HWa...


Do you need to bind to the device itself? Using the correct IP address should 
normally be sufficient. The system you're talking to won't know anything about 
the device, only the address. Try skipping the device binding step.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get python socket to use a specific interface

2015-10-26 Thread Robin Becker

.

device? --
Robin Becker


Using eth0:0 is normally a method to setup eth0 to respond to a 2nd
IPV4/IPV6 address.  Have you done the ifconfig steps to enable that?  If
its been done, you will see it's 2nd address in an ifconfig query.  Man
pages are wunnerful things.
yes I have done that and the address is responding, problem is it has been email 
blacklisted



$ ifconfig
eth0  Link encap:Ethernet  HWaddr 00:...4 GB)
  Interrupt:16

eth0:0Link encap:Ethernet  HWa...

loLink encap:Local Loopback

..


The problem is as stated in the original email; when I try to execute the 
commands to bind to that device I get an error eg




# ~rptlab/tmp/proxy/bin/python
Python 2.7.5 (default, Aug 20 2013, 15:27:38)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

from socket import socket, SOL_SOCKET
BIND_DEVICE='eth0:0'
sock = socket()
sock.settimeout(10)
sock.setsockopt(SOL_SOCKET, 25, BIND_DEVICE)

Traceback (most recent call last):
  File "", line 1, in 
  File "/home/rptlab/LOCAL/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 19] No such device





so I guess I'm not using the right name for the device. I tried a leter python 
3.5 in case my python 2.7 is too old, but that also fails.


--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get python socket to use a specific interface

2015-10-25 Thread Gene Heskett
On Friday 23 October 2015 05:03:41 Robin Becker wrote:

> I need to run lynx on a ubuntu headless server with a specific IP
> address in order to try and cancel an email blacklist.
>
> Lynx doesn't provide a way to do that so I thought to use a local
> proxy. I tried pymiproxy and hacked the connection code so it looked
> like this
>
>
> proxy.py
> 
>
> > from socket import socket, SOL_SOCKET
> > ...
> > import os
> > BIND_ADDRESS = os.environ.get('PYMIPROXY_BIND_ADDRESS',None)
> > BIND_DEVICE = os.environ.get('PYMIPROXY_BIND_DEVICE',None)
> > .
> > # Connect to destination
> > sock = self._proxy_sock = socket()
> > sock.settimeout(10)
> > if BIND_ADDRESS:
> > sock.bind((BIND_ADDRESS, 0))
> > if BIND_DEVICE:
> > sock.setsockopt(SOL_SOCKET, 25, BIND_DEVICE)
> > sock.connect((self.hostname, int(self.port)))
>
> 25 is derived from /usr/include/asm-generic/socket.h
>
> > #define SO_BINDTODEVICE 25
>
> This works if I export BIND_DEVICE='eth0' and run as root, but my
> desired interface is actually called 'eth0:0' and when I run with that
> exported I get a device error. Does anyone know what I should call the
> device? --
> Robin Becker

Using eth0:0 is normally a method to setup eth0 to respond to a 2nd 
IPV4/IPV6 address.  Have you done the ifconfig steps to enable that?  If 
its been done, you will see it's 2nd address in an ifconfig query.  Man 
pages are wunnerful things.

I think you can use it as a normal user, but the ifconfig IIRC has to be 
done by root, or sudo.

One of my printers has a cat5 socket, which turns out to be its fastest 
interface, and I had to setup an eth0:0 in 192.168.0.## block for a day 
or so until I had wandered thru its menus and found I could put it on my 
local networks class D block and did, so eth0:0 was no longer required.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 
-- 
https://mail.python.org/mailman/listinfo/python-list