php代码: $print = shell_exec($command); $print 的结果显示不完整
时间: 2023-12-10 19:04:34 浏览: 33
如果 `shell_exec()` 函数的结果在输出时显示不完整,很可能是因为输出内容太多,超过了 PHP 的缓冲区大小限制所导致的。
你可以尝试通过增加缓冲区大小的方法来解决这个问题,具体来说,可以使用 `ob_start()` 函数和 `ob_get_clean()` 函数来开启和关闭 PHP 的输出缓冲区。你可以在调用 `shell_exec()` 函数之前使用 `ob_start()` 开启输出缓冲,然后在调用 `shell_exec()` 函数之后使用 `ob_get_clean()` 函数获取完整的输出结果。
例如,下面是一个使用 `ob_start()` 和 `ob_get_clean()` 函数解决输出不完整问题的示例代码:
```php
ob_start();
$print = shell_exec($command);
$output = ob_get_clean();
echo $output;
```
这段代码会先开启输出缓冲区,然后执行 `shell_exec()` 函数,并将结果赋值给 `$print` 变量。接着,使用 `ob_get_clean()` 函数获取完整的输出结果,并将其赋值给 `$output` 变量。最后,使用 `echo` 函数将完整的输出结果输出到屏幕上。
需要注意的是,使用 `ob_start()` 函数开启输出缓冲区可能会对性能产生一定的影响,因此应该谨慎使用。
相关问题
前端button点击后执行import ssh # 建立 SSH 连接 client = ssh.SSHClient() client.load_system_host_keys() client.connect('remote_host', username='username', password='password') # 执行 shell 命令 command = 'sh /text/text.sh' stdin, stdout, stderr = client.exec_command(command) # 输出执行结果 print(stdout.read().decode()) # 关闭 SSH 连接 client.close()
前端无法直接执行SSH连接和命令操作,你需要借助后端来实现此功能。具体实现方式如下:
1. 在后端中使用paramiko库建立SSH连接并执行命令:
```
import paramiko
def execute_command():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('remote_host', username='username', password='password')
command = 'sh /text/text.sh'
stdin, stdout, stderr = ssh.exec_command(command)
output = stdout.read().decode()
ssh.close()
return output
```
2. 定义后端API,在API中调用execute_command()方法并返回结果:
```
from flask import Flask, jsonify
import paramiko
app = Flask(__name__)
@app.route('/execute_command', methods=['GET'])
def execute_command_api():
output = execute_command()
return jsonify({'output': output})
if __name__ == '__main__':
app.run()
```
3. 在前端中使用fetch()方法调用后端API:
```
const executeCommand = async () => {
const response = await fetch('/execute_command');
const result = await response.json();
console.log(result.output);
}
```
这样就可以实现前端按钮点击后执行SSH命令的功能了。需要注意的是,为了保证SSH连接的安全性,建议将SSH连接的用户名和密码等敏感信息存储在后端的环境变量中,在代码中通过读取环境变量来获取这些信息。
stdin, stdout, stderr = coon.exec_command('ls') if stderr.read() == b'': # 没有发生错误 for line in stdout.readlines(): print(line.strip()) else: print(stderr.read())
这段代码是使用Python的Paramiko库来连接SSH服务器,并执行命令。具体流程如下:
1. 使用Paramiko库的SSHClient类来建立SSH连接,如下所示:
```python
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='hostname', port=22, username='username', password='password')
```
2. 使用SSH连接对象的exec_command()方法执行命令,如下所示:
```python
stdin, stdout, stderr = ssh.exec_command('ls')
```
这里执行的是`ls`命令,可以根据需要更改为其他命令。
3. 检查是否有错误发生,如果没有,则读取输出结果并打印,如下所示:
```python
if stderr.read() == b'':
# 没有发生错误
for line in stdout.readlines():
print(line.strip())
```
如果有错误发生,则读取错误信息并打印。
完整代码如下所示:
```python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='hostname', port=22, username='username', password='password')
stdin, stdout, stderr = ssh.exec_command('ls')
if stderr.read() == b'':
# 没有发生错误
for line in stdout.readlines():
print(line.strip())
else:
# 发生错误
print(stderr.read())
```
注意,这里需要安装Paramiko库才能运行代码。可以使用以下命令安装:
```shell
pip install paramiko
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)