Python批量管理主机(paramiko、fabric与pexpect)
来源:互联网 发布:淘宝照片拍摄 编辑:程序博客网 时间:2024/04/28 16:07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
paramiko
import
sys
hostname
=
'192.168.1.215'
port
=
22
username
=
'root'
password
=
'123456'
client
=
paramiko.SSHClient()
# 绑定实例
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port, username, password, timeout
=
5
)
stdin, stdout, stderr
=
client.exec_command(
'df -h'
)
# 执行bash命令
result
=
stdout.read()
error
=
stderr.read()
# 判断stderr输出是否为空,为空则打印执行结果,不为空打印报错信息
if
not
error:
print
result
else
:
print
error
client.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
paramiko
import
sys
hostname
=
'192.168.1.215'
port
=
22
username
=
'root'
key_file
=
'/root/.ssh/id_rsa'
cmd
=
" "
.join(sys.argv[
1
:])
def
ssh_conn(command):
client
=
paramiko.SSHClient()
key
=
paramiko.RSAKey.from_private_key_file(key_file)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port, username, pkey
=
key)
stdin, stdout, stderr
=
client.exec_command(command)
# 标准输入,标准输出,错误输出
result
=
stdout.read()
error
=
stderr.read()
if
not
error:
print
result
else
:
print
error
client.close()
if
__name__
=
=
"__main__"
:
ssh_conn(cmd)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
os, sys
import
paramiko
hostname
=
'192.168.1.215'
port
=
22
username
=
'root'
password
=
'123456'
local_path
=
'/root/test.txt'
remote_path
=
'/opt/test.txt'
if
not
os.path.isfile(local_path):
print
local_path
+
" file not exist!"
sys.exit(
1
)
try
:
s
=
paramiko.Transport((hostname, port))
s.connect(username
=
username, password
=
password)
except
Exception as e:
print
e
sys.exit(
1
)
sftp
=
paramiko.SFTPClient.from_transport(s)
# 使用put()方法把本地文件上传到远程服务器
sftp.put(local_path, remote_path)
# 简单测试是否上传成功
try
:
# 如果远程主机有这个文件则返回一个对象,否则抛出异常
sftp.
file
(remote_path)
print
"上传成功."
except
IOError:
print
"上传失败!"
finally
:
s.close()
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
os, sys
import
paramiko
hostname
=
'192.168.1.215'
port
=
22
username
=
'root'
password
=
'123456'
local_path
=
'/root/test.txt'
remote_path
=
'/opt/test.txt'
try
:
s
=
paramiko.Transport((hostname, port))
s.connect(username
=
username, password
=
password)
sftp
=
paramiko.SFTPClient.from_transport(s)
except
Exception as e:
print
e
sys.exit(
1
)
try
:
# 判断远程服务器是否有这个文件
sftp.
file
(remote_path)
# 使用get()方法从远程服务器拉去文件
sftp.get(remote_path, local_path)
except
IOError as e:
print
remote_path
+
"remote file not exist!"
sys.exit(
1
)
finally
:
s.close()
# 测试是否下载成功
if
os.path.isfile(local_path):
print
"下载成功."
else
:
print
"下载失败!"
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
os, sys
import
paramiko
hostname
=
'192.168.1.215'
port
=
22
username
=
'root'
password
=
'123456'
local_path
=
'/root/abc'
remote_path
=
'/opt/abc'
# 去除路径后面正斜杠
if
local_path[
-
1
]
=
=
'/'
:
local_path
=
local_path[
0
:
-
1
]
if
remote_path[
-
1
]
=
=
'/'
:
remote_path
=
remote_path[
0
:
-
1
]
file_list
=
[]
if
os.path.isdir(local_path):
for
root, dirs, files
in
os.walk(local_path):
for
file
in
files:
# 获取文件绝对路径
file_path
=
os.path.join(root,
file
)
file_list.append(file_path)
else
:
print
path
+
"Directory not exist!"
sys.exit(
1
)
try
:
s
=
paramiko.Transport((hostname, port))
s.connect(username
=
username, password
=
password)
sftp
=
paramiko.SFTPClient.from_transport(s)
except
Exception as e:
print
e
for
local_file
in
file_list:
# 替换目标目录
remote_file
=
local_file.replace(local_path, remote_path)
remote_dir
=
os.path.dirname(remote_file)
# 如果远程服务器没目标目录则创建
try
:
sftp.stat(remote_dir)
except
IOError:
sftp.mkdir(remote_dir)
print
"%s -> %s"
%
(local_file, remote_file)
sftp.put(local_file, remote_file)
s.close()
@runs_once函数装饰器,函数只会执行一次
-l打印可用的命令(函数)--set=KEY=VALUE,...逗号分隔,设置环境变量--shortlist简短打印可用命令-c PATH指定本地配置文件-D不加载用户known_hosts文件-f PATH指定fabfile文件-g HOST逗号分隔要操作的主机-i PATH指定私钥文件-k不加载来自~/.ssh下的私钥文件-p PASSWORD使用密码认证and/or sudo-P默认为并行执行方法--port=PORT指定SSH连接端口-R ROLES根据角色操作,逗号分隔-s SHELL指定新shell,默认是'/bin/bash -l -c'--show=LEVELS以逗号分隔的输出--ssh-config-path=PATHSSH配置文件路径-t N设置连接超时时间,单位秒-T N设置远程命令超时时间,单位秒-u USER连接远程主机用户名-x HOSTS以逗号分隔排除主机-z INT并发进程数
1
2
3
4
5
6
7
from
fabric.api
import
local
def
command():
local(
'ls'
)
# fab command
[localhost] local: ls
fabfile.py fabfile.pyc tab.py tab.pyc
Done.
1
2
3
4
5
6
7
8
9
10
11
12
from
fabric.api
import
run
def
command():
run(
'ls'
)
# fab -H 192.168.1.120 -u user command
[
192.168
.
1.120
] Executing task
'command'
[
192.168
.
1.120
] run: ls
[
192.168
.
1.120
] Login password
for
'user'
:
[
192.168
.
1.120
] out: access.log a.py
[
192.168
.
1.120
] out:
Done.
Disconnecting
from
192.168
.
1.120
... done.
1
2
3
4
5
6
7
8
9
10
11
from
fabric.api
import
run
def
hello(name
=
"world"
):
print
(
"Hello %s!"
%
name)
# fab -H localhost hello
[localhost] Executing task
'hello'
Hello world!
Done.
# fab -H localhost hello:name=Python
[localhost] Executing task
'hello'
Hello Python!
Done.
1
2
3
4
5
6
from
fabric.api
import
run, env
env.hosts
=
[
'root@192.168.1.120:22'
,
'root@192.168.1.130:22'
]
env.password
=
'123.com'
env.exclude_hosts
=
[
'root@192.168.1.120:22'
]
# 排除主机
def
command():
run(
'ls'
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# vi install.py
from
fabric.api
import
run, env
env.roledefs
=
{
'web'
: [
'192.168.1.10'
,
'192.168.1.20'
],
'db'
: [
'192.168.1.30'
,
'192.168.1.40'
]
}
env.password
=
'123'
@roles
(
'web'
)
def
task1():
run(
'yum install httpd -y'
)
@roles
(
'db'
)
def
task2():
run(
'yum install mysql-server -y'
)
def
deploy():
execute(task1)
execute(task2)
# fab -f install.py deploy
1
2
3
4
5
6
7
8
from
fabric.api
import
*
env.hosts
=
[
'192.168.1.120'
]
env.user
=
'user'
env.password
=
'123.com'
def
task():
put(
'/root/abc'
,
'/home/user'
)
run(
'ls -l /home/user'
)
# fab task
1
2
3
4
5
6
7
8
from
fabric.api
import
*
env.hosts
=
[
'192.168.1.120'
]
env.user
=
'user'
env.password
=
'123.com'
def
task():
get(
'/home/user/b'
,
'/opt'
)
local(
'ls -l /opt'
)
# fab task
1
2
3
4
5
6
from
fabric.colors
import
*
def
show():
print
green(
'Successful.'
)
print
red(
'Failure!'
)
print
yellow(
'Warning.'
)
# fab show
1
2
3
4
5
例
1
:执行ls命令
>>>
import
pexpect
>>> pexpect.run(
"ls"
)
例
2
:获得命令状态返回值
>>> command_output, exitstatus
=
pexpect.run(
"ls"
, withexitstatus
=
1
)
1
2
3
4
5
6
7
8
9
10
11
# ftp 192.168.1.10
Connected to
192.168
.
1.10
(
192.168
.
1.10
).
220
-
FileZilla Server version
0.9
.
46
beta
220
-
written by Tim Kosse (tim.kosse@filezilla
-
project.org)
220
Please visit http:
/
/
sourceforge.net
/
projects
/
filezilla
/
Name (
192.168
.
1.10
:root): yunwei
331
Password required
for
yunwei
Password:
230
Logged on
Remote system
type
is
UNIX.
ftp>
1
2
3
4
5
6
7
8
9
10
11
import
pexpect
child
=
pexpect.spawn(
'ftp 192.168.1.10'
)
child.expect(
'Name .*: '
)
child.sendline(
'yunwei'
)
child.expect(
'Password:'
)
child.sendline(
'yunweipass'
)
child.expect(
'ftp> '
)
child.sendline(
'ls'
)
child.sendline(
'bye'
)
child.expect(pexpect.EOF)
# pexpect.EOF程序打印提示信息
print
child.before
# 保存命令执行结果
0 0
- Python批量管理主机(paramiko、fabric与pexpect)
- Python批量管理主机(paramiko、fabric与pexpect)
- Python+paramiko基于配置文件的批量主机管理
- pexpect与fabric
- python Pexpect和Fabric模块对比分析
- python之路--paramiko实现简单主机管理
- python fabric paramiko 文件上传 下载
- python下paramiko模块学习之二:利用配置文件登录批量主机
- python下paramiko模块学习之三:上传批量文件到远程主机
- python下paramiko模块学习之四:从远程主机批量下载文件到本机
- python下paramiko模块学习之五:批量主机上传文件
- Python 的pexpect 破解主机密码
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python + fabric 实现批量部署
- 批量主机工具管理
- python模块paramiko与ssh
- python模块paramiko与ssh
- python多进程线程练习:主机批量管理
- RabbitMQ性能测试
- 【典型例题】报错
- 你以为的SPSS只是简单的数据分析软件吗?
- Ubuntu 使用apt-get时提示错误:无法获得锁 /var/lib/dpkg/lock
- linux下操作tomcat进程
- Python批量管理主机(paramiko、fabric与pexpect)
- windows下pycrypto的简易安装
- Maven系列 8.Maven发布
- css - 换行
- MindMapper怎么更换风格
- HDU1203 I NEED A OFFER!(动态规划DP)
- Undirected Graph BFS/DFS
- stm32f407定时器时钟频率的问题
- web的基础知识