加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 操作 ssh

发布时间:2020-12-17 17:16:58 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #coding:utf-8""" ssh操作例子 实现了服务器日志下载 2012-08-24 yywolf"""import paramikoimport time hostname="????"port=22username="app"passwor

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#coding:utf-8
"""
    ssh操作例子 实现了服务器日志下载
    2012-08-24
    yywolf
"""
import paramiko
import time
 
hostname="????"
port=22
username="app"
password="????"
if __name__=="__main__":
#    paramiko.util.log.log_to_file('paramiko.log')
    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname,port,username,password,timeout=4)
    stdin,stdout,stderr = s.exec_command("sh ~/log/check")
    print stdout.read()
    s.close()
     
     
    #sftp
    t = paramiko.Transport((hostname,port))
    t.connect(username=username,password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    files = sftp.listdir("/home/app/log/")
    for f in files:
        print f
     
    filetime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    #需要下载的文件 和下载后的文件名
    sftp.get("/home/app/log/server.txt","C:UsersAdministratorDesktopserver.txt")    
    sftp.get("/home/app/log/"+filetime+".log.zip","C:UsersAdministratorDesktop"+filetime+".log.zip")
     
    #RASkey
    pkey_file = "E:yytoolskeyrsa.txt"
    key = paramiko.RSAKey.from_private_key_file(pkey_file)
    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname,pkey=key)
    stdin,stderr = s.exec_command("ls -l /home/app/log")
    print stdout.read()
    s.close()
 
    raw_input()

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读