免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4002 | 回复: 1
打印 上一主题 下一主题

[其他] python ssh [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-18 11:02 |只看该作者 |倒序浏览
本帖最后由 rdcwayx 于 2014-11-19 19:04 编辑
  1. import paramiko
  2. import time
  3. import sys
  4. #Collecting Huawe Storage
  5. #Created by wind1211 2013/07
  6. def ssh2(ip,username,passwd,cmd):
  7.     #print ip+","+username+","+passwd+","+cmd.rstrip()
  8.     try:
  9.         ssh = paramiko.SSHClient()
  10.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  11.         ssh.connect(ip,22,username,passwd,timeout=8)
  12.         stdin,stdout,stderr = ssh.exec_command(cmd)
  13.         cmd_output = stdout.read()
  14.         ssh.close()
  15.     except:
  16.         print "connect to "+ip+"failed"
  17.     return cmd_output

  18. def _wr_csv(buff,_file):
  19.     fb = open(_file,"w")
  20.     #argument buff must be list
  21.     for item in(buff):
  22.         try:
  23.             fb.write(item)
  24.         except:
  25.             print("write"+_file+" info failed")
  26.     fb.close()

  27. #define _ocenStoreSys
  28. def _ocenStoreSys():
  29.     ost_instinfo = "name,sn,model,hba\n"
  30.     _ocenStore_cmd1 = 'license show sys'
  31.     ocenStore_output1 = ssh2(ip,username,passwd,_ocenStore_cmd1)
  32.     #print(ocenStore_output1)
  33.     ocenStore_output = ocenStore_output1.split("\n")
  34.     for ost_item in (ocenStore_output):
  35.         ost_inst = ost_item.rstrip().split()
  36.         ost_inst_len = len(ost_inst)
  37.         if( ost_inst_len == 5 and ost_inst[1] == "Serial"):
  38.             ocenstor_sn = ost_inst[3]
  39.         if(ost_inst_len == 4 and ost_inst[1] == "Model:"):
  40.             ocenstor_model = ost_inst[2]
  41.     #print(ocenstor_sn)
  42.     #print(ocenstor_model)
  43.     ######OceanStore HBA and array name####
  44.     store_file=csv_path+ip+"_array.csv"
  45.     _ocenStore_cmd2_hba = 'storage hba'
  46.     _ocenStore_output2 =ssh2(ip,username,passwd,_ocenStore_cmd2_hba)
  47.     _ocenStore_output = _ocenStore_output2.split("\n")
  48.     for ost_hba_item in (_ocenStore_output):
  49.         ost_hba_inst = ost_hba_item.rstrip().split()
  50.         if(len(ost_hba_inst)>=2 and ost_hba_inst[0] != "Node" and ost_hba_inst[0] != "----"):
  51.             #print(ost_hba_inst)
  52.             ost_inst_hbainfo = ''.join(ost_hba_inst[1:])
  53.             ost_instinfo=ost_instinfo+ip+","+ost_hba_inst[0]+","+ocenstor_sn+","+ocenstor_model+","+ost_inst_hbainfo.replace(",","_")+"\n"
  54.     #print(ost_instinfo)
  55.     _wr_csv(ost_instinfo,store_file)


  56. def _ocenStoreDiskDetail():
  57.     disk_info="name,pool,size,sn\n"
  58.     disk_file=csv_path+ip+"_disk.csv"
  59.     disk_cmd = 'storage disk list detail'
  60.     disk_output1 = ssh2(ip,username,passwd,disk_cmd)
  61.     disk_output = disk_output1.split("\n")
  62.     for disk_item in (disk_output):
  63.         disk_inst = disk_item.rstrip().split()
  64.         if(len(disk_inst)>1 and disk_inst[0] != "Disk" and disk_inst[0] != "===="):
  65.             disk_info = disk_info +disk_inst[0]+","+disk_inst[1]+","+disk_inst[3]+","+disk_inst[6]+"\n"
  66.     #print(disk_info)
  67.     _wr_csv(disk_info,disk_file)

  68. def _ocenStoreLun():
  69.     lun_info="name,size,raid,poolname\n"
  70.     lun_file=csv_path+ip+"_lun.csv"
  71.     lun_cmd = 'storage fs list'
  72.     lun_output1 = ssh2(ip,username,passwd,lun_cmd)
  73.     lun_output = lun_output1.split("\n")
  74.     for lun_item in (lun_output):
  75.         lun_inst = lun_item.rstrip().split()
  76.         if(len(lun_inst)==11 and lun_inst[0] != "FS" and lun_inst[0] != "===="):
  77.             lun_info = lun_info+lun_inst[0]+","+lun_inst[2]+","+lun_inst[3]+","+lun_inst[10]+"\n"
  78.     #print(lun_info)
  79.     _wr_csv(lun_info,lun_file)
  80.    
  81. if __name__=='__main__':
  82.     ip = '1.2.3.4'
  83.     username = 'admin'
  84.     passwd = '1234'
  85.     csv_path="C:\\test\\test3\\remote\\OceanStore\\"
  86.     _ocenStoreSys()

  87.     #Get disk info and reletive pool name
  88.     _ocenStoreDiskDetail()

  89.     #get lun info
  90.     _ocenStoreLun()
复制代码

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
2 [报告]
发表于 2014-11-19 19:07 |只看该作者
能给个代码备注吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP