python 脚本中的隧道并执行 python 命令

python 脚本中的隧道并执行 python 命令

我想登录到不同的主机并想要执行下面的 python 脚本

isi storagepool list -v |
awk ' /Requested Protection:/ { parity=substr($NF,length($NF)-1,1) }
    /Nodes:/ { nodes=$NF }
    /HDD Total/ { hdd_total=$NF }
    /HDD Used/ { hdd_used=$NF }
    END {
        multiplier=nodes-parity
        total=hdd_total/nodes*multiplier
        used=hdd_used/nodes
        print "parity =" parity
        print "NodeNumber =" nodes
        print "Total = " total " TB"
        print "Effective Total volume = " total*0.8 " TB"
        print "USED =" used                          
        print "Effective used=" used*multiplier*0.8 " TB" 
        print "Available volume=" (hdd_total-hdd_used)/nodes*multiplier*0.8 " TB" }'

在下面的隧道脚本中,我们要运行上述命令,而不是 isi storagepool list 命令。我们不是在寻找 exec_command 脚本python.py,因为我们无法在存储主机上创建任何文件。

import paramiko
from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
    ('jump_host_ip', 222),
    ssh_username="User.name",
    ssh_pkey=paramiko.RSAKey.from_private_key_file("/tmp/id_rsa"),
  #  ssh_private_key_password="user_pass",
    remote_bind_address=("host_ip", 22),
    local_bind_address=('127.0.0.1', 10022)
) as tunnel:
    client = paramiko.SSHClient()
#   client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname='127.0.0.1', username='user.name, password='user_pass', port=10022)
#   client.connect('127.0.0.1', 10022)
    # do some operations with client session
    stdin, stdout, stderr = client.exec_command('sshpass -p  ******* ssh root@Storage_host_ip "isi storagepool list"')
    print stdout.readlines()
    print stderr.readlines()
    client.close()

print('FINISH!')`

相关内容