Chinaunix
标题:
如何把主机名替换为IP
[打印本页]
作者:
caixicheng
时间:
2013-06-17 15:06
标题:
如何把主机名替换为IP
who -m | awk '{print $5}' | sed -e 's/(//g' -e 's/)//g' 得到了 test:0.0 ,我想把test替换为IP,大伙帮帮忙。
作者:
Shell_HAT
时间:
2013-06-17 15:11
请把who -m的结果贴出来看看
作者:
caixicheng
时间:
2013-06-17 15:17
回复
2#
Shell_HAT
test pts/0 2013-06-17 09:19 (test:0.0)
作者:
关阴月飞
时间:
2013-06-17 15:48
本帖最后由 关阴月飞 于 2013-06-17 15:48 编辑
回复
1#
caixicheng
目测一下:
who -m | awk -F'[ )(]+' '{sub(/test/,"IP",$5);print $5}'
复制代码
作者:
Shell_HAT
时间:
2013-06-17 15:50
回复
3#
caixicheng
ping -c 1 $(who -m | awk -F'[()]+' '{print $2}') | awk -F'[()]+' 'NR==1{print $2}'
复制代码
作者:
caixicheng
时间:
2013-06-17 15:57
回复
4#
关阴月飞
要通用性,我是写到各用户.cshrc文件,每个用户都不一样的主机和IP
作者:
caixicheng
时间:
2013-06-17 15:58
回复
5#
Shell_HAT
Illlegal variable name
作者:
Shell_HAT
时间:
2013-06-17 16:20
回复
7#
caixicheng
什么操作系统?
ping -c 1 `who -m | awk '{print $5}' | sed -e 's/(//g' -e 's/)//g'` | sed -n '1s/[^(]*(\([0-9.]\+\)).*/\1/p'
复制代码
作者:
dn833
时间:
2013-06-17 16:34
谁知道LZ能who出多少种结果,谁就能第一个猜到正确答案
作者:
seesea2517
时间:
2013-06-17 16:38
我的who -m括号里怎么没有test
作者:
caixicheng
时间:
2013-06-17 16:45
回复
8#
Shell_HAT
centos5.5
ping: unkown host test:0.0
作者:
caixicheng
时间:
2013-06-17 16:46
回复
10#
seesea2517
test是hostname
作者:
iwojima
时间:
2013-06-17 17:49
同上:wink:
作者:
seesea2517
时间:
2013-06-17 17:57
回复
12#
caixicheng
这样啊,我的怎么括号里直接是显示IP,只有IP,没有hostname……
作者:
yinwei2012
时间:
2013-06-17 18:46
who -m | awk -F"[()]" '{print $2}'
按照lz和我的系统情况 这就可以了
作者:
代号:军刀
时间:
2013-06-17 19:08
楼主的操作系统是自己定制的么?我使用who -m括号也是只有ip地址,没有域名的
作者:
Shell_HAT
时间:
2013-06-17 20:46
回复
11#
caixicheng
ping -c 1 `who -m | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'` | sed -n '1s/[^(]*(\([0-9.]\+\)).*/\1/p'
复制代码
作者:
永夜之魂
时间:
2013-06-17 23:43
centos 5.2 5.3 6.4的都试过了,手头没有5.5,LZ的方案抽出的就是IP·····
作者:
caixicheng
时间:
2013-06-18 08:50
回复
16#
代号:军刀
root是IP,普通用户试试。。。。。
作者:
caixicheng
时间:
2013-06-18 08:59
回复
15#
yinwei2012
不要用root执行看看
作者:
caixicheng
时间:
2013-06-18 09:02
回复
17#
Shell_HAT
不好意思,尝试还是 unkown host caixc:0.0
作者:
caixicheng
时间:
2013-06-18 09:04
回复
14#
seesea2517
root显示IP
作者:
Shell_HAT
时间:
2013-06-18 10:21
回复
21#
caixicheng
思路你能看明白吗?
就是把括号里面的机器名取出来,ping一下,然后在ping的结果里面取出ip。
你自己尝试一下吧,实在搞不定的话,我可以继续帮你写。
作者:
caixicheng
时间:
2013-06-18 10:35
回复
23#
Shell_HAT
大致知道一点,只知道最基础的,还是麻烦你写一下吧,谢谢!
作者:
Shell_HAT
时间:
2013-06-18 10:49
回复
24#
caixicheng
第一步,先把括号里面的机器名取出来,你把这两个命令的结果贴出来看看:
echo "test pts/0 2013-06-17 09:19 (test:0.0)" | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'
复制代码
who -m | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'
复制代码
作者:
caixicheng
时间:
2013-06-18 11:14
回复
25#
Shell_HAT
执行1 返回 test
执行2返回 test
作者:
Shell_HAT
时间:
2013-06-18 11:32
回复
26#
caixicheng
第二步,ping获取到的机器名:
ping -c 1 `who -m | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'`
复制代码
作者:
caixicheng
时间:
2013-06-18 13:11
回复
27#
Shell_HAT
结果是:ping test (10.254.92.17) 56(84) bytes of data.
64 bytes from test (10.254.92.17):icmp_seq=1 ttl=127 time=0.275ms
作者:
Shell_HAT
时间:
2013-06-18 13:19
回复
28#
caixicheng
第三步,从ping的结果中取出ip:
ping -c 1 `who -m | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'` | sed -n '1s/[^(]*(\([0-9.]\+\)).*/\1/p'
复制代码
作者:
caixicheng
时间:
2013-06-18 13:47
回复
29#
Shell_HAT
执行结果 :10.254.92.17 *who -m返回的是test:0.0
作者:
Shell_HAT
时间:
2013-06-18 14:01
回复
30#
caixicheng
17楼和29楼的代码不是一样吗,为什么之前报错现在正常了?
strWho=`who -m`
strDisplay=`echo "$strWho" | sed -e 's/.*://' -e 's/)//'`
ping -c 1 `echo "$strWho" | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'` | sed -n "1s/[^(]*(\([0-9.]\+\)).*/\1$strDisplay/p"
复制代码
作者:
caixicheng
时间:
2013-06-18 16:03
回复
31#
Shell_HAT
不好意思,不是直接复制过去执行,手动输入,所以出错了
谢谢你耐心帮助,执行结果ok,strDisplay=`echo "$strWho" | sed -e 's/.*://' -e 's/)//'` 这个结果是0.0,能在前面加入“:”冒号吗?我自己尝试在第二行加入,没办法实现。
作者:
Shell_HAT
时间:
2013-06-18 16:08
回复
32#
caixicheng
strDisplay=`echo "$strWho" | sed -e 's/.*:/:/' -e 's/)//'`
复制代码
作者:
caixicheng
时间:
2013-06-18 16:25
回复
33#
Shell_HAT
谢谢你,执行OK了,我的项目可以进行了,非常感谢!!!!
作者:
caixicheng
时间:
2013-06-18 16:47
回复
33#
Shell_HAT
你好,第三句放入.cshrc文件,setenv strVnc ping -c 1 `echo "$strWho" | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'` | sed -n "1s/[^(]*(\([0-9.]\+\)).*/\1$strDisplay/p"
source ,提示too many arguments。
作者:
Shell_HAT
时间:
2013-06-18 16:50
回复
35#
caixicheng
请把.cshrc的完整内容贴出来看看,我测试一下。
作者:
caixicheng
时间:
2013-06-18 16:55
回复
36#
Shell_HAT
setenv strWho `who -m`
setenv strDisplay `echo "$strWho" | sed -e 's/.*:/:/' -e 's/)//'`
setenv strVnc `ping -c 1 `echo "$strWho" | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'` | sed -n "1s/[^(]*(\([0-9.]\+\)).*/\1$strDisplay/p"`
作者:
Shell_HAT
时间:
2013-06-18 17:11
回复
37#
caixicheng
setenv strWho `who -m`
setenv strDisplay `echo "$strWho" | sed -e 's/.*(.*:/:/' -e 's/)//'`
setenv strIp `echo "$strWho" | awk '{print $5}' | sed -e 's/(//g' -e 's/:.*//g'`
setenv strVnc `ping -c 1 $strIp | sed -n "1s/[^(]*(\([0-9.]\+\)).*/\1$strDisplay/p"`
复制代码
作者:
caixicheng
时间:
2013-06-18 17:32
回复
38#
Shell_HAT
OK,TKS!
VNC好像DISPLAY使用不同于xmanager,第三句修改为 setenv strIp `echo $HOSTNAME`
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2