利用python實現批量自動化營運指令碼案例

來源:互聯網
上載者:User

本文為通過密碼或密鑰實現python批量自動化營運指令碼案例分享

#!/usr/bin/env python# this script comes from beijing oldboy trainning.# e_mail:70271111@qq.com# function: remote multi exec cmd by ssh.# version:1.1################################################# oldboy trainning info.# QQ 80042789 70271111# site:http://www.etiantian.org# blog:http://oldboy.blog.51cto.com# oldboy trainning QQ group: 208160987 45039636################################################import paramikoimport socketimport sys,oshost = sys.argv[1]user = sys.argv[2]port = int(sys.argv[3])auth_method = sys.argv[4]if auth_method == 'PASSWORD':    password = sys.argv[5]    cmd = sys.argv[6]else:    cmd = sys.argv[5]    s = paramiko.SSHClient()    s.load_system_host_keys()    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())#pkey_file = '/home/alex/.ssh/id_rsa'#key = paramiko.RSAKey.from_private_key_file(pkey_file)#s.connect(host,port,username,pkey=key,timeout=5)try:if auth_method == 'SSH_KEY':    pkey_file = '/home/alex/.ssh/id_rsa'    key = paramiko.RSAKey.from_private_key_file(pkey_file)    s.connect(host,port,user,pkey=key,timeout=5)elif auth_method == 'PASSWORD':    s.connect(host,port,user,password,timeout=5)    stdin,stdout,stderr = s.exec_command(cmd)    cmd_result = stdout.read(),stderr.read()print "--------Result from? %s -------" % hostfor line in cmd_result:    print line,s.close()except paramiko.AuthenticationException:    print "\033[31;1m%s Authentication Error!\033[0m" % hostexcept socket.error:    print "\033[31;1m%s Connection Refused!\033[0m" % host

執行命令:

python batch_ssh.py 10.0.0.8 oldboy 22 SSH_KEY  df

python batch_ssh.py 10.0.0.9 root   33 PASSWORD 123456 df

本文出自 “老男孩linux營運” 部落格,請務必保留此出處http://oldboy.blog.51cto.com/2561410/1221129

查看本欄目更多精彩內容:http://www.bianceng.cn/Programming/extra/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.