当前位置: 首页 > news >正文

python -ssh学习

def exe_sshcmd(ip,username,userpswd,port,cmd):
    """
    功能:SSH登录到指定设备,并执行对应的命令
    入参:前四项为ssh登录shell的ip和port,具备管理员权限的用户名和密码,
          cmd可以是单条命令,也可以是命令列表
    返回:每次命令执行结果列表,标准输出结果,不包含错误输出

    Examples:
    |Exe Sshcmd| ip|name|pswd|cmd|

    """
    try:
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_client.connect(ip, int(port), username, userpswd)
        ret=[]

        if isinstance(cmd, list):
            cmd = ";".join(cmd)
        print('exe command:',cmd)
        stdin, stdout, stderr=ssh_client.exec_command("%s"%cmd,timeout=30)
        ret=stdout.read()
        retcode = stdout.channel.recv_exit_status()
        print('exe command return info:\n',str(ret,encoding="gbk"))
        print('exe command return code:',stdout.channel.recv_exit_status())
        if stdout.channel.recv_exit_status()!=0:
            print('stderr:',str(stderr.read(),encoding='gbk'))
        ssh_client.close()
        return bytes.decode(ret).strip(),retcode
    except Exception as err:
        print(err)
        print("ssh connect failed(ip=%s,user=%s,pwd=%s,port=%d)." %(ip,username,userpswd,int(port)))
        return "ssh connect fail",1

相关文章:

  • dify接入语音转文本模型后报错: microphone not authorized
  • C++ 变量的输入输出教程
  • 智科技赋能宠物关怀新时代
  • 纯代码实战--用Deepseek+SQLite+Ollama搭建数据库助手
  • 微信小程序自定义导航栏实现指南
  • vscode使用豆包MARSCode----集成doubao1.5 DeepSeekR1 DeepseekV3模型的ai编程插件
  • 【Go | 从0实现简单分布式缓存】-3:分布式节点通信
  • DeepSeek模型认识:R1V3
  • 密码学(哈希函数)
  • 结构型模式---享元模式
  • CT技术变迁史——CT是如何诞生的?
  • HTTP学习——————(四)TLS等加密算法
  • 哪些因素会导致痉挛性斜颈
  • Muvera模型理论保证的证明
  • Cursor AI编程-详细教程
  • 1-7makefile
  • Graphics View画一个可调速的风机(pyqt)
  • 经典算法 最多约数问题
  • MySQL 数据库安全配置最佳实践
  • 【Java】System 类
  • 视频网站可以做B2C模式吗/北京网络营销公司哪家好
  • 网站导航栏种类/公司怎么做网络营销
  • 物流网站建设重要性/微信推广多少钱一次
  • 贵阳建网站公司/天津seo推广优化
  • 焦作建设银行门户网站/定制开发公司
  • 三亚今天最新通知/爱站网站长seo综合查询工具