Rog*_*ger 1 ip bash command-line-interface
有没有办法只回显来自Bash的服务器ip地址?我在Linux/Debian6上.
我的意图是为我在服务器中注册的每个新域写入"/ etc/hosts"对domain_name IP.
由于这个bash脚本将打开供公众使用,我试图找到一种通过cli获取此信息的方法.
结论:
这是最终的代码,基于我的朋友帮助我的线索:
newhost() {
DMN=$1
X=`ifconfig | grep Bcast`
Y=`echo "${X#*:}"`
DNS=`echo "${Y%\ B*}"` # server DNS(207.112.37.222)
H='/etc/hosts' #hostfile
PAIR="$DNS\t$DMN"
if grep -i --silent "$DMN" "$H"; then
echo -e "$DMN already exists in $H"
else
bash <<EOF
echo -e "$PAIR" >> "$H"
EOF
echo -e "$PAIR added to $H"
fi
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,一些天生就已经聪明的家伙投票结束这个问题,这不是很有趣吗?
谢谢.
使用:
ifconfig- 你的ip将inet addr:在这种情况下1.1.1.1
样本输出:
root@server [~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0A:E4:89:0B:97
inet addr:1.1.1.1 Bcast:1.1.1.63 Mask:255.255.255.192
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1/or modify
RX packets:14804377317 errors:0 dropped:0 overruns:0 frame:0
TX packets:11766937374 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13799286299902 (12.5 TiB) TX bytes:4891709752100 (4.4 TiB)
Interrupt:169 Memory:fa000000-fa012800 will be useful,
Run Code Online (Sandbox Code Playgroud)
http://www.debianadmin.com/network-interface-configuration-using-ifconfig.html