情景linux–在脚本中如何优雅地退出telnet

我这里使用的时centos7-mini,centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同,可通过 python
–V 或 python –version 查看系统自带的python版本

  linux下部署php项目环境可以分为两种,一种使用Apache,php,mysql的压缩包安装,一种用yum命令进行安装。

情景

telnet命令是TELNET协议的用户接口,它支持两种模式:命令模式会话模式。虽然telnet支持许多命令,但大部分情况下,我们只是使用它查看目标主机是否打开了某端口(默认是23)

其执行结果有两种:

  • 端口未打开

$ telnet 101.199.97.65 62715

Trying 101.199.97.65...
telnet: connect to address 101.199.97.65: Connection refused

此时,命令已退出。

  • 端口已打开

$ 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
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呢?

有一些系统命令时需要用到python2,不能卸载

使用三种软件的压缩包进行安装,需要手动配置三者之间的关系。apache和php之间的配置没有什么难度,但是和mysql进行配置的时候就需要对php的了解了。

方案

其实可以这样:

  • 输出结果后立即退出

$ 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
  • 输出结果后延迟退出
    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.

使用这种方式可以将标准输出和标准错误重定向到文件中,通过分析文件的内容来判断端口打开状态。

1、安装依赖包

以下是用yum在linux中配置php环境:

1)首先安装gcc编译器,gcc有些系统版本已经默认安装,通过 
gcc –version  查看,没安装的先安装gcc,yum -y install gcc

 

2)安装其它依赖包,(注:不要缺少,否则有可能安装python出错,python3.7.0以下的版本可不装
libffi-devel )

MySql

 

  1.mysql和apache最好是首先进行安装的,因为在配置php的时候需要与mysql和apache进行关联配置和测试

首先下载mysql-sever文件,因为博主linux环境是CentOS版本,yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载

1.下载mysql-service文件

[root@tele-1 ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

2.安装mysql-service文件

[root@tele-1 ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

 

   2.安装mysql

[root@tele-1 ~]# yum install mysql-community-server

 

   3.安装完毕之后启动mysql服务

[root@tele-1 ~]# service mysqld restart

 

  4.
初步安装的mysql是没有密码的,用户名默认是root。所以我们需要修改密码,用mysql命令行进行修改

 1.进入mysql命令行

[root@tele-1 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 474801
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

2.使用命令进行密码修改

mysql> set password for 'root'@'localhost' = password('你要修改的密码');
Query OK, 0 rows affected (0.06 sec)

 

  5.因为博主是用本地navicat软件来连接linux下的mysql的,所以如果要在本地访问的话,就需要改一下mysql数据库中的user表了

1.操作mysql数据库表

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 


2.查看user表中的数据(在mysql命令行中可以直接进行sql语句编写)

mysql> select * from user;
+-----------+------+-----------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| Host      | User | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin                | authentication_string | password_expired |
+-----------+------+-----------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
| %         | root | *3ce8dad2446975d8b0c771bd7e48d15b | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 | mysql_native_password |                       | N                |
| localhost | root | *3ce8dad2446975d8b0c771bd7e48d15b | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 | mysql_native_password |                       | N                |
| 127.0.0.1 | root | *3ce8dad2446975d8b0c771bd7e48d15b | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 | mysql_native_password |                       | N                |
| ::1       | root | *3ce8dad2446975d8b0c771bd7e48d15b | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 | mysql_native_password |                       | N                |
| tele-1    |      |                                           | N           | N           | N           | N           | N           | N         | N           | N             | N            | N         | N          | N               | N          | N          | N            | N          | N                     | N                | N            | N               | N                | N                | N              | N                   | N                  | N                | N          | N            | N                      |          |            |             |              |             0 |           0 |               0 |                    0 | mysql_native_password | NULL                  | N                |
+-----------+------+-----------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-----------------------+------------------+
5 rows in set (0.00 sec)

mysql> 

 

  3.博主的表中是修改之后的表了,想要远程访问,就需要上边红色标注的数据了,Host指的是可以访问此数据库的ip地址,%代表的是所有的请求都可以连接进来。

大家可以修改一条数据,也可以添加一条数据。但是最好不要修改上边蓝色标注的数据,修改语句就是下边的格式

mysql> update user set Host = '%' where ???

 

  4.最后推出exit或者\q都是退出mysql命令行的方法

mysql> \q
Bye

 

 

 

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel

 安装Apache

 

  1.apache安装方法相对简单

[root@tele-2 ~]# yum install httpd

 

  2.外网访问虚拟机中的地址,我们就需要修改一下apache的配置文件/etc/httpd/conf/httpd.conf

 

图片 1

 找到  #ServerName
www.example.com:80            改为  ServerName
localhost:80

   如右图所示:

 

 

 

 

图片 2

 找到  #Listen  
                              改为  Listen:8080(linux中开放的端口号80XX)

 如右图所示:

   

 

 

 

 

 

  

  3.修改完成之后我们需要再次启动httpd服务,并查看启动状态

[root@tele-2 ~]# service httpd start
Redirecting to /bin/systemctl start  httpd.service
[root@tele-2 ~]# service httpd status
Redirecting to /bin/systemctl status  httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2017-06-05 15:57:34 CST; 5s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 54532 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 39046 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 54573 (httpd)
   Status: "Processing requests..."
   Memory: 15.8M
   CGroup: /system.slice/httpd.service
           ├─54573 /usr/sbin/httpd -DFOREGROUND
           ├─54576 /usr/sbin/httpd -DFOREGROUND
           ├─54577 /usr/sbin/httpd -DFOREGROUND
           ├─54578 /usr/sbin/httpd -DFOREGROUND
           ├─54579 /usr/sbin/httpd -DFOREGROUND
           └─54580 /usr/sbin/httpd -DFOREGROUND

Jun 05 15:57:34 tele-2 systemd[1]: Starting The Apache HTTP Server...
Jun 05 15:57:34 tele-2 systemd[1]: Started The Apache HTTP Server.

 

  4.此时你就可以访问你的服务器了,输入localhost或者ip地址,出现一个Apache
test page powered by centos的测试页面

 

2、下载python3.7.0源码,根据需求下载

PHP

1)在

 

  1.php安装命令

[root@tele-2 ~]# yum install php

 

  2.直接一路安装,安装完成之后再次重启httpd服务

[root@tele-2 ~]# service httpd start
Redirecting to /bin/systemctl start  httpd.service

 

  3.重启之后我们进行测试PHP相关信息,我们新建一个PHP界面进行测试

在apache默认页面路径/``var``/www/html下新建一个test.php页面,添加代码

<?php
 phpinfo();
?>

     

图片 3

 

  4.访问这个页面,输入localhost/test.php,或者

ip:端口号/test.php就可以看见php环境的配置信息了。如右图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

图片 4

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注