Python2.7 paramiko模块
来源:互联网 发布:鸟哥的linux私房菜txt 编辑:程序博客网 时间:2024/05/20 20:43
一般使用paramiko的功能就是通过ssh远程执行命令,远程传输文件等等。
模拟远程执行命令:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import
paramiko
#设置日志记录
paramiko.util.log_to_file(
'/tmp/test'
)
#建立连接
ssh
=
paramiko.SSHClient()
#缺失host_knows时的处理方法
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接远程客户机器
ssh.connect(
'10.1.6.190'
,port
=
22
,username
=
'root'
,password
=
'password'
,compress
=
True
)
#获取远程命令执行结果
stdin, stdout, stderr
=
ssh.exec_command(
'hostname;uptime'
)
print
stdout.read()
#输出执行结果
ssh.close()
模拟远程文件传输:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import
paramiko
#建立一个加密的管道
scp
=
paramiko.Transport((
'10.1.6.190'
,
22
))
#建立连接
scp.connect(username
=
'root'
,password
=
'password'
)
#建立一个sftp客户端对象,通过ssh transport操作远程文件
sftp
=
paramiko.SFTPClient.from_transport(scp)
#Copy a remote file (remotepath) from the SFTP server to the local host
sftp.get(
'/root/debian7'
,
'/tmp/debian7'
)
#Copy a local file (localpath) to the SFTP server as remotepath
sftp.put(
'/root/crash-6.1.6.tar.gz'
,
'/tmp/crash-6.1.6.tar.gz'
)
scp.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import
paramiko
import
interactive
#记录日志
paramiko.util.log_to_file(
'/tmp/test'
)
#建立ssh连接
ssh
=paramiko.SSHClient()
ssh
.load_system_host_keys()
ssh
.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh
.connect(
'10.1.6.190'
,port=22,username=
'root'
,password=
'xxxxxx'
,compress=True)
#建立交互式shell连接
channel=
ssh
.invoke_shell()
#建立交互式管道
interactive.interactive_shell(channel)
#关闭连接
channel.close()
ssh
.close()
interactive模块内容如下:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import
socket
import
sys
# windows does not have termios...
try
:
import
termios
import
tty
has_termios
=
True
except
ImportError:
has_termios
=
False
def
interactive_shell(chan):
if
has_termios:
posix_shell(chan)
else
:
windows_shell(chan)
def
posix_shell(chan):
import
select
oldtty
=
termios.tcgetattr(sys.stdin)
try
:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(
0.0
)
while
True
:
r, w, e
=
select.select([chan, sys.stdin], [], [])
if
chan
in
r:
try
:
x
=
chan.recv(
1024
)
if
len
(x)
=
=
0
:
print
'\r\n*** EOF\r\n'
,
break
sys.stdout.write(x)
sys.stdout.flush()
except
socket.timeout:
pass
if
sys.stdin
in
r:
x
=
sys.stdin.read(
1
)
if
len
(x)
=
=
0
:
break
chan.send(x)
finally
:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
# thanks to Mike Looijmans for this code
def
windows_shell(chan):
import
threading
sys.stdout.write(
"Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n"
)
def
writeall(sock):
while
True
:
data
=
sock.recv(
256
)
if
not
data:
sys.stdout.write(
'\r\n*** EOF ***\r\n\r\n'
)
sys.stdout.flush()
break
sys.stdout.write(data)
sys.stdout.flush()
writer
=
threading.Thread(target
=
writeall, args
=
(chan,))
writer.start()
try
:
while
True
:
d
=
sys.stdin.read(
1
)
if
not
d:
break
chan.send(d)
except
EOFError:
# user hit ^Z or F6
pass
来自:http://my.oschina.net/guol/blog/131774
0 0
- Python2.7 paramiko模块
- Python2.7 paramiko模块
- windows 7环境python2.7安装paramiko模块
- Win7下Python2.7环境安装paramiko模块
- Windows(64bit)下Python2.7环境安装paramiko模块
- Win7下Python2.7环境安装paramiko模块
- Linux安装python2.7 easy_install pip paramiko pexpect
- Python2.7 psutil模块
- python2.7安装模块
- 安装paramiko模块
- python ssh模块paramiko
- paramiko模块的使用
- python安装paramiko模块
- Python paramiko模块安装
- Python模块paramiko安装
- Python Paramiko模块【转载】
- python模块之 paramiko
- paramiko模块使用
- Xcode6与Xcode5之间的细节差别(Precompile Prefix Header)
- 第三阶段:java深入
- jquery遍历json数据
- NSString属性声明中的copy和retain区别
- Unity之实时渲染场景中的游戏对象
- Python2.7 paramiko模块
- JS导出EXCEL
- getLayoutParams()和setLayoutParams()方法源码
- 题解析
- 前台线程和后台线程
- android am命令
- c++用WinForm做界面的实现
- 【LeetCode】Next Permutation
- RequireJS入门(一)