wind1211 发表于 2014-11-18 11:02

python ssh

本帖最后由 rdcwayx 于 2014-11-19 19:04 编辑

import paramiko
import time
import sys
#Collecting Huawe Storage
#Created by wind1211 2013/07
def ssh2(ip,username,passwd,cmd):
    #print ip+","+username+","+passwd+","+cmd.rstrip()
    try:
      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect(ip,22,username,passwd,timeout=8)
      stdin,stdout,stderr = ssh.exec_command(cmd)
      cmd_output = stdout.read()
      ssh.close()
    except:
      print "connect to "+ip+"failed"
    return cmd_output

def _wr_csv(buff,_file):
    fb = open(_file,"w")
    #argument buff must be list
    for item in(buff):
      try:
            fb.write(item)
      except:
            print("write"+_file+" info failed")
    fb.close()

#define _ocenStoreSys
def _ocenStoreSys():
    ost_instinfo = "name,sn,model,hba\n"
    _ocenStore_cmd1 = 'license show sys'
    ocenStore_output1 = ssh2(ip,username,passwd,_ocenStore_cmd1)
    #print(ocenStore_output1)
    ocenStore_output = ocenStore_output1.split("\n")
    for ost_item in (ocenStore_output):
      ost_inst = ost_item.rstrip().split()
      ost_inst_len = len(ost_inst)
      if( ost_inst_len == 5 and ost_inst == "Serial"):
            ocenstor_sn = ost_inst
      if(ost_inst_len == 4 and ost_inst == "Model:"):
            ocenstor_model = ost_inst
    #print(ocenstor_sn)
    #print(ocenstor_model)
    ######OceanStore HBA and array name####
    store_file=csv_path+ip+"_array.csv"
    _ocenStore_cmd2_hba = 'storage hba'
    _ocenStore_output2 =ssh2(ip,username,passwd,_ocenStore_cmd2_hba)
    _ocenStore_output = _ocenStore_output2.split("\n")
    for ost_hba_item in (_ocenStore_output):
      ost_hba_inst = ost_hba_item.rstrip().split()
      if(len(ost_hba_inst)>=2 and ost_hba_inst != "Node" and ost_hba_inst != "----"):
            #print(ost_hba_inst)
            ost_inst_hbainfo = ''.join(ost_hba_inst)
            ost_instinfo=ost_instinfo+ip+","+ost_hba_inst+","+ocenstor_sn+","+ocenstor_model+","+ost_inst_hbainfo.replace(",","_")+"\n"
    #print(ost_instinfo)
    _wr_csv(ost_instinfo,store_file)


def _ocenStoreDiskDetail():
    disk_info="name,pool,size,sn\n"
    disk_file=csv_path+ip+"_disk.csv"
    disk_cmd = 'storage disk list detail'
    disk_output1 = ssh2(ip,username,passwd,disk_cmd)
    disk_output = disk_output1.split("\n")
    for disk_item in (disk_output):
      disk_inst = disk_item.rstrip().split()
      if(len(disk_inst)>1 and disk_inst != "Disk" and disk_inst != "===="):
            disk_info = disk_info +disk_inst+","+disk_inst+","+disk_inst+","+disk_inst+"\n"
    #print(disk_info)
    _wr_csv(disk_info,disk_file)

def _ocenStoreLun():
    lun_info="name,size,raid,poolname\n"
    lun_file=csv_path+ip+"_lun.csv"
    lun_cmd = 'storage fs list'
    lun_output1 = ssh2(ip,username,passwd,lun_cmd)
    lun_output = lun_output1.split("\n")
    for lun_item in (lun_output):
      lun_inst = lun_item.rstrip().split()
      if(len(lun_inst)==11 and lun_inst != "FS" and lun_inst != "===="):
            lun_info = lun_info+lun_inst+","+lun_inst+","+lun_inst+","+lun_inst+"\n"
    #print(lun_info)
    _wr_csv(lun_info,lun_file)
   
if __name__=='__main__':
    ip = '1.2.3.4'
    username = 'admin'
    passwd = '1234'
    csv_path="C:\\test\\test3\\remote\\OceanStore\\"
    _ocenStoreSys()

    #Get disk info and reletive pool name
    _ocenStoreDiskDetail()

    #get lun info
    _ocenStoreLun()

rdcwayx 发表于 2014-11-19 19:07

能给个代码备注吗?
页: [1]
查看完整版本: python ssh