分页: 1 / 1

谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 8:09
nameaj
思路:每60秒通过 http://www.ip.cn/ 这个网站查询本机ip 如果ip改变就执行一段命令 程序要循环运行

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 8:23
麦斯特
我的想法是先弄个循环,sleep 60然后用sed获取ifconfig那几个关键词的输出,跟上一次获得的结果进行比照,假如结果不同就进行处理,想也不是很难。

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 8:25
nameaj
麦斯特 写了:我的想法是先弄个循环,sleep 60然后用sed获取ifconfig那几个关键词的输出,跟上一次获得的结果进行比照,假如结果不同就进行处理,想也不是很难。
帮我写一下吧 我是小白不懂
你看下这个帖子 最好从网站获取ip http://icanhazip.com/ 用这个网站获取ip容易点

http://forum.ubuntu.org.cn/viewtopic.php?f=21&t=374618

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 8:32
麦斯特
现在的环境不方便……

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 9:04
nameaj
麦斯特 写了:现在的环境不方便……
有时间帮我写下 好不

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 9:08
jtshs256
curl ifconfig.me

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 9:10
jtshs256
哦,还要刷着,那就watch一下…

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 9:18
麦斯特
记得之前YeLee写过一个网路监视的脚本,你去搜一下吧。

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 9:32
nameaj
jtshs256 写了:哦,还要刷着,那就watch一下…
能帮我写下吗
帮我用这个改一下吧 http://p.vim-cn.com/cGW/python

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 12:06
枫叶饭团

代码: 全选

^_^[maplebeats@acer-5750G:~/Works/test]$ python3 ip.py 
123.145.97.29
123.145.97.29
123.145.97.29
^CTraceback (most recent call last):
  File "ip.py", line 9, in <module>
    time.sleep(2)
KeyboardInterrupt
QAQ[maplebeats@acer-5750G:~/Works/test]$ vi ip.py 
^_^[maplebeats@acer-5750G:~/Works/test]$ cat ip.py 
#/usr/bin/env python3
import urllib.request as ur
import re,time
while True:
    fp = ur.urlopen("http://icanhazip.com")
    ip=re.compile(r'[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')
    text = fp.read().decode("utf8")  
    print(ip.findall(text)[0])
    time.sleep(60)
^_^[maplebeats@acer-5750G:~/Works/test]$ 

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 12:07
枫叶饭团
写了个很搞笑的正则 :em05 :em05

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 12:26
枫叶饭团
我错了,还要判断啊

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 12:41
枫叶饭团
[python]
#/usr/bin/env python3
import urllib.request as ur
import re,time
def getip():
fp =ur.urlopen("http://icanhazip.com")
ip = re.compile(r'[\d\.]*')
return ip.findall(fp.read().decode("utf8"))[0]
temp = getip()
while True:
myip = getip()
if myip != temp:
print(myip) #do
temp = getip()
print(myip)
time.sleep(60)
[/python]

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 13:05
YeLee
楼上给力啊,菜鸟学习了。 :em11 :em11 :em11

Re: 谁能用shell脚本帮我写段代码 谢谢

发表于 : 2012-05-20 13:23
ptpt52

代码: 全选

#!/bin/bash

myip=`wget http://www.ip138.com/ip2city.asp -O - 2>/dev/null | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'`

while :; do
    sleep 60
    newip=`wget http://www.ip138.com/ip2city.asp -O - 2>/dev/null | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'`
    [ ! "x$myip" = "x$newip" ] && {
        #do your jop
        myip=$newip
    }
done
如上