设为首页 收藏本站
查看: 568|回复: 0

批量执行shell命令

[复制链接]

尚未签到

发表于 2018-8-24 08:24:32 | 显示全部楼层 |阅读模式
  虽然目前都实现了自动化如puppet saltstack在环境中的应用,但工作中不可避免的要自己写一些简单的批量执行shell命令的脚本。
  python paramiko模块是目前使用得较为顺手的模块,执行命令时基本无需要转换,直接将shell命令扔进去执行就OK
  简单示例,10个线程同时执行ssh或scp动作,未设置timeout时间,如执行长时间无反应会导致脚本执行问题:
  #!/usr/bin/python
  # _*_ coding: utf-8 _*_
  import paramiko
  import sys
  import logging
  from multiprocessing.dummy import Pool as ThreadPool
  import re
  import os
  try:
  fun_name = sys.argv[1]
  file = open(sys.argv[2])
  except Exception,e:
  print """use like:
  #copy dir or file
  shell_exec_map.py scp ip_file source_file des_file
  #do cmd in host
  exec_map.py ssh ip_file 'yum install -y zabbix'
  """
  exit()
  logging.basicConfig(level=logging.DEBUG ,
  format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
  datefmt='%a, %d %b %Y %H:%M:%S',
  filename='exec_map.log',
  filemode='a+')
  #ssh key文件位置
  privatekeyfile  = os.path.expanduser('/root/.ssh/id_rsa')
  mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
  #############################
  #对ssh key设置密码方式
  #privatekeyfile  = os.path.expanduser('/root/.ssh/id_rsa')
  #mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='password')
  #############################
  def getCmdInfo(host_ip,cmd):
  print host_ip
  try:
  c = paramiko.SSHClient()
  c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ##################
  #密码登录方式
  #c.connect(host_ip,22,'root',password)
  ##################
  c.connect(host_ip,22,'root',pkey=mykey)
  stdin,stdout,stderr = c.exec_command(cmd)
  info = stdout.read()
  c.close()
  return info
  except Exception as e:
  logging.error( 'getCmdInfo ip %s  %s error: %s' % (host_ip,cmd,e))
  def postFileToRemote(host_ip,localpath,remotepath):
  print host_ip
  try:
  t = paramiko.Transport((host_ip,22))
  ##################
  #密码登录方式
  #t.connect(username='root',password='')
  ##################
  t.connect(username='root',pkey=mykey)
  sftp = paramiko.SFTPClient.from_transport(t)
  sftp.put(localpath,remotepath)
  sftp.close()
  t.close()
  except Exception,e:
  logging.error( 'scp_cmd ip %s soure:%s des: %s error: %s' % (host_ip,localpath,remotepath,e))
  #10个并发同时进行
  pool = ThreadPool(10)
  line_list = []
  for line in file.readlines():
  line_list.append(line.strip())
  if fun_name == 'scp':
  try:
  pool.map(scpFile,line_list)
  logging.debug('scpFile ip file %s source %s dec %s' % (sys.argv[2],sys.argv[3],sys.argv[4]))
  except Exception as e:
  logging.error( 'scpFile map error: %s ' % e)
  elif fun_name == 'ssh':
  try:
  if pattern_cmd.findall(sys.argv[3]):
  print "can't use rm command!"
  logging.debug('sshCmd ip file %s cmd %s' % (sys.argv[2],sys.argv[3]))
  else:
  pool.map(sshCmd,line_list)
  logging.debug('sshCmd ip file %s cmd %s' % (sys.argv[2],sys.argv[3]))
  except Exception as e:
  logging.error( 'sshCmd map error: %s ' % e)
  pool.close()
  pool.join()
  ansible基于paramiko模块做的批量执行脚本,远端执行
  1、安装ansible软件,配置了epel源,或者线上下载ansible-1.7.2
  2、配置节点,IP(主机名需要主机能正常解析)
  vi /etc/ansible/hosts
  [other]
  zabbix-127023.xxx.com
  haproxy-127021.xxx .com
  controller-127022.xxx .com
  haproxy-127021.xxx.com
  3、执行Ping命令测试(带上环境变量. ~/.bash_profile)
  ansible -m shell -a '. ~/.bash_profile &&ping -c 2 10.197.128.1' other
  controller-129022.xxx.com | success | rc=0 >>
  PING 10.197.128.1 (10.198.127.1) 56(84) bytes of data.
  64 bytes from 10.198.127.1: icmp_seq=1 ttl=254 time=0.466 ms
  64 bytes from 10.198.127.1: icmp_seq=2 ttl=254 time=0.409 ms
  --- 10.198.127.1 ping statistics ---
  2 packets transmitted, 2 received, 0% packet loss, time 1000ms
  rtt min/avg/max/mdev = 0.409/0.437/0.466/0.035 ms
  pinsh-127024.xxx.com | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
  ansible方法有个不方便的地方第一次执行时,需要手动确认下。python脚本不需要手动确认


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-555744-1-1.html 上篇帖子: Shell脚本编程——数组(array) 下篇帖子: Discuz!6.0后台拿shell-linkboy
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表