Last active
August 29, 2015 14:27
-
-
Save rday/05f8096d019b3483fb08 to your computer and use it in GitHub Desktop.
case1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import struct | |
| import fcntl | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| sockfd = sock.fileno() | |
| #define SIOCGIFADDR _IOWR('i', 33, struct ifreq) /* get ifnet address */ | |
| SIOCGIFADDR = 0xc0206921 | |
| def get_ip(iface): | |
| ifreq = struct.pack('16sH14s', iface.encode('utf-8'), socket.AF_INET, b'\x00'*14) | |
| try: | |
| res = fcntl.ioctl(sockfd, SIOCGIFADDR, ifreq) | |
| except Exception as e: | |
| print(e) | |
| return None | |
| ip = struct.unpack('16sH2x4s8x', res)[2] | |
| return socket.inet_ntoa(ip) | |
| if __name__ == "__main__": | |
| print("Interface list: {}".format(socket.if_nameindex())) | |
| print("IP of vioif0: {}".format(get_ip('vioif0'))) | |
| import time | |
| time.sleep(10) | |
| print("Creating socket...") | |
| HOST = '192.168.1.4' # The remote host | |
| PORT = 54321 # The same port as used by the server | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| print("Connecting...") | |
| s.connect(('10.10.10.10', 12345)) | |
| s.sendall('Hello, world') | |
| s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment