Tuesday, March 24, 2009

Solaris如何change网卡IP地址?

1. Temporarily change 网卡IP地址

ifconfig用于配置网卡地址,改动后立即生效,但并不保存配置,下次启动后失效。

# ifconfig -a
lo0: flags=849 mtu 8232
inet 127.0.0.1 netmask ff000000
hme0: flags=863 mtu 1500
inet 172.16.255.2 netmask ffff0000 broadcast 172.16.255.255
ether 8:0:20:ee:11:51

ifconfig hme0 172.16.3.3 netmask 255.255.255.0

ifconfig –a
lo0: flags=849 mtu 8232
inet 127.0.0.1 netmask ff000000
hme0: flags=863 mtu 1500
inet 172.16.3.3 netmask ffffff00 broadcast 172.16.3.255
ether 8:0:20:ee:11:51

#vi /etc/hosts

2. Change 网卡IP地址 permanence

想要永久更改配置,则要更改配置文件,以网卡hme0为例:

#more /etc/hostname.hme0
Sunland

#more /etc/hosts
127.0.0.1 localhost
172.16.255.2 Sunland loghost

#vi /etc/hosts

#more /etc/hosts
127.0.0.1 localhost
172.16.3.3 Sunland loghost

#more /etc/netmasks
#
# The netmasks file associates Internet Protocol (IP) address # masks with IP network numbers.
#
# network-number netmask
#
# The term network-number refers to a number obtained from the Internet Network
# Information Center. Currently this number is restricted to being a class # A, B, or C network number. In the future we should be able to support # arbitrary network numbers per the Classless Internet Domain Routing # guidelines.
#
# Both the network-number and the netmasks are specified in
# "decimal dot" notation, e.g:
#
# 128.32.0.0 255.255.255.0

#vi /etc/netmasks

#more /etc/netmasks
#
# The netmasks file associates Internet Protocol (IP) address # masks with IP network numbers.
#
# network-number netmask
#
# The term network-number refers to a number obtained from the Internet Network # Information Center. Currently this number is restricted to being a class # A, B, or C network number. In the future we should be able to support # arbitrary network numbers per the Classless Internet Domain Routing # guidelines.
#
# Both the network-number and the netmasks are specified in
# "decimal dot" notation, e.g:
#
# 128.32.0.0 255.255.255.0
172.16.3.0 255.255.255.0

reboot

If/Else

Remember that the spacing is very important in the if statement.
Notice that the termination of the if statement is fi.
You can also replace the "==" with "!=" to test if the variables are NOT equal.

Comparisons:
-eq : equal to
-ne : not equal to
-lt : less than
-le : less than or equal to
-gt : greater than
-ge : greater than or equal to

File Operations:
-s : file exists and is not empty
-f : file exists and is not a directory
-d : directory exists
-x : file is executable
-w : file is writable
-r : file is readable

Script 1:

First, to write age to a file, named "username_DAT".

======================================
#!/bin/sh

# Prompt for a user name...
echo "Please enter your name:"
read USERNAME

# Check for the file.
if [ -s ${USERNAME}_DAT ]; then
# Read the age from the file.
AGE=`cat ${USERNAME}_DAT`
echo "You are $AGE years old!"
else
# Ask the user for his/her age
echo "How old are you?"
read AGE

if [ "$AGE" -le 2 ]; then
echo "You are too young!"
else
if [ "$AGE" -ge 100 ]; then
echo "You are too old!"
else
# Write the age to a new file.
echo $AGE > ${USERNAME}_DAT
fi
fi
fi
======================================

You can test multiple expressions at once by using the (or) operator or the && (and) operator.
The structure of elif is the same as the structure of if

Script 2:

======================================
#!/bin/sh

# Prompt for a user name...
echo "Please enter your age:"
read AGE

if [ "$AGE" -lt 20 ] [ "$AGE" -ge 50 ]; then
echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then
echo "You are in your 20s"
elif [ "$AGE" -ge 30 ] && [ "$AGE" -lt 40 ]; then
echo "You are in your 30s"
elif [ "$AGE" -ge 40 ] && [ "$AGE" -lt 50 ]; then
echo "You are in your 40s"
fi
======================================