1
2
3
4
|
1
.Paramiko SSH连接远程主机
(
1
)使用用户名密码连接远程主机
(
2
)使用key连接远程主机
2
.Paramiko SFTP传送文件
|
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
|
#!/usr/bin/env python
import
paramiko #导入paramiko模块
import
sys,os
host = sys.argv[
1
] #通过sys下的argv来获取命令行的输入,这里为获取命令的第一个参数
user =
'oldboy'
#需要连接的远程主机的用户名
password =
'123456'
#需要连接的远程主机的密码
cmd = sys.argv[
2
] #通过sys下的argv来获取命令行的输入,这里为获取命令的第二个参数
s = paramiko.SSHClient() #调用paramiko模块下的SSHClient()
s.load_system_host_keys() #加载本地的known_hosts文件,该文件是纪录连到对方时,对方给的 host key。每次连线时都会检查目前对方给的 host key 与纪录的 host key 是否相同,可以简单验证连结是否又被诈骗等相关事宜。
s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #加上这句话不用担心选yes的问题,会自动选上(用ssh连接远程主机时,第一次连接时会提示是否继续进行远程连接,选择yes)
s.connect(host,
22
,user,password,timeout=
5
) #连接远程主机,SSH端口号为
22
stdin,stdout,stderr=s.exec_command(cmd) #执行命令
cmd_result = stdout.read(),stderr.read() #读取命令结果,读取命令结果时,一个是空,一个不是空,有错就不会有结果,有结果就没错。
for
line
in
cmd_result:
print line,
s.close()
|
1
2
3
4
5
6
7
8
9
10
11
|
root@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh1.py
192.168
.
1.124
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3
9602276
3538300
5576196
39
% /
tmpfs
959228
0
959228
0
% /dev/shm
/dev/sda1
198337
34143
153954
19
% /boot
root@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh1.py
192.168
.
1.124
'df -h'
Filesystem Size Used Avail Use% Mounted on
/dev/sda3
9
.2G
3
.4G
5
.4G
39
% /
tmpfs 937M
0
937M
0
% /dev/shm
/dev/sda1 194M 34M 151M
19
% /boot
|
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
|
#!/usr/bin/env python
import
paramiko
import
sys,os
host = sys.argv[
1
]
user =
'oldboy'
pkey_file =
'/home/xpleaf/.ssh/id_rsa'
#指定用来解密的私钥的路径,这个需要手动生成,下面会讲如何生成
key = paramiko.RSAKey.from_private_key_file(pkey_file) #使用私钥解密
cmd = sys.argv[
2
]
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(host,
22
,user,pkey=key,timeout=
5
)
stdin,stdout,stderr=s.exec_command(cmd)
cmd_result = stdout.read(),stderr.read()
for
line
in
cmd_result:
print line,
s.close()
|
1
2
3
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ ssh-keygen
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ ls /home/xpleaf/.ssh/
id_rsa id_rsa.pub known_hosts
|
1
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ ssh-copy-id oldboy@
192.168
.
1.124
|
1
2
3
4
5
6
7
8
9
10
11
|
root@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh_key2.py
192.168
.
1.124
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3
9602276
3538304
5576192
39
% /
tmpfs
959228
0
959228
0
% /dev/shm
/dev/sda1
198337
34143
153954
19
% /boot
root@xpleaf-machine:/mnt/hgfs/Python/day6# python ssh_key2.py
192.168
.
1.124
'df -h'
Filesystem Size Used Avail Use% Mounted on
/dev/sda3
9
.2G
3
.4G
5
.4G
39
% /
tmpfs 937M
0
937M
0
% /dev/shm
/dev/sda1 194M 34M 151M
19
% /boot
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/usr/bin/env python
import
paramiko
import
sys,os
host = sys.argv[
1
]
user =
'oldboy'
password=
'123456'
t = paramiko.Transport((host,
22
))
t.connect(username=user,password=password) #连接方式也可以用key,这里只需要将password=password改为pkey=key,其余的key代码与前面的一样
sftp = paramiko.SFTPClient.from_transport(t) #使用t的设置方式连接远程主机
sftp.
get
(
'/tmp/hello.txt'
,
'hello.txt'
) #下载文件
sftp.put(
'ssh1.py'
,
'/tmp/ssh1.py'
) #上传文件
t.close()
|
1
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python ssh_sftp3.py
192.168
.
1.124
|
1
2
3
4
5
6
7
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ ls -l
总用量
7
-rwxrwxrwx
1
root root
19
10
月
8
23
:
49
hello.txt
drwxrwxrwx
1
root root
4096
10
月
8
22
:
40
sorftwares
-rwxrwxrwx
1
root root
435
10
月
8
11
:
36
ssh1.py
-rwxrwxrwx
1
root root
544
10
月
8
15
:
07
ssh_key2.py
-rwxrwxrwx
1
root root
331
10
月
8
23
:
42
ssh_sftp3.py
|
1
2
3
4
5
|
[oldboy@moban tmp]$ ls -l
total
12
drwxrwxrwt.
3
root root
4096
May
13
09
:
35
etc
-rw-rw-r--
1
oldboy oldboy
19
Oct
8
12
:
11
hello.txt
-rw-rw-r--
1
oldboy oldboy
435
Oct
8
20
:
31
ssh1.py
|