how to get netmask address from IP in MFC(VC++) ?
Thanks.
don't forget that one PC can have multiple ip addresses so you will have to choose one..
i'm guessing your question is actually: "how can i find the netmask of one of my local ip addresses?"
then:
1) you can do it easily by parsing the output of the system command: "netstat -rn" / "route print"
2) you can do it in the hard way, using the GetIpForwardTable function. (http://msdn.microsoft.com/en-us/library/aa365953(VS.85).aspx#)
Related
I have to make a STUN server in OpenSIPs, and it says that I need to bind 2 IP addresses.
http://www.opensips.org/About/News0042
A STUN server uses 2 ips and 2 ports to create 4 sockets on which to listen or respond.
STUN requires 2 routable ip addresses
How can I enable two public IP addresses into one Linux server? I've searched all website, and failed to find the answer.
Several options.
Option 1.
You likely just need to use ifconfig from the command line to start
You can assign an additional static IP address to your NIC via the command line. Type ifconfig to get the name of your default adapter. It's typically "eth0". Then do add a secondary address to this adapter, the command is something like the following:
sudo ifconfig eth0:1 inet up netmask 255.255.255.0 192.168.1.55
Where 255.255.255.0 is the netmask of my 8-bit subnet and 192.16.1.55 is an existing IP address that no other device on my subnet is already using.
Option 2.
After you get your server up and running with Option 1, you likely need to find a way to get the IP address assigned by "ifconfig" to persist after a reboot. You could likely stick an ifconfig statement into one of your rc.init files. But most Linux skus have a formal way of configuring an interface with another /etc file. But this step varies between different flavors of Linux. On Ubuntu, this is all defined in the /etc/network/interfaces file. Add these three lines to the bottom of your existing file:
iface eth0:1 inet static
address 192.168.1.55
netmask 255.255.255.0
Option 3 (shameless plug)
Switch to Stuntman ( www.stunprotocol.org ) as your STUN server. Its default mode only requires one IP address to be present on the box. Most client usages of the STUN protocol don't require the second IP address unless to do NAT classification and behavior tests.
I want to know the IP adress of my linux (virtual machine)
but when i enter ifconfig it doesn't return an IP adress.
Normally it should be between "link encap" and "up broadcast" but it's not..
where to look?
Does your VM have internet connection? It seems to me that it doesn't have the interface configured.
Quick try, write in a shell "dhclient eth0".
If it still doesn't have IP, post the output of ifconfig.
I work on embedded device with linux on it. I want to use DHCP client first, but if there will be no answer from DHCP Server I want to set static-default IP. I suppose it shouldn't be complicated, but I haven't found strict answer.
I'm thinking about 2 solutions (Unfortunately I can test them in few days):
I set static IP with ifconfig, then I call udhcpc. If udhcpc will not obtain new IP, old one will stay.
I can also first call udhcpc, wait a while and check if IP is obtained. But this is not nice for me. I wouldn't like to add any wait routines into startup.
BR
Bartek
I use udhcpc - something like:
udhcpc -n -f -i eth0
if ifconfig | grep -A1 eth0 | grep inet
then
dhclient should support fallback via lease declaration
have a look at the dhclient.conf man page.
Add something like this to your dhclient.conf
timeout 10;
lease {
interface "eth0";
fixed-address 10.0.0.10;
option subnet-mask 255.255.255.0;
renew 2 2022/1/1 00:00:01;
rebind 2 2022/1/1 00:00:01;
expire 2 2022/1/1 0:00:01;
}
or you can assign a second IP to the interface like /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto eth0:1
iface eth0:1 inet static
address 10.10.10.2
netmask 255.255.255.0
Although an old question, it might be worth noting here that Gentoo Linux has had this functionality for a long time (I've been using it since 2004). Gentoo's network config file (/etc/conf.d/net) allows for fallback IP addresses to be easily specified for any interface in the event that DHCP fails, e.g.:
modules="dhclient"
config_eth0="dhcp"
config_eth1="dhcp"
dhclient_eth1="nogateway"
fallback_eth0="apipa"
fallback_eth1="192.168.10.10/24"
fallback_routes_eth1="default via 192.168.10.1"
The solution Maurizio provided to use an alias like eth0:0 is fine in many (probably most) situations, but not all. I've run into one piece of software that does not consider eth0:0 to be a suitable substitute for eth0 when it is undefined due to no answer from DHCP, even though it is the same port. So a static fallback address is slightly superior to the alias solution.
I need to write a script to set ip address/mask/broadcast as an alias on eth0:0 plus set the default gateway.
This solution works:
ifconfig eth0:0 <ip> netmask <mask> up
ip route replace default via <ip>
but sometimes the second call gets an error "network unavailable".
Adding a sleep between them fixes it, but is unreliable. What is the proper way to wait for the network to be ready?
The best I came up with so far is retrying the ip call a couple times. This works, but feels ugly.
You could use ping -c1 -w on the gateway address in a loop until it comes up.
I think it is a bit strange that the interface isn't up when ifconfig returns.. I would try to skip ifconfig and only use the 'ip' command:
ip address add <ip>/<mask> dev eth0
ip route replace default via <ip>
This doesn't create a new alias interface eth0:0, it just configures an additional ip address on the primary interface, visible with 'ip address list'. I'm not sure if this works better, but it is worth a try.
In a shellscript, I'd like to set the IP of my box, run a command, then move to the next IP. The IPs are an entire C block.
The question is how do I set the IP of the box without editing a file? What command sets the IP on Slackware?
Thanks
As mentioned in other answers, you can use either the ifconfig command or the ip command. ip is a much more robust command, and I prefer to use it. A full script which loops through a full class C subnet adding the IP, doing stuff, then removing it follows. Note that it doesn't use .0 or .255, which are the network and broadcast addresses of the subnet. Also, when using the ip command to add or remove an address, it's good to include the mask width, as well (the /24 at the end of the address).
#!/bin/bash
SUBNET=192.168.135.
ETH=eth0
for i in {1..254}
do
ip addr add ${SUBNET}${i}/24 dev ${ETH}
# do whatever you want here
ip addr del ${SUBNET}${i}/24 dev ${ETH}
done
It should be something like: ifconfig eth0 192.168.0.42 up
Replace eth0 by the network interface of your network card, obviously adapt the ip address to your needs and the up is only necessary once, but doesn't hurt if you run it each time.
I don't know Slackware very well, I last used it over ten years ago. However, any mainstream Linux distribution should have either the 'ifconfig' program, or the 'ip' program or both. You will need to have root privilges, so either become root (e.g with su) or use the 'sudo' program if you know how. Let's do it with 'ip' first.
ip addr add 10.1.2.3 dev eth0
sets the device eth0 (usually the primary wired network adaptor) to have IP address 10.1.2.3. You can remove the address from this adaptor again when you're done with it...
ip addr del 10.1.2.3 dev eth0
ifconfig works a bit differently,
ifconfig eth0 10.1.2.3 netmask 255.255.255.0
again sets up device eth0, with IP address 10.1.2.3
Depending on what you want these addresses for, you may also need to know how to set up a manual route, so that your IP packets actually get delivered wherever they're going.
In one line, e.g.: ifconfig eth0 192.168.10.12 netmask 255.255.255.0