paramiko STELNET登陆设备

news2024/9/23 13:29:33

实验目的:

公司有一台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()

关闭会话连接。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1237397.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Azure Machine Learning - 搜索中的语义排名

目录 什么是语义排名&#xff1f;语义排名的工作原理如何收集和总结输入语义排名的输出如何对摘要进行评分 语义功能和限制 在 Azure AI 搜索中&#xff0c;“语义排名”通过使用语言理解对搜索结果重新排名来显著提高搜索相关性&#xff0c; 本文概括性地介绍了语义排名工作原…

脱离form表单校验input(校验单个input输入框)提交时边框变红

把需要自定义校验的数据放在一个对象中&#xff0c;方便以后多个字段校验 customVerifyInps:{communityInp2:"",asPathInp:"",}, 在输入框中绑定id <el-inputid"communityInp2"placeholder""v-model"customVerifyInps.commu…

【数据结构-零基础学习】线索二叉树(代码+图示+解析)

【数据结构-零基础学习】线索二叉树(代码图示解析) 文章目录 【数据结构-零基础学习】线索二叉树(代码图示解析)[toc]定义产生背景种类示意图1)未加入线索的普通二叉树示意图1.12)线索添加的规则3)中序线索二叉树示意图1.24)中序线索二叉树分析示意图1.3 设计代码逻辑(重点)代码…

户外耳机推荐,这几款高性价比的户外耳机不容错过!

对于喜欢户外运动的人来说&#xff0c;在耳机的选择上至关重要&#xff0c;推荐使用骨传导运动耳机的&#xff0c;因为相较于普通的入耳式耳机&#xff0c;前者是通过振动来传输声音的&#xff0c;而后者则是通过空气传导到耳内的。如果是在户外跑步听不到周围环境声音和鸣笛声…

振南技术干货集:制冷设备大型IoT监测项目研发纪实(2)

注解目录 1.制冷设备的监测迫在眉睫 1.1 冷食的利润贡献 1.2 冷设监测系统的困难 &#xff08;制冷设备对于便利店为何如何重要&#xff1f;了解一下你所不知道的便利店和新零售行业。关于电力线载波通信的论战。&#xff09; 2、电路设计 2.1 防护电路 2.1.1 强电防护 …

vue超好用的自定义指令封装

一、指令封装 目录结构&#xff1a; index.ts 统一注册 import { App, Directive } from "vue"; import auth from "./modules/auth"; import copy from "./modules/copy"; import waterMarker from "./modules/waterMarker"; impor…

Python简直是万能的,这5大主要用途你一定要知道!

从2015开始国内就开始慢慢接触Python了&#xff0c;从16年开始Python就已经在国内的热度更高了&#xff0c;目前也可以算的上"全民Python"了。 众所周知小学生的教材里面已经有Python了&#xff0c;国家二级计算机证也需要学习Python了&#xff01; 因为Python简单…

基于springboot实现冬奥会科普平台系统【项目源码+论文说明】计算机毕业设计

基于SpringBoot实现冬奥会科普平台系统演示 摘要 随着信息技术和网络技术的飞速发展&#xff0c;人类已进入全新信息化时代&#xff0c;传统管理技术已无法高效&#xff0c;便捷地管理信息。为了迎合时代需求&#xff0c;优化管理效率&#xff0c;各种各样的管理平台应运而生&…

CSS实现空心的“尖角”

大家好&#xff0c;我是南宫&#xff0c;来分享一个昨天解决的问题。 我记得之前刷面试题的时候&#xff0c;CSS面试题里面赫然有一题是“如何用CSS实现三角形”&#xff0c;我觉得这个问题确实很经典&#xff0c;我上的前端培训班当初就讲过。 大概思路如下&#xff1a; 先…

Redis -- 介绍

1、NoSQL: 指的是非关系型数据库&#xff0c;主要分成四大类&#xff1a;键值存储数据库、列存储数据库、文档型数据库、图形数据库。 2、什么是Redis&#xff1a; Redis是一种基于内存的数据库&#xff0c;一般用于做缓存的中间件。 3、Redis的主要的特点&#xff1a; 1、Rd…

埃尔米特插值(hermite 插值) C++

埃尔米特插值 原理 #pragma once #include <vector> #include <functional> /*埃尔米特插值*/ struct InterpolationPoint {double x; // 插值点的横坐标double y; // 插值点的纵坐标double derivative; // 插值点的导数值// 默认构造函数InterpolationPoint() : x…

一个测试驱动的Spring Boot应用程序开发

文章目录 系统任务用户故事搭建开发环境Web应用的框架Spring Boot 自动配置三层架构领域建模域定义与领域驱动设计领域类 业务逻辑功能随机的Challenge验证 表示层RESTSpring Boot和REST API设计API第一个控制器序列化的工作方式使用Spring Boot测试控制器 小结 这里采用面向需…

str转wstr的三种方法和从网站获取json数据到数据随机提取,返回拼接字符串和动态数组

库的设置 hv库 外部包含目录&#xff1a;…\include\libhv_new\hv; 库目录&#xff1a;…\include\libhv_new\lib\x86\Release; 附加依赖项&#xff1a;hv.lib; //Get请求 获取json数据&#xff0c;然后提取符合 条件的&#xff0c;time值大于自定义变量的值&#xff0c;然后取…

老知识复盘-SQL从提交到执行到底经历了什么 | 京东云技术团队

一、什么是SQL sql(Structured Query Language: 结构化查询语言)是高级的费过程化编程语言,允许用户在高层数据结构上工作, 是一种数据查询和程序设计语言, 也是(ANSI)的一项标准的计算机语言. but… 目前仍然存在着许多不同版本的sql语言,为了与ANSI标准相兼容, 它们必须以相…

webpack 创建typescript项目

【视频链接】尚硅谷TypeScript教程&#xff08;李立超老师TS新课&#xff09; 创建webpack 项目 IDE&#xff1a;webstorm 新建一个空的项目运行npm init初始化项目目录结构 1. 安装 webpack&#xff1a;构建工具webpack-cli&#xff1a; webpack的命令行工具typescript&am…

处理无线debug问题

无限debug的产生 条件说明 开发者工具是打开状态 js代码中有debugger js有定时处理 setInterval(() > {(function (a) {return (function (a) {return (Function(Function(arguments[0]" a ")()))})(a)})(bugger)(de, 0, 0, (0, 0)); }, 1000); ​ #这里就…

【论文阅读】An Experimental Survey of Missing Data Imputation Algorithms

论文地址&#xff1a;An Experimental Survey of Missing Data Imputation Algorithms | IEEE Journals & Magazine | IEEE Xplore 处理缺失数据最简单的方法就是是丢弃缺失值的样本&#xff0c;但这会使得数据更加不完整并且导致偏差或影响结果的代表性。因此&#xff0c;…

wpf使用CefSharp.OffScreen模拟网页登录,并获取身份cookie

目录 框架信息&#xff1a;MainWindow.xamlMainWindow.xaml.cs爬取逻辑模拟登录拦截请求Cookie获取 CookieVisitorHandle 框架信息&#xff1a; CefSharp.OffScreen.NETCore 119.1.20 MainWindow.xaml <Window x:Class"Wpf_CHZC_Img_Identy_ApiDataGet.MainWindow&qu…

虚函数可不可以重载为内联 —— 在开启最大优化时gcc、clang和msvc的表现

下面是对该问题的一种常见回答&#xff1a; 首先&#xff0c;内联是程序员对编译器的一种建议&#xff0c;因此可以在在重载虚函数时在声明处加上inline关键字来修饰&#xff0c; 但是因为虚函数在运行时通过虚函数表&#xff0c;而内联函数在编译时进行代码嵌入&#xff0c;因…

【Spring】之IoC与对象存取

未来的几周时间&#xff0c;大概率我会更新一下Spring家族的一些简单知识。而什么是Spring家族&#xff0c;好多同学还不是很清楚&#xff0c;我先来简单介绍一下吧&#xff1a; 所谓Spring家族&#xff0c;它其实就是一个框架&#xff0c;是基于Servlet再次进行封装的内容。为…