paramiko学习笔记总结使用方法

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)

[外链图片转存失败(img-aqVOAfTj-1563940817671)(resources/8DD240593762FBCF6D9130917F374BC1.jpg)]

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)

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值