import paramiko import openpyxl # 读取Excel中的账号和密码信息 wb = openpyxl.load_workbook('/root/hosts.xlsx') sheet = wb.active # 连接至Linux主机 # values_only=True ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for row in sheet.iter_rows(min_row=2,values_only=True): username = row[0] password = row[1] host = row[2] ssh.connect(hostname=host, username=username, password=password, port=22) # 执行脚本 stdin, stdout, stderr = ssh.exec_command(''' #!/bin/bash NAME=$(uname -n) if [ -x "$(command -v mysql)" ]; then echo "MySQL is installed" fi if [ -x "$(command -v oracle)" ]; then echo "Oracle is installed" fi if [ -x "$(command -v postgres)" ]; then echo "PostgreSQL is installed" fi if [ -x "$(command -v mongo)" ]; then echo "Mongo is installed" fi if [ -x "$(command -v redis-cli)" ]; then echo "Redis is installed" fi if [ ! -x "$(command -v mysql)" ] && [ ! -x "$(command -v oracle)" ] && [ ! -x "$(command -v postgres)" ] && [ ! -x "$(command -v mongo)" ] && [ ! -x "$(command -v redis-cli)" ]; then echo "No supported database is installed" ''') # 收集查询结果并反馈 output = stdout.read().decode() print(host+"\n"+output) # 关闭连接 ssh.close() 这个python脚本无法检测Oracle,但是里面的shell脚本单独执行可以检测Oracle
时间: 2023-11-28 21:50:06 浏览: 93
用paramiko:ssh.exec_command怎样运行import命令
这可能是由于在Python脚本中使用了双引号,导致Oracle命令被解析成了变量。可以尝试将双引号改成单引号或者使用转义符号来解决这个问题,例如:
```
if [ -x "$(command -v 'oracle')" ]; then
```
或者
```
if [ -x "$(command -v \"oracle\")" ]; then
```
阅读全文