python3 批量管理Linux服务器 下发命令与传输文件
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import paramiko
import os, stat
import sys
import operator as op
from string import Template def ssh_connect( _host, _username, _password ):
_ssh_fd = paramiko.SSHClient()
_ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
_ssh_fd.connect( _host, username = _username, password = _password, timeout = 5 )
return _ssh_fd def ssh_exec_cmd( _ssh_fd, _cmd ):
return _ssh_fd.exec_command( _cmd ) def ssh_sftp( _ssh_fd, _local_path, _remote_path ):
_sftp = paramiko.SFTPClient.from_transport(_ssh_fd.get_transport())
_sftp = _ssh_fd.open_sftp()
_sftp.put(_local_path, _remote_path)
#_sftp.get(_remote_path, _local_path)
_sftp.close() def ssh_close( _ssh_fd ):
_ssh_fd.close() def main(ip, password, fiperror, cmds, flag):
username = 'root' fip = open(fiperror,'a') cmds = cmds
flag = flag try:
sshd = ssh_connect( ip, username, password ) if flag == '':
local_path=input('local file path: ')
remote_path=input('remote host path: ') local_path = os.path.join(os.getcwd(), local_path)
try:
ssh_sftp(sshd, local_path, remote_path)
except Exception as e:
print('Error: sftp failed') for cmd in cmds:
stdin, stdout, stderr = ssh_exec_cmd( sshd, cmd )
err_list = []
err_list = stderr.readlines() items = []
items = stdout.readlines() for item in items:
print("{} {}".format(ip, item), end='')
s = Template("nova list --${host_name}")
s = s.safe_substitute(host_name=item) ssh_close( sshd )
except Exception as e:
print( 'ssh %s@%s: %s' % (username, ip, e) )
fip.writelines([ip,"\t",password,"\n"])
fip.close() if __name__ == "__main__":
with open('iplist') as f: errfile = "/tmp/err.log"
fd = open(errfile, 'w')
fd.truncate()
fd.close() scp_flag = input('scp regular file? yes(input 1), no(input 0): ')
cmds = []
cmd = input("input the cmd you want to execute(end with 0): ")
while cmd != '':
cmds.append(cmd)
cmd = input('input the cmd you want to execute(end with 0): ')
for line in f:
x = line.split()
ip = x[0]
if len(x) == 1:
password = ''
else:
password = x[1]
print('---------------ip address: %s--------------' % ip)
main(ip=ip, password=password, fiperror=errfile, cmds=cmds, flag=scp_flag)
python3 批量管理Linux服务器 下发命令与传输文件的更多相关文章
- linux服务器rz命令上传文件
1.首先,要是服务器不支持rz命令的话,需要安装执行 yum -y install lrzsz 2.再输入rz -be命令,选择需要上传的本地文件
- 使用ansible批量管理远程服务器
使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全 ...
- 使用OpenSSH远程管理Linux服务器
一.使用OpenSSH远程管理Linux服务器 sshd是OpenSSH的服务器端守护进程,与之对应的Windows下客户端软件有SecureCRT/Xshell/PuTTY等. OpenSSH一般为 ...
- 如何使用图形界面Webmin管理linux服务器
出处:http://linux.cn/thread/11992/1/1/ 如何使用图形界面Webmin管理linux服务器 一台典型的linux服务器运行命令行环境中,并已经包括了一些用于安装和配置各 ...
- [转载]你需要知道的 16 个 Linux 服务器监控命令
转载自: 你需要知道的 16 个 Linux 服务器监控命令 如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个 专业的 Linux 系统管理员. 有些 ...
- linux 服务器常用命令整理
linux 服务器常用命令整理 目录 网络分析 - tcpdump \ telnet \ (netstat \ ss \ lsof) \ nload 网络传输 - scp \ rsync \ (rz ...
- Gravitational Teleport 开源的通过ssh && kubernetes api 管理linux 服务器集群的网关
Gravitational Teleport 是一个开源的通过ssh && kubernetes api 管理linux 服务器集群的网关 支持以下功能: 基于证书的身份认证 ssh ...
- ansible批量修改linux服务器密码的playbook
从网上找到批量修改Linux服务器root密码的playbook. 使用方法: 1.输入要修改的inventory组 2.按需要,在playbook中输入要修改的IP.新密码,如下: - hosts: ...
- 管理Linux服务器的用户和组
管理Linux服务器的用户和组 Linux操作系统是一个多用户多任务的操作系统,允许多个用户同时登录到系统,使用系统资源. 为了使所有用户的工作顺利进行,保护每个用户的文件和进程,规范每个用户的权限, ...
随机推荐
- prop 和 attr 中一些羞羞的事情
引言 前几天做一个迷你京东小项目的时候涉及到一个全选的小功能,一开始用的是 attr,但是效果完全不是自己想要的,当商品按钮点击过一次后,attr就无法对其状态进行更改,最后谷歌了一番发现需要用 pr ...
- Python中的列表(5)
1.使用函数 range() 创建一个数字列表 for value in range(1,5): print(value) console: 我们发现,它并不会打印数字5,因为 range() 函数, ...
- stm8l定时器中的ARPE
• Auto-reload preload enabled (ARPE bit set in the TIM1_CR1 register). In this mode,when data is wri ...
- LIBSVM使用方法及参数设置
LIBSVM 数据格式需要---------------------- 决策属性 条件属性a 条件属性b ... 2 1:7 2:5 ... 1 1:4 2:2 ... 数据格式转换--------- ...
- Online IDE & Public URLs & turbo
Online IDE powered by Visual Studio Code https://stackblitz.com/ https://www.polymer-project.org/3.0 ...
- chrome webstore
chrome webstore https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfil ...
- Aizu - 0558 Cheese (bfs)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49879 在H * W的地图上有N个奶酪工厂,分别生产硬度为1-N的奶酪.有一 ...
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...
- 洛谷 U41572 Portal2
U41572 Portal2 题目背景 某地ENLIGHTENED的XM研究所正在研究Portal的处理法则,想要揭示XM能量的来源以及应用XM能量.ENLIGHTENED的首席科学家Jacks发现其 ...
- JDBC驱动类型
一下内容引用自http://wiki.jikexueyuan.com/project/jdbc/drive-types.html: 一.什么是JDBC驱动程序? JDBC驱动实现了JDBC API中定 ...