python-ssh到h3c交换机执行指令

import paramiko


def ssh_client_con():
    """创建ssh连接,并执行shell指令"""
    # 1 创建ssh_client实例
    ssh_client = paramiko.SSHClient()
    # 自动处理第一次连接的yes或者no的问题
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    # port = '22'
    # username = '用户名'
    # password = ''
    hostname = ['ip',]

    # 2 连接服务器
    for i in hostname:
        print(i)
    ssh_client.connect(
        port=22,
        hostname= 'ip',
        username="用户名",
        password="密码",
        allow_agent=False,
        look_for_keys=False
    )
# 3 执行shell命令
# 构造shell指令
    shell_command = """
             display  interface brief \r     
             \r 
    """


    # 执行shell指令
    stdin, stdout, stderr = ssh_client.exec_command(shell_command)
        # 输出返回信息

    stdout_r = stdout.read()
    print(stdout_r)
    print(type(stdout_r))
    # with open('host_site.txt', 'wb', ) as host_file:
    #     host_file.write(stdout_r)
    # with open('host_site.txt', 'rb') as host_file:
    #     print((host_file.read()).decode("GB2312"))



if __name__ == '__main__':
   ssh_client_con()



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值