#! /usr/bin/python# _*_ coding: utf-8 _*_
# coding=utf-8
import os
import sys
import configparser
import subprocess
import paramiko
import time
# from multiprocessing import process,pool
# import base64
''' 主機存活檢測命令'''
pingcheck = ['ping', '-c', '3', '-i', '0.2', '-w', '1']
''' ssh-keygen 命令 '''
keygencmd = '''ssh-keygen -q -t rsa -p '' -f ~/.ssh/id_rsa && echo yes || echo no'''
''' 讀取pub檔案 '''
readpubcmd = '''test -f ~/.ssh/id_rsa.pub && cat ~/.ssh/id_rsa.pub || echo '''
''' 檢視pub檔案是否存在 '''
testexistscmd = '''test -f ~/.ssh/id_rsa.pub && echo yes || echo no'''
''' 追加公鑰至authorized_keys中'''
addkeycmd = '''echo %s >> ~/.ssh/authorized_keys && echo yes || echo no'''
''' 建立互信後,首次登入時不輸入yes'''
add_ssh_config=''' echo 'stricthostkeychecking no' >> /etc/ssh/ssh_config '''
del_ssh_config=''' sed -i 's/^stricthostkeychecking no/#stricthostkeychecking no/' /etc/ssh/ssh_config '''
''' 測試ssh互信'''
ssh_date ='''ssh %s date '''
''' 訊息返回分隔符,應盡量複雜'''
_mess_part = '''|+|'''
''' 程序池大小 '''
processes = 30
''' 初始化配置檔案 '''
def change_init():
server_id = '20,21,23'
node = 'node2'
config_file_path='my.ini'
# change host
# os.popen("hostnamectl set-hostname %s" % node)
# os.popen("hostname")
## change my.cnf
# os.chdir('/root/pycharm')
cf = configparser.configparser()
cf.read(config_file_path)
print(cf.get('mysqld','server_id'))
cf.set('mysqld','server_id',server_id)
cf.set('mysqld','report_host','192.168.66.102')
with open(config_file_path, "w") as cfg:
cf.write(cfg)
print('server_id:' + str(eval(cf.get('mysqld','server_id'))))
print('report_host:' + cf.get('mysqld','report_host'))
sys.exit()
aut_dic=
ip_list=['192.192.66.101','192.168.66.102','node1','node2']
s_dic=[
,]# ssh-keygen 命令
def exec_ssh_keygen(i,cmd=keygencmd):
server = paramiko.sshclient()
server.load_system_host_keys()
server.set_missing_host_key_policy(paramiko.autoaddpolicy())
server.connect(i['ip'],str(i['port']),i['username'],i['password'], timeout=5)
stdin, stdout, stderr = server.exec_command(cmd)
# print("stdin=%s\nstdout=%s\nstderr=%s" % (stdin,stdout,stderr))
# 讀取pub檔案
def readpubinfo(i,cmd=readpubcmd):
server = paramiko.sshclient()
server.load_system_host_keys()
server.set_missing_host_key_policy(paramiko.autoaddpolicy())
server.connect(i['ip'],i['port'],i['username'],i['password'], timeout=5)
stdin, stdout, stderr = server.exec_command(cmd)
result = stdout.read()
result = str(result,'utf-8')
# i['aud'] = result.replace('\n','')
# print(aut_dic)
# 追加公鑰至authorized_keys中
def writekeys(i,cmd=addkeycmd):
server = paramiko.sshclient()
server.load_system_host_keys()
server.set_missing_host_key_policy(paramiko.autoaddpolicy())
server.connect(i['ip'],i['port'],i['username'],i['password'], timeout=5)
# result = readpubinfo(i['ip'],i['port'],i['username'],i['password'],readpubcmd)
for x in aut_dic:
stdin, stdout, stderr = server.exec_command(cmd % x )
def test_ssh(i):
server = paramiko.sshclient()
server.load_system_host_keys()
server.set_missing_host_key_policy(paramiko.autoaddpolicy())
server.connect(i['ip'], i['port'], i['username'], i['password'], timeout=5)
stdin, stdout, stderr = server.exec_command(add_ssh_config)
for ip in ip_list:
stdin, stdout, stderr = server.exec_command(ssh_date % ip)
stdin, stdout, stderr = server.exec_command(del_ssh_config)
if __name__ == '__main__':
for i in s_dic:
exec_ssh_keygen(i, keygencmd)
readpubinfo(i,readpubcmd)
for i in s_dic:
writekeys(i, cmd=addkeycmd)
test_ssh(i)
expect指令碼實現批量ssh互信
搭hadoop時候需要配置多節點ssh互信,節點數量多了,手工配起來就很麻煩。網上有很多自動配置ssh互信的指令碼,但基本都不能直接用。今天摸索了下,自己寫了個簡易版,測試還能用,在這裡分享下。1 每個節點安裝expect包 yum install expect y 2 將auto ssh.sh及i...
python3 實現多執行緒ssh 批量遠端執行命令
需要模組 paramiko pip3 install paramikoimport paramiko import sys import getpass def rcmd host,password,cmd,port 22,username root ssh paramiko.sshclient s...
python3實現CryptoJS AES加密演算法
from crypto.cipher import aes from binascii import b2a hex,a2b hex import base64 class aescrypt def init self,key self.key key.encode utf8 self.mode a...