import
paramiko
import
uuid
class
SSHConnection(
object
):
def
__init__(
self
, host
=
'sydnagios'
, port
=
22
, username
=
'root'
,pwd
=
'123'
):
self
.host
=
host
self
.port
=
port
self
.username
=
username
self
.pwd
=
pwd
self
.__k
=
None
def
run(
self
):
self
.connect()
pass
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
cmd(
self
, command):
ssh
=
paramiko.SSHClient()
ssh._transport
=
self
.__transport
stdin, stdout, stderr
=
ssh.exec_command(command)
result
=
stdout.read()
return
result
def
upload(
self
,local_path, target_path):
sftp
=
paramiko.SFTPClient.from_transport(
self
.__transport)
sftp.put(local_path, target_path)
ssh
=
SSHConnection()
ssh.connect()
r1
=
ssh.cmd(
'df'
)
print
(r1.decode())
ssh.upload(
'c:\\temp\\aaa.txt'
,
"/home/yli/s7.py"
)
ssh.close()
-
-
-
-
-
-
-
-
-
-
"C:\Program Files\Python3\python.exe"
C:
/
s13课上代码
/
s13day13课上代码
/
s13day13_课上代码
/
paramiko模块_demo.py
Filesystem
1K
-
blocks Used Available Use
%
Mounted on
/
dev
/
mapper
/
centos
-
root
28813572
6760732
22052840
24
%
/
devtmpfs
1011616
0
1011616
0
%
/
dev
tmpfs
1021272
80
1021192
1
%
/
dev
/
shm
tmpfs
1021272
113928
907344
12
%
/
run
tmpfs
1021272
0
1021272
0
%
/
sys
/
fs
/
cgroup
/
dev
/
sda1
508588
172604
335984
34
%
/
boot
tmpfs
204256
12
204244
1
%
/
run
/
user
/
42
tmpfs
204256
0
204256
0
%
/
run
/
user
/
0