linux 测试对方地址通不通
1. ping xxxxx
如没返回就不通
2.telnet 172.25.97.101 8088 或 telnet 172.25.97.101
如果返回telnet: command not found 说明未安装
解决方法: 安装telnet服务
先更新系统
yum -y update
centos、ubuntu安装telnet命令的方法.
yum list telnet* 列出telnet相关的安装包 yum install telnet-server 安装telnet服务 yum install telnet.* 安装telnet客户端
[root@localhost ~]# yum list telnet* Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.shu.edu.cn * updates: mirrors.cn99.com Available Packages telnet.x86_64 1:0.17-48.el6 base telnet-server.x86_64 1:0.17-48.el6 base
[root@localhost ~]# yum install telnet-server Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.shu.edu.cn * updates: mirrors.cn99.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package telnet-server.x86_64 1:0.17-48.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: telnet-server x86_64 1:0.17-48.el6 base 37 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 37 k Installed size: 54 k Is this ok [y/N]: y Downloading Packages: telnet-server-0.17-48.el6.x86_64.rpm | 37 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : 1:telnet-server-0.17-48.el6.x86_64 1/1 Verifying : 1:telnet-server-0.17-48.el6.x86_64 1/1 Installed: telnet-server.x86_64 1:0.17-48.el6 Complete! [root@localhost ~]#
[root@localhost ~]# yum install telnet.* Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.shu.edu.cn * updates: mirrors.cn99.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package telnet.x86_64 1:0.17-48.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================== Installing: telnet x86_64 1:0.17-48.el6 base 58 k Transaction Summary ==================================================================================================================================== Install 1 Package(s) Total download size: 58 k Installed size: 109 k Is this ok [y/N]: y Downloading Packages: telnet-0.17-48.el6.x86_64.rpm | 58 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : 1:telnet-0.17-48.el6.x86_64 1/1 Verifying : 1:telnet-0.17-48.el6.x86_64 1/1 Installed: telnet.x86_64 1:0.17-48.el6 Complete! [root@localhost ~]#
测试端口使用telnet IP地址或主机名 端口,使用tcp协议的例如telnet 192.168.0.1 80测试192.168.0.1的80端口
其执行结果有两种:
1. 端口未打开
$ telnet 101.199.97.65 62715
Trying 101.199.97.65...
telnet: connect to address 101.199.97.65: Connection refused
此时,命令已退出。
2. 端口已打开
$ telnet 101.199.97.65 62715
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is '^]'.
此时命令未退出。
根据提示Escape character is ‘]‘.可知退出字符为’]’(CTRL+])。此时输入其它字符不能使其退出,CTRL+C都不行。输入CTRL+]后会自动执行,进入命令模式:
^]
telnet>
此时再运行 quit 才会真正退出。
telnet> quit
Connection closed.
其中,Escape character可以自定义,使用参数-e:
$ telnet -e p 101.199.97.65 62715 #使用p字符
Telnet escape character is 'p'.
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is 'p'.
p
telnet> quit
Connection closed.
即便如此,退出telnet还是麻烦。那么,更进一步,如果出现在脚本中应该如何(优雅地)退出telnet呢?
方案
其实可以这样:
1. 输出结果后立即退出
$ echo "" | telnet 101.199.97.65 62715
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is '^]'.
Connection closed by foreign host. #已成功连通端口并自动退出
$ echo "" | telnet 101.199.97.65 62715
Trying 101.199.97.65...
telnet: connect to address 101.199.97.65: Connection refused #端口未开放
2. 输出结果后延迟退出
sleep 2使得telnet输出结果后,停留2秒后退出命令模式。
$ sleep 2 | telnet 101.199.97.65 62715
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is '^]'.
Connection closed by foreign host.
使用这种方式可以将标准输出和标准错误重定向到文件中,通过分析文件的内容来判断端口打开状态