paramiko STELNET登陆设备

实验目的:

公司有一台CE12800的设备,管理地址位172.16.1.2,现在需要编写自动化脚本,通过ssh登陆到设备上并进行简单的信息查看。

实验拓扑:

实验步骤:

步骤1:将本地电脑和ensp的设备进行桥接,桥接配置如下图所示:

步骤2:配置交换机的IP地址。

<HUAWEI>system-view immediately

[HUAWEI]sysname CE1

[CE1]interface  Vlanif 1

[CE1-Vlanif1]ip address 172.16.1.2 24

[CE1-Vlanif1]quit

[CE1]interface  g1/0/0

[CE1-GE1/0/0]undo  shutdown

测试本地的cmd窗口与CE1设备的连通性。

C:\Users\xxx>ping 172.16.1.2

正在 Ping 172.16.1.2 具有 32 字节的数据:

来自 172.16.1.2 的回复: 字节=32 时间=19ms TTL=255

来自 172.16.1.2 的回复: 字节=32 时间=7ms TTL=255

来自 172.16.1.2 的回复: 字节=32 时间=5ms TTL=255

来自 172.16.1.2 的回复: 字节=32 时间=7ms TTL=255

172.16.1.2 的 Ping 统计信息:

    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

最短 = 5ms,最长 = 19ms,平均 = 9ms

步骤3:配置CE1的SSH登陆。

  1. 创建SSH登陆的账号

[CE1]aaa

[CE1-aaa]local-user python password cipher Huawei@123

[CE1-aaa]local-user python user-group manage-ug

[CE1-aaa]local-user python service-type ssh

[CE1-aaa]local-user python level 3

  1. 在CE1设备配置SSH用户的认证方式和服务类型。

[CE1]ssh user python

[CE1]ssh user python authentication-type password

[CE1]ssh user python service-type stelnet

  1. 配置vty用于的登陆方式,及开启stenet服务

[CE1]stelnet server  enable

Info: Succeeded in starting the STelnet server.

[CE1]user-interface vty 0 4

[CE1-ui-vty0-4]authentication-mode  aaa

[CE1-ui-vty0-4]protocol  inbound ssh

[CE1-ui-vty0-4]user  privilege level  3

[CE1-ui-vty0-4]q

使用shell工具查看是否能够登陆到CE1设备

步骤4:编写python代码

完整代码如下:

import  paramiko

import  time

ssh_user = 'python'

ssh_pass = 'Huawei@123'

service_IP = '172.16.1.2'

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy)

client.connect(hostname=service_IP,username=ssh_user,password=ssh_pass)

shell = client.invoke_shell()

shell.send('n\n')

shell.send('screen-length 0 temporary\n')

shell.send('display  current-configuration\n')

time.sleep(3)

dis_cu = shell.recv(999999).decode()

shell.send('display  version\n')

time.sleep(3)

dis_ve = shell.recv(999999).decode()

print(dis_cu)

print('_______________________________')

print(dis_ve)

print('_______________________________')

client.close()

步骤5:编译器执行

步骤6 :查看输出结果

Warning: The initial password poses security risks.

The password needs to be changed. Change now? [Y/N]:n

Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 2.

      The current login time is 2023-11-08 17:44:26.

      The last login time is 2023-11-08 17:34:00 from 172.16.1.1 through SSH.

<CE1>screen-length 0 temporary

Info: The configuration takes effect on the current user terminal interface only.

<CE1>display  current-configuration

!Software Version V800R011C00SPC607B607

!Last configuration was updated at 2023-11-08 17:33:12+00:00 by SYSTEM automatically

#

sysname CE1

#

device board 17 board-type CE-MPUB

device board 1 board-type CE-LPUE

#

aaa

 local-user python password irreversible-cipher $1c$l}y.#(<UOQ$zxCQM:&Z(CZ$k"$,0pK,V(2oKQ9T;726;t3}zG8U$

 local-user python service-type ssh

 local-user python level 3

 local-user python user-group manage-ug

 #

 authentication-scheme default

 #

 authorization-scheme default

 #

 accounting-scheme default

 #

 domain default

 #

 domain default_admin

#

interface Vlanif1

 ip address 172.16.1.2 255.255.255.0

#

interface MEth0/0/0

 undo shutdown

#

interface GE1/0/0

 undo shutdown

#

interface GE1/0/1

 shutdown

#

interface GE1/0/2

 shutdown

#

interface GE1/0/3

 shutdown

#

interface GE1/0/4

 shutdown

#

interface GE1/0/5

 shutdown

#

interface GE1/0/6

 shutdown

#

interface GE1/0/7

 shutdown

#

interface GE1/0/8

 shutdown

#

interface GE1/0/9

 shutdown

#

interface NULL0

#

stelnet server enable

ssh user python

ssh user python authentication-type password

ssh user python service-type stelnet

ssh authorization-type default aaa

#

ssh server cipher aes256_gcm aes128_gcm aes256_ctr aes192_ctr aes128_ctr aes256_cbc aes128_cbc 3des_cbc

#

ssh server dh-exchange min-len 1024

#

ssh client cipher aes256_gcm aes128_gcm aes256_ctr aes192_ctr aes128_ctr aes256_cbc aes128_cbc 3des_cbc

#

user-interface con 0

#

user-interface vty 0 4

 authentication-mode aaa

 user privilege level 3

 protocol inbound ssh

#

vm-manager

#

return

<CE1>

_______________________________

display  version

Huawei Versatile Routing Platform Software

VRP (R) software, Version 8.180 (CE12800 V800R011C00SPC607B607)

Copyright (C) 2012-2018 Huawei Technologies Co., Ltd.

HUAWEI CE12800 uptime is 0 day, 0 hour, 38 minutes

SVRP Platform Version 1.0

<CE1>

_______________________________

进程已结束,退出代码0

代码解析:

(1)导入库

import  paramiko

import  time

导入paramiko 和time 两个库,如果没有安装paramiko库,可以使用编译工具进行按照,以pycharm为例。

点击pycharm的文件----设置,选择python解释器,点击‘+’。

搜素paramiko,点击安装软件包。

(2)定义变量

ssh_user = 'python'

ssh_pass = 'Huawei@123'

service_IP = '172.16.1.2'

分别将在设备配置好的ssh用户名,密码以及登陆设备的IP地址定义为变量。

(3)建立SSH会话连接

client = paramiko.SSHClient()

使用Paramiko SSHClient()实例化SSH对象。本例赋值给client。

client.set_missing_host_key_policy(paramiko.AutoAddPolicy)

新建立ssh连接时不需要再输入yes或no进行确认。

client.connect(hostname=service_IP,username=ssh_user,password=ssh_pass)

目的SSH服务器为172.16.1.2,用户名为python, 密码为Huawei@123以密码认证方式进行用户认证。

(4)

shell = client.invoke_shell()

调用invoke_shell()赋值给shell。invoke_shell()作用是打开一个交互的shell会话。该会话为一个逻辑通道channel,建立在SSH会话连接上。

shell.send('n\n')

shell.send('screen-length 0 temporary\n')

shell.send('display  current-configuration\n')

time.sleep(3)

shell.send即在命令行输入相应的指令。

shell.send('n\n')代表登陆到设备时无需再次修改密码。

shell.send('screen-length 0 temporary\n')代表取消分屏。

shell.send('display  current-configuration\n')代表查看设备当前配置

time.sleep(3)代表等待3秒。

dis_cu = shell.recv(999999).decode()

invoke_shell()已经创建了一个channel逻辑通道。此前所有的输入输出的过程信息都在此channel中。我们可以获取这个channel中所有信息,显示到Python编译器。

shell.send('display  version\n')

time.sleep(3)

dis_ve = shell.recv(999999).decode()

查看设备的版本信息,并且获取回显信息。

print(dis_cu)

print('_______________________________')

print(dis_ve)

print('_______________________________')

打印回显信息。

client.close()

关闭会话连接。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值