#!/usr/bin/python # -*- coding:utf-8 -*- ################################### # # 檢查主機的損壞磁盤 # ################################### import paramiko import sys def DiskCheck(ip): try: # 建立一個sshclient對象 ssh = paramiko.SSHClient() # 允許將信任的主機自動加入到host_allow 列表,此方法必須放在connect方法的前面 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 指定本地的RSA私鑰文件,如果建立密鑰對時設置的有密碼,password為設定的密碼,如無不用指定password參數 # pkey = paramiko.RSAKey.from_private_key_file('/home/super/.ssh/id_rsa', password='12345') pkey = paramiko.RSAKey.from_private_key_file('/home/ptop/topicjie/scripts/keys/id_rsa') # 建立連接 ssh.connect(hostname=ip, port=22, username='ptop', pkey=pkey) # 執行命令 stdin, stdout, stderr = ssh.exec_command("for i in $(df -h|grep data|awk '{print $6}'); do touch $i/test.txt; done; df -h|grep data") # 結果放到stdout中,如果有錯誤將放到stderr中 print(stdout.read().decode()) print(stderr.read()) # 關閉連接 ssh.close() except Exception,e: print e if __name__ =='__main__': if len(sys.argv) != 2: print 'Usage: python CheckDisk.py ip' sys.exit() print 'Host: %s' % sys.argv[1] print '' DiskCheck(sys.argv[1]) print '-' * 80