LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 944|回复: 7

求脚本

[复制链接]
发表于 2006-1-5 16:00:02 | 显示全部楼层 |阅读模式
我在使用Mercury这个软件,需要把

83.82.146.62 dstorm.mine.nu


这一行加入到/etc/hosts文件里。

但是这个IP地址是会变的,当Mercury不能连接的时候,需要Ping一下dstorm.mine.nu,得到IP后再把这个IP替换/etc/hosts文件里的IP。

我想要一个脚本,只要运行了就可以Ping  dstorm.mine.nu,得到IP后写入/etc/hosts替换原来的IP。
发表于 2006-1-5 17:09:27 | 显示全部楼层

  1. #!/bin/bash
  2. host="dstorm.mine.nu"
  3. ip=`nslookup $host|tail -n 2|awk -F" " '{print $2}'`
  4. sed -i '/$dstorm.mine.nu/d' /etc/hosts
  5. echo "$ip       $host">>/etc/hosts
复制代码

不知道行不行,不大会用sed
这个IP变的也太快了?
回复 支持 反对

使用道具 举报

发表于 2006-1-5 18:17:24 | 显示全部楼层

  1. #!/bin/bash
  2. ip=`ping -c 1 dstorm.mine.nu | grep 'from' | sed 's/^.*(\(.*\)):.*/\1/' `
  3. sed -i "s/^.*dstorm\.mine\.nu.*/$ip dstorm\.mine\.nu/" /etc/hosts
复制代码

第一行获取IP,第二行替换IP。
回复 支持 反对

使用道具 举报

发表于 2006-1-6 00:46:06 | 显示全部楼层

  1. ping -c1 dstorm.mine.nu|awk 'NR==1{sub(/\(/,"");sub(/)/,"");print $3"\t"$2}' >>/etc/hosts
复制代码

BTW:as root,of course ^_^
回复 支持 反对

使用道具 举报

发表于 2006-1-6 08:16:08 | 显示全部楼层
天龙盖地虎啊!向二位学习
回复 支持 反对

使用道具 举报

发表于 2006-1-6 10:35:22 | 显示全部楼层
Post by 寂寞烈火

  1. ping -c1 dstorm.mine.nu|awk 'NR==1{sub(/\(/,"");sub(/)/,"");print $3"\t"$2}' >>/etc/hosts
复制代码

BTW:as root,of course ^_^


大侠,你的代码无法运行:

  1. #ping -c1 dstorm.mine.nu|awk 'NR==1{sub(/\(/,"");sub(/)/,"");print $3"\t"$2}'
  2. awk: line 1: regular expression compile failed (missing '(')
  3. )
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-6 11:29:18 | 显示全部楼层
感谢各位热心的回复
回复 支持 反对

使用道具 举报

发表于 2006-1-7 00:26:44 | 显示全部楼层
Post by ideawu
大侠,你的代码无法运行:

  1. #ping -c1 dstorm.mine.nu|awk 'NR==1{sub(/\(/,"");sub(/)/,"");print $3"\t"$2}'
  2. awk: line 1: regular expression compile failed (missing '(')
  3. )
复制代码

或许是awk版本差异造成的吧
GNU Awk 3.1.1
看我的输出
/home/lee#ping -c1 dstorm.mine.nu|awk 'NR==1{sub(/\(/,"");sub(/)/,"");print $3"\t"$2}'
65.160.219.113  dstorm.mine.nu
/home/lee#ping -c1 dstorm.mine.nu|awk 'NR==1{sub(/\(/,"");sub(/)/,"");print $3"\t"$2}'
216.221.188.182 dstorm.mine.nu
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表