一、IP查看命令ifconfig: command not found及相关事宜
ifconfig是常用的查看本机的IP的命令 publish:January 17, 2018 -Wednesday ,用于配置GNU/Linux系统的网络接口。它显示网络接口卡的详细信息,包括IP地址,MAC地址,以及网络接口卡状态之类。但在最小化版本的RHEL 7以及它的克隆版本CentOS 7,Oracle Linux 7和Scientific Linux 7中找不到该命令了。我在某台centos5.7上遇到ifconfig: command not found
[root@test ~]$ cat /etc/issue
CentOS release 5.7 (Final)
Kernel \r on an \m
[root@test ~]$ ifconfig
-bash: ifconfig: command not found
这说明服务器没有安装带有ifconfig命令的包,那是什么包提供了ifconfig命令呢?可以使用yum provides或者yum whatprovides来反向查找相关的安装包名称,下面的命令未找到,建立使用*/ifconfig或者*bin/ifconfig
[root@test ~]$ yum provides ifconfig
Loaded plugins: fastestmirror, security
......
Warning: 3.0.x versions of yum would erroneously match against filenames.
You can use "*/ifconfig" and/or "*bin/ifconfig" to get that behaviour
No Matches found
#下面试试whatprovides命令
[root@test ~]$ yum whatprovides ifconfig
Loaded plugins: fastestmirror, security
......
Warning: 3.0.x versions of yum would erroneously match against filenames.
You can use "*/ifconfig" and/or "*bin/ifconfig" to get that behaviour
No Matches found
#这是在一台装了ifconfig上面执行的,然后试试发现能找到net-tools工具包名,未安装下好像也不能找到。。
[root@test ~]$ yum whatprovides "*/ifconfig"
Loaded plugins: fastestmirror, security
......
net-tools-1.60-114.el6.x86_64 : Basic networking tools
Repo : base
Matched from:
Filename : /sbin/ifconfig
net-tools-1.60-110.el6_2.x86_64 : Basic networking tools
Repo : installed
Matched from:
Filename : /sbin/ifconfig
net-tools包提供了ifconfig命令。所以需要安装net-tools包来使用ifconfig命令。执行:
yum install -y net-tools
上面的系统都是5.7,yum也不更新了,未再处理。对于未安装ifconfig命令的情况如果要查看本机IP,可直接查看ip的配置文件,示例如下:
/etc/sysconfig/network-scripts/ifcfg-eth0 #eth0一般配置外网IP地址
/etc/sysconfig/network-scripts/ifcfg-eth1 #eth1配置内网IP地址
/etc/sysconfig/network-scripts/ifcfg-eth0:1 #还可以配置多个外网IP地址
#命令执行如下:
[root@test yum.repos.d]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=118.181.168.12
NETMASK=255.255.255.0
#GATEWAY=118.181.168.1
[root@test yum.repos.d]$ cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=192.168.168.12
NETMASK=255.255.0.0
[root@test yum.repos.d]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
ONBOOT=yes
IPADDR=183.44.30.12
NETMASK=255.255.255.0
GATEWAY=183.44.30.1
后期的趋势是不再推荐使用ifconfig命令。CentOS 7最小化系统,推荐使用 ip addr(ip address) 和 ip link 命令来查找网卡详情。要查看统计数据,可以使用ip -s link
二、vim编辑或者通过sed提取一行里的某个字符串插入到行的最前面-后向引用
有以下非常多行的代码,需要同时编辑,在经过替换后示例几行代码如下 publish:January 19, 2018 -Friday:
有以下非常多行的代码,需要同时编辑,在经过替换后示例几行代码如下:
while 1:
body.get('table', None)
body.get('switch', None)
body.get('uid', None)
body.get('ver', None)
body.get('type', None)
body.get('classnema', None)
body.get('cid', None)
body.get('public_time', None)
break
print('ok')
现有需求需要批量将单引号里的内容提出来放至最前面最后形成一个赋值语句,最终想要实现的目标如下:
while 1:
table = body.get('table',None)
switch = body.get('switch',None)
uid = body.get('uid',None)
ver = body.get('ver',None)
type = body.get('type',None)
classnema = body.get('classnema',None)
cid = body.get('cid',None)
public_time = body.get('public_time',None)
print('ok')
之前也用过vim或者sed里面的后向引用,今天正好拿这个再练练手。不过还是费了些时间,vim进入文件执行命令如下(注50,57是中间涉及body所在的行):
:50,57s/.*'\(.*\)'.*/\1=\0/
在尝试的时候,参考awk,猜测\0代表原始行内容,测试果然如此。另外(需要使用反斜线,这样操作之后还有一个问题就是新加的内容离body.get有些远,结果如下:
table= body.get('table', None)
switch= body.get('switch', None)
uid= body.get('uid', None)
ver= body.get('ver', None)
type= body.get('type', None)
classnema= body.get('classnema', None)
cid= body.get('cid', None)
public_time= body.get('public_time', None)
再使用如下命令替换空格并通过=号替换在前后加上一个空格,:
:50,57s/ //g
:50,57s/=/ = /g
将空格替换,这里也没有去尝试更精进的方法,直接一步到位的。只要能实现即可。所可以直接在文件外使用sed编辑内容。
sed -i "50,57s/.*'\(.*\)'.*/\1=\0/g" test.py
三、全国拼音相同的城市(地级市)明细列表及所属省份
之前还在想这么一个事,publish:July 23, 2018 -Monday 单独想吧,真的很难想出来哪些城市的拼音会相同,今天在整理数据的时候处理了一下,发现全国还真有这28个城市的拼音是相同的。列表如下:
计数 | 省份 | 省辖城市 | 城市拼音 |
1 | 江西省 | 丰城 | fengcheng |
2 | 辽宁省 | 凤城 | fengcheng |
3 | 安徽省 | 阜阳 | fuyang |
4 | 浙江省 | 富阳 | fuyang |
5 | 广东省 | 鹤山 | heshan |
6 | 广西省 | 合山 | heshan |
7 | 吉林省 | 集安 | jian |
8 | 江西省 | 吉安 | jian |
9 | 福建省 | 建阳 | jianyang |
10 | 四川省 | 简阳 | jianyang |
11 | 内蒙 | 集宁 | jining |
12 | 山东省 | 济宁 | jining |
13 | 河北省 | 晋州 | jinzhou |
14 | 辽宁省 | 锦州 | jinzhou |
15 | 辽宁省 | 开原 | kaiyuan |
16 | 云南省 | 开远 | kaiyuan |
17 | 广西省 | 赁祥 | linxiang |
18 | 湖南省 | 临湘 | linxiang |
19 | 安徽省 | 宿州 | suzhou |
20 | 江苏省 | 苏州 | suzhou |
21 | 江苏省 | 泰州 | taizhou |
22 | 浙江省 | 台州 | taizhou |
23 | 河南省 | 舞钢 | wugang |
24 | 湖南省 | 武冈 | wugang |
25 | 黑龙江省 | 伊春 | yichun |
26 | 江西省 | 宜春 | yichun |
27 | 广西省 | 玉林 | yulin |
28 | 陕西省 | 榆林 | yulin |