写一个shell脚本执行telnet 127.0.0.1 12362611511311351如果输出内容有bad port则判断telnet服务正常
时间: 2024-03-27 15:41:30 浏览: 24
好的,下面是一个简单的shell脚本,可以执行telnet命令,并判断输出内容是否包含"bad port",以此来判断telnet服务是否正常:
```
#!/bin/bash
output=$(echo "quit" | telnet 127.0.0.1 12362611511311351 2>&1)
if [[ $output == *"bad port"* ]]; then
echo "telnet服务正常"
else
echo "telnet服务异常"
fi
```
脚本的执行流程如下:
1. 使用echo命令发送"quit"字符串到127.0.0.1的12362611511311351端口,这相当于使用telnet连接到指定端口并立即退出。
2. 将telnet命令的输出重定向到变量$output中。
3. 使用字符串匹配判断$output的内容是否包含"bad port",如果是,则输出"telnet服务正常",否则输出"telnet服务异常"。
注意,为了避免telnet命令在连接失败时阻塞脚本,我们使用了echo命令发送"quit"字符串,并将telnet命令的输出重定向到变量中。同时,为了避免telnet命令输出到标准错误流中,我们使用了2>&1将标准错误流重定向到标准输出流中。
相关问题
写一个shell脚本内容判断#!/bin/bash # 检查telnet-server是否安装 if ! rpm -qa | grep -qw telnet-server; then echo "telnet-server未安装" exit 1 fi # 检查xinetd是否安装 if ! rpm -qa | grep -qw xinetd; then echo "xinetd未安装" exit 1 fi echo "telnet-server和xinetd已安装" output=$(echo "quit" | telnet 127.0.0.1 12362611511311351 2>&1) if [[ $output == *"bad port"* ]]; then echo "telnet-server is running" else echo "telnet-server is not running" fi # 检查xinetd服务是否正常 xinetd_status=$(systemctl status xinetd | grep Active | awk '{print $2}') if [ $xinetd_status == "active" ]; then echo "Xinetd service is running" else echo "Xinetd service is not running" fi输出正常,则执行echo“aaa”,不正常则退出
好的,下面是一个根据你要求做出修改后的脚本:
```
#!/bin/bash
# 检查telnet-server是否安装
if ! rpm -qa | grep -qw telnet-server; then
echo "telnet-server未安装"
exit 1
fi
# 检查xinetd是否安装
if ! rpm -qa | grep -qw xinetd; then
echo "xinetd未安装"
exit 1
fi
echo "telnet-server和xinetd已安装"
# 检查telnet服务是否正常
output=$(echo "quit" | telnet 127.0.0.1 12362611511311351 2>&1)
if [[ $output == *"bad port"* ]]; then
echo "telnet-server is running"
else
echo "telnet-server is not running"
exit 1
fi
# 检查xinetd服务是否正常
xinetd_status=$(systemctl status xinetd | grep Active | awk '{print $2}')
if [ $xinetd_status == "active" ]; then
echo "Xinetd service is running"
else
echo "Xinetd service is not running"
exit 1
fi
echo "aaa"
```
脚本的执行流程如下:
1. 检查telnet-server和xinetd是否安装,如果未安装则输出提示信息并退出脚本。
2. 检查telnet服务是否正常,如果输出包含"bad port"则表示服务正常,否则输出提示信息并退出脚本。
3. 检查xinetd服务是否正常,如果状态为"active"则表示服务正常,否则输出提示信息并退出脚本。
4. 如果以上检查都通过,则输出"aaa"。
注意,如果有任何一个检查不通过,脚本都会输出提示信息并使用exit 1退出,表示执行失败。如果所有检查都通过,则输出"aaa"表示执行成功。
写一个shell脚本用于自动化执行MySQL安全配置时需要交互的操作
好的,这里是一个简单的shell脚本,可以用于自动化执行MySQL安全配置时需要交互的操作:
```bash
#!/bin/bash
echo "Starting MySQL secure installation..."
# Set root password
mysql -u root -p <<EOF
UPDATE mysql.user SET Password=PASSWORD('your_password_here') WHERE User='root';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';
FLUSH PRIVILEGES;
EOF
# Remove anonymous users
mysql_secure_installation <<EOF
y
EOF
# Disallow root login remotely
mysql_secure_installation <<EOF
n
y
EOF
# Remove test database and access to it
mysql_secure_installation <<EOF
y
EOF
# Reload privileges
mysql -u root -p <<EOF
FLUSH PRIVILEGES;
EOF
echo "MySQL secure installation completed!"
```
在脚本中,我们使用了Here Document(即 `<<EOF`)来将交互命令传递给MySQL命令行。在交互中,我们设置了root密码,删除了匿名用户,禁止了root远程登录,移除了测试数据库和访问权限,并刷新了权限。
请注意,这个脚本是一个简单的示例,您需要根据自己的需求进行修改和适应。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)