Python Forum
Use specific IP Address
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use specific IP Address
#1
Hi guys,

I'm would like your help since I'm new with socket and packet manipulation.

I'm working on Kali Linux, I have my eth0 with 2 IP address 192.168.1.3 and 192.168.1.4, how can I force my python program to use the 192.168.1.4 IP address which is the secondary instead of 192.168.1.3??

Here my code:

import mechanize

def viewPage(url):
    browser = mechanize.Browser()
    page = browser.open(url)
    source_code = page.read()
    print source_code

viewPage("www.google.com")
Reply
#2
Are you trying to change your public IP address via a script? 192.168.X.X is your internal ip address. The address to differentiate multiple devices on your network, etc. Your public ip address is what displays if you go to a site such as https://whatismyipaddress.com/ or if you type in "ip address" in google's search bar.
Recommended Tutorials:
Reply
#3
I would first verify that you do indeed have 2 ip addresses assigned to the same device "eth0", rather than NIC1 assigned to "eth0" and NIC2 to "eth1" (for example) or perhaps a WiFi connection. I believe in Linux, the command is ifconfig . If they are indeed both assigned to to "eth0", why would you do that? It would be helpful for you to post the output of the "ifconfig" command (between the 'output' code tags, of course).
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Thanks for your answers, let me explain myself.

I'm not trying to change my Public IP, I can't, that's ISP network.

I have not 2 I have like 200 IP's on my NIC, I already create a script to do that: here the link: https://github.com/juliourena/secipmanager

What I'm trying to do is:

Create an script to visit a list of web site (on a .txt file), but generate that traffic from multiples Internals IP's.

So, I have a Firewall and I would like to test it, for general purpose (throughput, bandwidth, sessions, how many client's it can handle, etc).

This code is an example, but at the end this is what I'm looking for, any help?
Reply
#5
You have to deal with the system here. Iptables or just set temporary the mask of the address you don't want to use to 255.255.255.255
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Jul-12-2017, 05:14 PM)wavic Wrote: You have to deal with the system here. Iptables or just set temporary the mask of the address you don't want to use to 255.255.255.255

I'm able to generate a connection, but I would like to know if is possible to retrieve some info from a page, download something or print the page source code.

With this code I'm able to send traffic using other IP:

My IP address is 192.168.1.3/24 assigned by DHCP, then I use the command:

# ip address add 192.168.1.4/24 dev eth0

If I run this code I get the response back from the webpage
s =""
def netPacket(dsthost,dstport,srcip,srcport):
    global s
    
    #create an INET, STREAMing socket
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error:
        print 'Failed to create socket'
        sys.exit()
    
    print 'Socket Created'

    try:
        remote_ip = socket.gethostbyname( dsthost )
    
    except socket.gaierror:
        #could not resolve
        print 'Hostname could not be resolved. Exiting'
        sys.exit()
        
    #connect using this IP
    s.bind((srcip,srcport))
    
    #Connect to remote server
    s.connect((remote_ip , int(dstport)))
    
    print 'Socket Connected to ' + dsthost + ' with ip ' + remote_ip + ' and port ' + dstport + ' from ' + srcip + ':' + str(srcport)
    
    #Send some data to remote server
    message = "GET / HTTP/1.1\r\n\r\n"
    
    try :
        #Set the whole string
        s.sendall(message)
    except socket.error:
        #Send failed
        print 'Send failed'
        sys.exit()
    
    print 'Message send successfully'    


def recv_timeout(the_socket,timeout=2):
    #make socket non blocking
    the_socket.setblocking(0)

    #total data partwise in an array
    total_data=[];
    data='';

    #beginning time
    begin=time.time()
    while 1:
        #if you got some data, then break after timeout
        if total_data and time.time()-begin > timeout:
            break

        #if you got no data at all, wait a little longer, twice the timeout
        elif time.time()-begin > timeout*2:
            break

        #recv something
        try:
            data = the_socket.recv(8192)
            if data:
                total_data.append(data)
                #change the beginning time for measurement
                begin=time.time()
            else:
                #sleep for sometime to indicate a gap
                time.sleep(0.1)
        except:
            pass

    #join all parts to make final string
    return ''.join(total_data)

netPacket('www.eicar.org','80','192.168.1.4',23454)

print recv_timeout(s)
Reply
#7
You can get a web page contend. You already did what is needed for this. Sending a http request.
page = socket.recv(bytes_to_receive)
print(page)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020