Python Forum
Use specific IP Address
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use specific IP Address
#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


Messages In This Thread
Use specific IP Address - by EnGeLs - Jul-12-2017, 04:52 AM
RE: Use specific IP Address - by metulburr - Jul-12-2017, 12:00 PM
RE: Use specific IP Address - by sparkz_alot - Jul-12-2017, 02:58 PM
RE: Use specific IP Address - by EnGeLs - Jul-12-2017, 04:16 PM
RE: Use specific IP Address - by wavic - Jul-12-2017, 05:14 PM
RE: Use specific IP Address - by EnGeLs - Jul-12-2017, 05:43 PM
RE: Use specific IP Address - by wavic - Jul-12-2017, 06:02 PM

Forum Jump:

User Panel Messages

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