实用自动化运维Python脚本分享


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于实用自动化运维Python脚本分享,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

并行发送sh命令

pbsh.py

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import paramiko

import sys

import threading

#Copy local file to remote server.

def sshclient_scp(hostname, port, username, password, local_path, remote_path):

 t = paramiko.Transport((hostname, port))

 t.connect(username=username, password=password) # 登录远程服务器

 sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议

 sftp.put(local_path, remote_path)

 t.close()

def sshclient_scp_get(hostname, port, username, password, remote_path, local_path):

 t = paramiko.Transport((hostname, port))

 t.connect(username=username, password=password) # 登录远程服务器

 sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议

 sftp.get(remote_path, local_path)

 t.close()

def sshclient_execmd(hostname, port, username, password, execmd):

 paramiko.util.log_to_file("paramiko.log")

 s = paramiko.SSHClient()

 s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

 s.connect(hostname=hostname, port=port, username=username, password=password)

 stdin, stdout, stderr = s.exec_command(execmd)

 stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.

 line=stdout.read()

 s.close()

 print (hostname+":")

 print line

try:

 file_name = sys.argv[1]

 cmd= sys.argv[2]

except IndexError:

 print 'Wrong params!'

 print 'Usage :'

 print '  batch.py "$OS_LIST_FILE" "$BATCH_EXECUTE_CMD"'

 print 'cat oslist.txt:'

 print '192.168.0.1,22,oracle,passwd1'

 print '192.168.0.2,22,oracle,passwd1'

 print '192.168.0.3,24,oracle,passwd1'

 print 'Format is :'

 print 'IPADDR,SSHPORT,USERNAME,PASSWORD'

 print 'Examples of usage:'

 print './batch.py "/root/workspace/oslist.txt" "df -h"'

 sys.exit()

#file_name = sys.argv[1]

#cmd= sys.argv[2]

#maintenance_osinfo

with open(file_name) as file_object:

 for line in file_object:

  splits_str = line.rstrip().split(',')

  a=threading.Thread(target=sshclient_execmd,args=(splits_str[0],int(splits_str[1]),splits_str[2],splits_str[3],cmd))

  a.start()

  #print sshclient_execmd(splits_str[0],int(splits_str[1]),splits_str[2],splits_str[3],cmd)

print sshclient_scp(splits_str[0], int(splits_str[1]), splits_str[2], splits_str[3], file_name, splits_str[4]+file_name)

阅读剩余部分

相关阅读 >>

如何用Python计算圆周率?

Python编译器和解释器的区别

Python如何生成exe文件

什么是Python中的协程?(实例解析)

Python函数之compile()函数

总结Python中执行命令的几种方法

Python在文本开头插入一行的实例

利用Python微信库itchat实现微信自动回复功能

Python随机密码的生成教程

为什么Python没有idle

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...