L72.linux命令每日一练 -- 第十章 Linux网络管理命令 -- mail和nslookup

10.17 mail:发送和接收邮件

10.17.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​  mail命令是命令行的电子邮件发送和接收的工具。mail命令是个软连接,真实的程序文件是mailx:

#这是CentOS 7的文件
[root@centos7 ~]# which mail
/usr/bin/mail
[root@centos7 ~]# ll /usr/bin/mail
lrwxrwxrwx. 1 root root 5 Oct 20 16:15 /usr/bin/mail -> mailx
[root@centos7 ~]# which mailx
/usr/bin/mailx
[root@centos7 ~]# ll -h /usr/bin/mailx
-rwxr-xr-x. 1 root root 384K Apr 11  2018 /usr/bin/mailx

#这是CentOS 6的文件
[root@centos6 ~]# which mail
/bin/mail
[root@centos6 ~]# ll /bin/mail
lrwxrwxrwx. 1 root root 22 Oct  9 04:01 /bin/mail -> /etc/alternatives/mail
[root@centos6 ~]# ll /etc/alternatives/mail
lrwxrwxrwx. 1 root root 10 Oct  9 04:01 /etc/alternatives/mail -> /bin/mailx
[root@centos6 ~]# ll -h /bin/mailx
-rwxr-xr-x. 1 root root 382K Oct  9  2018 /bin/mailx

【语法格式】

mail [option]
mail [选项]

说明:

​ 1)在mail命令及后面的选项里,每个元素直接都至少要有一个空格。

​ 2)原生的mail命令发送邮件时必须要有邮件服务支持,否则会发送不成功,具体说明见mailq范例1。

【选项说明】

​ 表10-17针对该命令的参数选项进行了说明。

​ 表10-17 mail命令的参数选项及说明
在这里插入图片描述

10.17.2 使用范例

10.17.2.1 基础范例

【环境准备】

​ 先检查邮件服务是否开启,命令如下:

[root@centos7 ~]# systemctl status postfix 
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-10-31 13:48:27 CST; 29min ago	#running表示正在运行。
  Process: 1403 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 1400 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 1391 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 1483 (master)
   CGroup: /system.slice/postfix.service
           ├─1483 /usr/libexec/postfix/master -w
           ├─1490 pickup -l -t unix -u
           └─1491 qmgr -l -t unix -u

Oct 31 13:48:23 centos7 systemd[1]: Starting Postfix Mail Transp....
Oct 31 13:48:27 centos7 postfix/postfix-script[1480]: starting th...
Oct 31 13:48:27 centos7 postfix/master[1483]: daemon started -- v...
Oct 31 13:48:27 centos7 systemd[1]: Started Postfix Mail Transpo....
Hint: Some lines were ellipsized, use -l to show in full.

[root@centos6 ~]# /etc/init.d/postfix status
master (pid  1663) is running...

​ **范例10-59:**交互式发送电子邮件。

【语法格式】

mail -s 邮件主题 收件人1 收件人2 ...
[root@centos7 ~]# mail -s "Hello from neteagle" [email protected]	#收件人请读者自行改成自己的邮箱。
hello,this is the content of mail.	#手动输入两行邮件内容。
welcome to www.neteagles.cn
EOT		#在新的空行输入Ctrl+d表示结束输入,发送邮件。

​ **范例10-60:**使用管道传入内容并发送电子邮件。

[root@centos7 ~]# echo -e "hello,this is the content of mail.\nwelcome to www.neteagles.cn" |mail -s "Hello from neteagle" [email protected]
#命令说明:echo的后面是邮件正文。

​ **范例10-61:**使用文件发送电子邮件。

[root@centos7 ~]# mail -s "Hello from neteagle" [email protected] <anaconda-ks.cfg 	#使用输入重定向将文件的内容传给mail命令。

​ 收信人邮箱截图如图10-3所示。

**范例10-62:**管理邮件的例子。

[root@centos7 ~]# mailq
Mail queue is empty
You have mail in /var/spool/mail/root	#当我们看到这样的语句时,就说明我们的邮箱有邮件了。Linux系统将收到的邮件存放在/var/spool/mail/目录下。不同用户的邮件保存在以用户名命名的文件中。例如,root用户的邮件将保存在文件/var/spool/mail/root中。
[root@centos7 ~]# mail	#使用mail命令就能管理邮件。
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 Mail Delivery System  Sat Oct 31 14:35 131/4221  "Undeliver"
& 1	#在&(mail命令的提示符)后输入邮件ID,就能阅读邮件的完整内容,这是一封往QQ邮箱发邮件但没有发送成功的退信内容。
Message  1:
From MAILER-DAEMON  Sat Oct 31 14:35:06 2020
Return-Path: <>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Sat, 31 Oct 2020 14:35:06 +0800 (CST)
From: [email protected] (Mail Delivery System)
Subject: Undelivered Mail Returned to Sender
To: [email protected]
Auto-Submitted: auto-replied
Content-Type: multipart/report; report-type=delivery-status;
	boundary="CAA2D3112ED4.1604126106/centos7.localdomain"
Status: R

Part 1:
Content-Description: Notification
Content-Type: text/plain; charset=us-ascii

This is the mail system at host centos7.localdomain.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

& d 1	#删除邮件:d 邮件ID。
& q		#退出。

10.17.2.2 生产案例

​ **范例10-63:**使用第三方邮箱发送邮件。

​ mail命令会默认使用本地postfix(sendmail)发送邮件,这就要求本地的机器必须安装和启动相关服务,这样不仅配置非常麻烦,而且还会带来不必要的资源占用。还有一个问题,很多时候,所发送的邮件会被视为垃圾邮件。

​ 修改配置文件/etc/mail.rc之后就可以使用外部邮件服务器了,比如QQ邮箱、163邮箱,但是因为邮件服务商频繁出现密码泄露的事件,因此对这些个人邮箱设置了一些安全措施,比如腾讯企业邮、163企业邮等。

​ 以下是使用第三方邮件发送邮件的方法,先修改/etc/mail.rc,在文件的最后加入一行内容,如下:

[root@centos7 ~]# vim .mailrc 
set from=19661891@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=19661891@qq.com
set smtp-auth-password=hrlnpctmxkpqbjdd
set smtp-auth=login
set ssl-verify=ignore
:wq

​ 上面的配置文件给出了两种方法,大家选择一种就可以了。其中参数的说明具体如下。

  • from是发送邮件的地址。
  • smtp是发信的外部smtp服务器的地址。
  • smtp-auth-user是外部smtp服务器认证的用户名。
  • smtp-auth-password是外部smtp服务器认证的用户密码。
  • smtp-auth是邮件认证的方式。
  • ssl-verify是ssl认证方式。

​ 配置成功之后,就可以使用了,可以发送一封邮件测试一些:

[root@centos7 ~]# echo "test" |mail -s "test" [email protected]

​ 查看收信人邮箱邮件(如图10-4所示)。注意观察发件人邮箱地址。

在这里插入图片描述
​ 图10-4 收信人邮箱截图

​ **注意:**使用这种方法不需要开启postfix服务,它们是两种方法。

10.18 nslookup:域名查询工具

10.18.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​  nslookup命令是常用的域名解析查询工具。

​ 如果系统没有nslookup命令,则需要安装下面的软件包:

[root@centos7 ~]# yum provides nslookup
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base/7/x86_64/filelists_db                   | 7.1 MB     00:31     
epel/x86_64/filelists_db                     |  12 MB     00:41     
extras/7/x86_64/filelists_db                 | 217 kB     00:00     
updates/7/x86_64/filelists_db                | 2.4 MB     00:00     
32:bind-utils-9.11.4-16.P2.el7.x86_64 : Utilities for querying DNS
                                      : name servers
Repo        : base
Matched from:
Filename    : /usr/bin/nslookup



32:bind-utils-9.11.4-16.P2.el7_8.2.x86_64 : Utilities for querying
                                          : DNS name servers
Repo        : updates
Matched from:
Filename    : /usr/bin/nslookup



32:bind-utils-9.11.4-16.P2.el7_8.3.x86_64 : Utilities for querying
                                          : DNS name servers
Repo        : updates
Matched from:
Filename    : /usr/bin/nslookup



32:bind-utils-9.11.4-16.P2.el7_8.6.x86_64 : Utilities for querying
                                          : DNS name servers
Repo        : updates
Matched from:
Filename    : /usr/bin/nslookup
[root@centos7 ~]# yum install -y bind-utils

【语法格式】

nslookup [option] [name] [server]
nslookup [选项] [域名/IP] [DNS服务器]

​ **说明:**在nslookup命令及后面的选项里,每个元素直接都至少要有一个空格。

【选项说明】

​ nslookup有如下两种模式。

  • 交互模式:用户可以向域名服务器查询各类主机、域名的信息,或者输出域名中的主机列表。
  • 非交互模式:针对一个主机或域名仅仅获取特定的名称或所需的信息。

​ 先来看看如何进入交互模式。

​ 直接输入nslookup命令,若不加任何参数,则会直接进入交互模式,此时nslookup会连接到默认的域名服务器(即/etc/resolv.conf的第一个DNS地址)。

​ 交互式也支持选定不同的域名服务器。只需要将第一个参数设置为“-”,然后第二个参数是设置要连接的域名服务器主机名或IP地址。

​ 表10-18针对该命令的参数选项进行了说明。

​ 表10-18 交互模式下nslookup命令的参数选项及说明

在这里插入图片描述
​ 对于非交互模式,可采用以下方式进入。

​ 直接在nslookup命令后加上所要查询的IP或主机名,即可进入非交互式模式,也可以在第二个参数位置设置所要连接的域名服务器。

​ 表10-19针对该命令的参数选项进行了说明。

​ 表10-19 非交互模式下nslookup命令的参数选项及说明
在这里插入图片描述

10.18.2 使用范例

​ **范例10-65:**交互模式。

[root@centos7 ~]# cat /etc/resolv.conf 	#默认是从该文件读取DNS服务器地址的。
# Generated by NetworkManager
nameserver 223.5.5.5

[root@centos7 ~]# nslookup
> www.qq.com	#符号“>”是nslookup命令的提示符。可以在此提示符下输入要查询的域名信息进行查询。
Server:		223.5.5.5	#默认DNS服务器。
Address:	223.5.5.5#53	#上面的DNS服务器的IP地址与端口号。

Non-authoritative answer:	#非授权域名服务器的应答,说明本域名服务器给出的域名解析信息是从其他域名服务器那里查询所得到的信息,而非自己管理的域。
www.qq.com	canonical name = https.qq.com.
Name:	https.qq.com
Address: 123.151.137.18	#显示域名对应的IP地址。
Name:	https.qq.com
Address: 2402:4e00:8030:1::7d

​ 指定解析域名的服务器地址,这个是阿里云的公共DNS服务器:

> server 223.6.6.6	#指定解析域名服务器地址,这个是阿里云的公共DNS服务器。
Default server: 223.6.6.6
Address: 223.6.6.6#53

​ 输入待解析的域名:

> www.qq.com	#输入解析的域名。
Server:		223.6.6.6
Address:	223.6.6.6#53

Non-authoritative answer:
www.qq.com	canonical name = https.qq.com.
Name:	https.qq.com
Address: 123.151.137.18
Name:	https.qq.com
Address: 2402:4e00:8030:1::7d

​ 查询域名有关的所有信息:

> set type=ANY	#查询域名有关的所有信息。
> www.baidu.com
Server:		223.6.6.6
Address:	223.6.6.6#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.

Authoritative answers can be found from:

​ 查询邮件MX记录:

> set type=MX	#查询邮件转发器。
> www.qq.com
Server:		223.6.6.6
Address:	223.6.6.6#53

Non-authoritative answer:
www.qq.com	canonical name = https.qq.com.

Authoritative answers can be found from:
https.qq.com
	origin = ns-tel1.qq.com
	mail addr = webmaster.qq.com
	serial = 1597839089
	refresh = 300
	retry = 600
	expire = 86400
	minimum = 86400
> exit	#退出。

​ 可以直接在命令指定解析域名的服务器地址,但是要注意写法,不要少了“-”。

[root@centos7 ~]# nslookup - 61.134.1.5	#可以直接在命令指定解析域名的服务器地址,但是要注意写法,不要少了“-”。
> www.jd.com
Server:		61.134.1.5
Address:	61.134.1.5#53

Non-authoritative answer:
www.jd.com	canonical name = www.jd.com.gslb.qianxun.com.
www.jd.com.gslb.qianxun.com	canonical name = cloud.jdcdn.com.
cloud.jdcdn.com	canonical name = img2x-v6-sched.jcloudedge.com.
Name:	img2x-v6-sched.jcloudedge.com
Address: 117.34.35.3
Name:	img2x-v6-sched.jcloudedge.com
Address: 240e:bf:c800:601:8000::3
> exit

​ **范例10-66:**非交互模式的例子。

​ 采用非交互式模式,指定域名服务器地址,查询www.aliyun.com对应的域名记录:

[root@centos7 ~]# nslookup www.aliyun.com 61.134.1.5	#非交互查询www.aliyun.com域名。
Server:		61.134.1.5
Address:	61.134.1.5#53

Non-authoritative answer:
www.aliyun.com	canonical name = www-jp-de-intl-adns.aliyun.com.
www-jp-de-intl-adns.aliyun.com	canonical name = www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com.
www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com	canonical name = sh.wagbridge.aliyun.aliyun.com.
sh.wagbridge.aliyun.aliyun.com	canonical name = aliyun-adns.aliyun.com.
aliyun-adns.aliyun.com	canonical name = aliyun-adns.aliyun.com.gds.alibabadns.com.
Name:	aliyun-adns.aliyun.com.gds.alibabadns.com
Address: 140.205.135.3	#这里就是域名对应的IP。
Name:	aliyun-adns.aliyun.com.gds.alibabadns.com
Address: 2401:b180:1:50::f
Name:	aliyun-adns.aliyun.com.gds.alibabadns.com
Address: 2401:b180:1:60::6

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/126203060