python脚本在远程SSH服务器上执行命令并且这个命令带有形参怎么拼接
时间: 2024-02-27 19:51:41 浏览: 82
您好,可以使用paramiko库连接远程SSH服务器,并使用exec_command方法执行命令,例如:
```python
import paramiko
# 连接SSH服务器
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='remote_host', username='remote_user', password='remote_password')
# 带有形参的命令
param1 = 'value1'
param2 = 'value2'
command = f'some_command {param1} {param2}'
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 输出命令执行结果
print(stdout.read().decode())
# 关闭SSH连接
ssh.close()
```
其中,`some_command`是带有形参的命令,`param1`和`param2`是形参的值,可以在命令中使用。在这个例子中,我使用了f-string来拼接带有形参的命令。
阅读全文