简介:
paramiko是(2.2或更高)的模块,遵循SSH2协议实现了安全(加密和认证)连接远程机器。
安装所需软件包:
tar zxvf pycrypto-2.5.tar.gz
cd pycrypto-2.5 python setup.py build
python setup.py install
tar zxvf paramiko-1.7.7.1.tar.gz
cd paramiko-1.7.7.1 python setup.py build
python setup.py install
脚本简单编写:
管理单台服务器:
脚本一:查询172.16.22.23磁盘使用情况
import paramiko
hostname="172.16.22.23" port=22 username="root" password="larryroot" if __name__=="__main__":
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
stdin,stdout,sterr=s.exec_command("df -Th") print stdout.read()
s.close()
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
脚本二:在远程服务器上执行相应命令
import sys import paramiko
hostname=sys.argv[1]
command = " ".join(sys.argv[2:])
port=22 username="root" password="larryroot" if __name__=="__main__":
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
stdin,stdout,sterr=s.exec_command(command) print stdout.read()
s.close()
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
脚本三:管理多台服务器:批量查询ip列表中对应服务器的磁盘使用情况
import paramiko
port=22 username="root" file=open("ip.list") for line in file:
hostname=str(line.split("\t")[1])
password=str(line.split("\t")[4]).strip() print "##########################",hostname,"########################" s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
stdin,stdout,sterr=s.exec_command("df -Th") print stdout.read()
s.close()
file.close()
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
脚本四:类似于脚本二,在所有远程服务器上执行相应命令
import paramiko import sys
port=22 username="root" command = " ".join(sys.argv[1:])
file=open("ip.list") for line in file:
hostname=str(line.split("\t")[1])
password=str(line.split("\t")[4]).strip() print "##################",hostname,"######################" s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
stdin,stdout,sterr=s.exec_command(command) print stdout.read()
s.close()
file.close()
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
简单整理到这里通过python的paramiko模块可以很方便的管理服务器,文件的上传下载后续会整理出来。
SSH
下面是通过ssh的dsa或rsa公钥验证批量登录服务器执行命令:
import paramiko import sys,os
port=22 username="larry" key_file="~/.ssh/authorized_keys" know_host="/home/larry/.ssh/known_hosts" command=" ".join(sys.argv[1:]) file=open("ip.list") for line in file:
hostname=str(line.split(" ")[1]) print "#####################################",hostname,"###############################################" s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.load_system_host_keys(know_host)
s.connect(hostname,port,username,key_file)
stdin,stdout,sterr=s.exec_command(command) print stdout.read().strip()
s.close()
file.close()
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
执行python脚本:
python sshkey.py df -h
阅读(2766) | 评论(0) | 转发(0) |