views:

1516

answers:

5

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

A: 

Read the man page for ifconfig.

Frank Wiles, Revolution Systems, www.revsys.com

Frank Wiles
A: 

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.

A: 

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.

tialaramex
+2  A: 

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
Thomee
A: 

In one line, e.g.: ifconfig eth0 192.168.10.12 netmask 255.255.255.0

Eduardo