Connect from specific ip address in linux - linux

I'm writing a distributed system and I want to test it on my machine. I created several ip addresses on the interface lo using ip addr add ip_add dev lo.
I have binded all servers to their specific addresses and now I want my servers to connect to each other such that each server would think that it connects from his own ip. But when I use connect I get connection from my localhost. How is it possible to connect from a specific ip address?

It turned out that calling bind() on my socket does all necessary work.

Related

How to add the same port on different network interface on the same machine?

I have a fedora workstation with 5 physical network interfaces on it.
Four of the network interface have ip 10.10.10.11 10.10.10.12 10.10.10.13 10.10.10.14.
There runs a filerun serice(port 8081),a gitlab serivce(port 8082), and a transmission(port 8083) service via docker. On my mac, I could access 10.10.10.11:8081 or 10.10.10.12:8081 or 10.10.10.13:8081 or 10.10.10.14:8081.
What I want is to access filerun via 10.10.10.11:80, gitlab 10.10.10.12:80, transmission via 10.10.10.13:80. How to configure the network?
Thanks a lot.
You have to bind on the right IP address/interface each service using the same port.
Ports (UDP or TCP) have their own pool per IP address.
You can listen on the same port if you change IP address or protocol (UDP or TCP).
See: http://www.bleepingcomputer.com/tutorials/tcp-and-udp-ports-explained/
Alternatively you could use a webserver like NGINX as a reverse proxy and bind the services to a specific ip address and port. Here you would also benefit in caching content and web acceleration for improved performance.
Find out how to set it up: https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

How to find the interface offering the route to a specific IP in Python3?

On a Windows machine there are different network interfaces available. One of these interfaces is the TAP interface connected to a VPN-server, with a specific IP (e.g. 10.25.1.9). To reach the host 10.2.1.1 the route leads through this interface.
I want to start a local python server program and bind it to this interface, so I need to know the IP address to bind to. Since this local server program shall be installed on several machines from which I don't know the IP addresses, I want to find this IP automagically.
It can be that there are several VPN connections installed on the machine, so neither a IP address prefix is unambiguous nor the interface name can safely assumed to be unique.
The only safe thing I know about the interface is the fact that through it the host 10.2.1.1 can be reached (probably I will test some simple service on that host to be sure that this is the right one).
I had a look at netifaces but it does not offer this. I also had a look at PyRoute2 but this is not available on Windows machines.
So, my question remains: how can I find out which interface the route to my VPN base host 10.2.1.1 leads through, so I can take this IP to bind to?
Provided that there is any known service to connect to on the remote host, use socket.getsockname() to find out which local IP is used for connecting:
import socket
# connect to known destination, e.g. via UDP port 80
test_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_sock.connect(("10.2.1.1", 80))
# check which local IP was used to connect
with test_sock:
private_ip, *_ = test_sock.getsockname()

How to write a Node.js server if my pc's ip address changes daily

I am owning a Windows PC. I have written a Web application that runs fine. But the problem is my PC's IP changes periodically. I want that application to be accessible in the network (from other pc's) without changing client side code . My client side code is in angular js.
The web server of your web application cannot bind to the correct IP if it doesn't know it in advance.
You can make your IP address static by following these steps:
In Windows, go to the Network and Sharing center
In the left pane, choose "Change adapter settings"
Right click your ethernet or wifi connection (the one connected to your router) and choose Properties
Double click Internet Protocol Version 4 (TCP / IPv4)
As an IP address, choose the one your computer already has (or try another IP address starting with the first same 3 numbers, i.e. xxx.xxx.xxx.yyy)
The default gateway is the IP address of your router
The subnet mask is typically 255.255.255.0
For the DNS server choose Google's 8.8.8.8 and/or your router's IP address
You have several choices depending upon your configuration.
If this is the public dynamic IP you get from your ISP, you can go to your ISP (whoever you get internet service from) and upgrade your service to a static IP address so it won't change.
If this is the public dynamic IP you get from your ISP, you can use a dynamic DNS service to attach a hostname to your IP address and then use that hostname to access the server. The dynamic DDNS app you put on your server will keep the DNS updated whenever your dynamic IP address changes.
If this is just a local IP address on your LAN (a 192.x.x.x or 10.x.x.x address), then you can pick an IP address that your router supports, but is above the range being used for DHCP and set that computer to use that IP address and not DHCP. In Windows, you can go to the networking configuration and choose the IP address instead of using DHCP. Then, the IP address won't ever change. It is important that you pick an address outside the range used for DHCP to avoid any conflicts. On my own LAN where the router is allocating addresses like 192.168.0.x, I manually assign addresses like 192.168.1.250.
This is something that your router handles through a protocol known as DHCP. Basically, the answer is to keep your IP from changing.
Many routers allow you to reserve an IP for certain MAC addresses. I would recommend that you access your router over your local network and work with the GUI it provides to try to configure this... if you're successful, then your IP will no longer change, and problem solved :-) If it's not intuitive, then of course refer to the documentation for your router.

How to connect to a computer not the modem using internet IP address?

I configured IIS in my windows 7, and when I enter 'http://localhost' in address bar; I can see my web site. Now, when I want to see my web site by entering internet IP address in address bar, I connect to the ADSL modem (it opens the page which we use to configure the modem!).
It seems that because the modem connects to the internet an gets the IP, not the PC, so the internet IP connects me to the modem. So, I think, I must change some settings of my modem.
I use a D-Link modem.
How can I fix that?
Thanks in advance
Unless you have a "server" or "business" configuration from your ISP which provides you a full subnet of public IP addresses, you've been allocated a single external IP address and the router attached to it does Network Address Translation for all the devices connected behind it. You can confirm this by using ipconfig or Windows Settings (ifconfig on Unix-like machines) to get your IP address. If using NAT, it will start with "10.", "172.16." through "172.31.", or "192.168.". These are "private" addresses and cannot be reached through the public internet.
For someone on the public internet to reach your computer, you need to set up Port Forwarding that redirects incoming traffic on your public, external IP to that port to a machine on the private network. The configuration pages for your router will have this configuration somewhere.
Note that if your router's configuration page is running on port #80 and you really want outside viewers to connect to you without giving an explicit port number, you will probably need to turn off or restrict modem configuration, move it to a another port, or go SSL only (port 443) so as to not cause a conflict with the port you're forwarding.
D-Link is a very common brand of router and there are pages dedicated to configuring port forwarding on them.
Also, just to complicate things, you almost certainly haven't been given a Static IP Address (they are usually quite expensive) which means that your external IP address will change from time to time (perhaps yearly, perhaps daily) making it difficult to tell others how to connect to your page. Your router configuration likely has support for Dynamic DNS (some free, some paid) where the router automatically updates the DNS entry whenever your public IP address changes.

how to use host name in the place of ip address in the socket programming in java?

I am using socket programming to connect two PCs in java through wi-fi. I used the ip address of the server at the client side. but the problem is that when the ip address of the server got changed then i have to change the ip address of server at client side. and when i use host name for connection nothing happens. is there any way to use static ip or any other solutions that can work to connect both of them which can work on any network. independent of ip address of the server.
please suggest me the solution.
i think there is a simple solution to your problem ...
you should get the ip address of your device by using Inetaddress.getbyName("user-pc").getHostAddress()
hope it will work for u...
1. First of all If you are on LAN, then Any IP from Class A, B, C will suffice, But if you want it over the Internet, then you will have to be careful about the Private and Public IPs.
2. For static IP over internet you need to contact you Internet Server Provider, or you can try out some sites over the net that provides static ip based on your dynamic ips.
3. But if you want that due to the change in your server ip the client code need NOT be changed then you can do the following...
- Use the Domain Name of the Server instead of IP, by using Domain Name Server.
- Instead of storing the raw IP of the Server in the Client code, use Some variable which gets the IP of the server from some file like the Property file, Database etc...

Resources