python进行ssh操作需要用到paramiko模块,paramiko是python的三方ssh模块,通过socket实现了ssh功能。
pip install paramiko
import paramiko
#创建一个ssh的客户端,用来连接服务器
ssh = paramiko.SSHClient()
#创建一个ssh的白名单
know_host = paramiko.AutoAddPolicy()
#加载创建的白名单
ssh.set_missing_host_key_policy(know_host)
#连接服务器
ssh.connect(
hostname = "10.10.21.177",
port = 22,
username = "root",
password = "123"
)
#执行命令
stdin,stdout,stderr = ssh.exec_command("rm -rf /root/Desktop/mv")
#stdin 标准格式的输入,是一个写权限的文件对象
#stdout 标准格式的输出,是一个读权限的文件对象
#stderr 标准格式的错误,是一个写权限的文件对象
print(stdout.read().decode())
ssh.close()
import threading
import paramiko
class paramikoThreading(threading.Thread):
def __init__(self,command,host,username,password,port=22):
self.command = command
self.host = host
self.username = username
self.password = password
self.port = port
super(paramikoThreading,self).__init__()
def run(self):
ssh = paramiko.SSHClient()
# 创建一个ssh的白名单
know_host = paramiko.AutoAddPolicy()
# 加载创建的白名单
ssh.set_missing_host_key_policy(know_host)
# 连接服务器
ssh.connect(
hostname=self.host,
port=self.port,
username=self.username,
password=self.password,
)
stdin, stdout, stderr = ssh.exec_command(self.command)
print("*"*60)
print("ip:%s,\ncommand:%s,\n"%(self.host,self.command))
print(stdout.read().decode())
print("*"*60)
ssh.close()
if __name__ == '__main__':
from settings import pool #调用配置文件配置文件为settings.py
command = "ls"
t_pool = []
for host in pool:
t = paramikoThreading(
host=host.get("host","localhost"),
username=host.get("username","root"),
password=host.get("password","123"),
command=command
)
t_pool.append(t)
for t in t_pool:
t.start()
for t in t_pool:
t.join()
pool = [
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
]
使用paramiko的Shell交互式连接会让本地返回结果为命令行界面,可以在本地代码的返回结果下,直接操作命令行。
代码如下:
import paramiko
#创建一个ssh的客户端
ssh = paramiko.SSHClient()
#创建一个ssh的白名单
know_host = paramiko.AutoAddPolicy()
#加载创建的白名单
ssh.set_missing_host_key_policy(know_host)
#连接服务器
ssh.connect(
hostname = "10.10.21.177",
port = 22,
username = "root",
password = "12345"
)
shell = ssh.invoke_shell()
shell.settimeout(1)
command = input(">>>"+"\n")
shell.send(command)
while True:
try:
recv = shell.recv(512).decode()
if recv:
print(recv)
else:
continue
except:
command = input(">>>") + "\n"
shell.send(command)
ssh.close() #关闭连接
import paramiko
trans = paramiko.Transport(
sock=("10.10.21.177",22)
)
trans.connect(
username="root",
password="12345"
)
sftp = paramiko.SFTPClient.from_transport(trans)
#上传
#把本地的文件settings.py,上传到远端为/root/Desktop/settings.py
sftp.put("settings.py","/root/Desktop/settings.py")
#下载
#从远程/root/Desktop/hh.py获取文件下载到本地名称为hh.py
# sftp.get("/root/Desktop/hh.py","hh.py")
sftp.close()