ssh简介:
SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题.
paramiko简介:
paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。paramiko支持Linux, Solaris, BSD, MacOS X, Windows等平台通过SSH从一个平台连接到另外一个平台。利用该模块,可以方便的进行ssh连接和sftp协议进行sftp文件传输。
paramiko 模块使用
1.执行命令 – 用户名+密码
def test1(ip, port, username, passwd, cmd):
import paramiko
try:
ssh = paramiko.SSHClient() #创建一个ssh对象
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接know_hosts中不存在的主机
ssh.connect(ip, port, username, passwd, timeout=5) # 连接服务器,其中timeout的是超时时间
stdin, stdout, stderr = ssh.exec_command(cmd) # 执行命令,并获取结果
print(stdout.read().decode('utf-8')) # 以utf-8编码对结果进行解码
ssh.close() #关闭ssh
except Exception as e:
print('%s' % e)
if __name__ == '__main__':
test1("49.234.***.***", 22, "root", "password***", "ls -al")
2.上传文件 – 用户名+密码
def test2(ip, port, username, passwd, local_file_path, remote_file_path):
import paramiko
try:
t = paramiko.Transport((ip, port))
t.connect(username=username, password=passwd)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(local_file_path, remote_file_path)
t.close()
except Exception as e:
print('%s' % e)
if __name__ == '__main__':
local_path = os.path.abspath(os.path.dirname(__file__)) + "/const.py"
remote_path = "/root/const.py"
test2("49.234.***.***", 22, "root", "password***", local_path, remote_path)
3.下载文件 – 用户名+密码
def test3(ip, port, username, passwd, local_file_path, remote_file_path):
import paramiko
try:
t = paramiko.Transport((ip, port))
t.connect(username=username, password=passwd)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(remote_file_path, local_file_path)
t.close()
except Exception as e:
print('%s' % e)
if __name__ == '__main__':
local_path = os.path.abspath(os.path.dirname(__file__)) + "/aaa.py"
remote_path = "/root/aaa.py"
test2("49.234.***.***", 22, "root", "password***", local_path, remote_path)
4. 执行命令 – 使用密钥
def test4(ip, port, username, private_key_path, cmd):
import paramiko
key = paramiko.RSAKey.from_private_key_file(private_key_path)
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, key)
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read().decode('utf-8'))
ssh.close()
except Exception as e:
print('%s' % e)
if __name__ == '__main__':
private_key_path = os.path.abspath(os.path.dirname(__file__)) + "/id_rsa"
test1("49.234.***.***", 22, "root", private_key_path, "ls -al")
5.上传文件 – 使用密钥
def test5(ip, port, username, private_key_path, local_file_path, remote_file_path):
import paramiko
key = paramiko.RSAKey.from_private_key_file(private_key_path)
try:
t = paramiko.Transport((ip, port))
t.connect(username=username, pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(local_file_path, remote_file_path)
t.close()
except Exception as e:
print('%s' % e)
if __name__ == '__main__':
private_key_path = os.path.abspath(os.path.dirname(__file__)) + "/id_rsa"
local_path = os.path.abspath(os.path.dirname(__file__)) + "/apps.py"
remote_path = "/root/apps.py"
test5("49.234.***.***", 22, "root", private_key_path, local_path, remote_path)
6.下载文件 – 使用密钥
def test6(ip, port, username, private_key_path, local_file_path, remote_file_path):
import paramiko
key = paramiko.RSAKey.from_private_key_file(private_key_path)
try:
t = paramiko.Transport((ip, port))
t.connect(username=username, pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(remote_file_path, local_file_path)
t.close()
except Exception as e:
print('%s' % e)
if __name__ == '__main__':
private_key_path = os.path.abspath(os.path.dirname(__file__)) + "/id_rsa"
local_path = os.path.abspath(os.path.dirname(__file__)) + "/ccc.py"
remote_path = "/root/ccc.py"
test5("49.234.***.***", 22, "root", private_key_path, local_path, remote_path)