基于SSH用于连接远程服务器并执行相关操作
用于连接远程服务器并执行基本命令,
有两种连接方式,一种是基于用户名密码的连接,另一种是基于公钥密钥的连接,两种方式都能封装transport的功能进行连接。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
paramiko
# 创建SSH对象
ssh
=
paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname
=
'192.168.136.8'
, port
=
22
, username
=
'root'
, password
=
'123456'
)
# 执行命令
stdin, stdout, stderr
=
ssh.exec_command(
'ls'
)
# 获取命令结果
result
=
stdout.read()
print
result
# 关闭连接
ssh.close()
|
SSHClient()自己创建t=transport对象,然后用t.connect连接等等操作。
1
2
3
4
5
6
7
8
9
10
11
12
|
import
paramiko
transport
=
paramiko.Transport((
'hostname'
,
22
))
transport.connect(username
=
'root'
, password
=
'123456'
)
ssh
=
paramiko.SSHClient()
ssh._transport
=
transport
# SSHClient 封装 Transport
stdin, stdout, stderr
=
ssh.exec_command(
'df'
)
print
stdout.read()
transport.close()
|
手动创建transport对象。
为什么要采用这种方式?因为后面的sftpclient只能用transport方式,为了避免代码重复,都用一种方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import
paramiko
private_key
=
paramiko.RSAKey.from_private_key_file(
'/home/auto/.ssh/id_rsa'
)
# 创建SSH对象
ssh
=
paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname
=
'c1.salt.com'
, port
=
22
, username
=
'root'
, key
=
private_key)
# 执行命令
stdin, stdout, stderr
=
ssh.exec_command(
'df'
)
# 获取命令结果
result
=
stdout.read()
# 关闭连接
ssh.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import
paramiko
private_key
=
paramiko.RSAKey.from_private_key_file(
'/home/auto/.ssh/id_rsa'
)
transport
=
paramiko.Transport((
'hostname'
,
22
))
transport.connect(username
=
'root'
, pkey
=
private_key)
ssh
=
paramiko.SSHClient()
ssh._transport
=
transport
stdin, stdout, stderr
=
ssh.exec_command(
'df'
)
transport.close()
SSHClient 封装 Transport
|
用于连接远程服务器并执行上传下载文件,只能用transport封装的模式执行。
1
2
3
4
5
6
7
8
9
10
11
12
|
import
paramiko
transport
=
paramiko.Transport((
'hostname'
,
22
))
transport.connect(username
=
'root'
,password
=
'123'
)
sftp
=
paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(
'/tmp/location.py'
,
'/tmp/test.py'
)
# 将remove_path 下载到本地 local_path
sftp.get(
'remote_path'
,
'local_path'
)
transport.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
paramiko
private_key
=
paramiko.RSAKey.from_private_key_file(
'/home/auto/.ssh/id_rsa'
)
transport
=
paramiko.Transport((
'hostname'
,
22
))
transport.connect(username
=
'root'
, pkey
=
private_key )
sftp
=
paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(
'/tmp/location.py'
,
'/tmp/test.py'
)
# 将remove_path 下载到本地 local_path
sftp.get(
'remote_path'
,
'local_path'
)
transport.close()
|
1、本地生成文件 uuid.ha
2、上传文件至服务器 uuid.ha
3、ha.cfg --> ha.cfg.bak
4、uuid.ha --> ha.cfg
5、 reload
传文件用paramiko模块的SFTPClient,命令操作()用paramiko模块的SSHClient,为了避免重复连接,所以都用Transport方式连接。
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
|
import
paramiko
import
uuid
#两个方法都用一个连接,减少连接开销
class
Haproxy(
object
):
def
__init__(
self
):
self
.host
=
'192.168.136.8'
self
.port
=
22
self
.username
=
'root'
self
.pwd
=
'123456'
def
create_file(
self
):
file_name
=
str
(uuid.uuid4())
#uuid('9fb01137-e13d-4ea8-b16e-d7861c2b8d9a')==>'9fb01137-e13d-4ea8-b16e-d7861c2b8d9a'
with
open
(file_name,
'wb'
)as f:
f.write(
'test'
)
return
file_name
def
run(
self
):
self
.connect()
self
.upload()
self
.rename()
self
.close()
def
connect(
self
):
transport
=
paramiko.Transport((
self
.host,
self
.port))
transport.connect(username
=
self
.username,password
=
self
.pwd)
self
.__transport
=
transport
def
close(
self
):
self
.__transport.close()
def
upload(
self
):
file_name
=
self
.create_file()
sftp
=
paramiko.SFTPClient.from_transport(
self
.__transport)
sftp.put(file_name,
'/tmp/ttt.py'
)
def
rename(
self
):
ssh
=
paramiko.SSHClient()
ssh._transport
=
self
.__transport
stdin,stdout,stderro
=
ssh.exec_command(
'mv /tmp/ttt.py /tmp/nnn.py'
)
result
=
stdout.read()
print
result
obj
=
Haproxy()
obj.run()
|