python ssh 交互式命令行脚本,查找关键字,进行下一步命令操作。
2023-09-27 14:19:40 时间
import paramiko import json import time from paramiko.ssh_exception import NoValidConnectionsError from paramiko.ssh_exception import AuthenticationException class SSHConnection(): def __init__(self, hostname, port, username, password): self.hostname = hostname self.port = port self.username = username self.password = password try: self.connect() except (AuthenticationException, NoValidConnectionsError, TimeoutError) as e: print(e) stdin, stdout, stderr = self.ssh.exec_command('system identity pr') output = stdout.read().decode() self.identity = output.split(':')[1].strip() def connect(self): self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password) def run(self, input): output = self.cmd(input) # self.close() return output def cmd(self, input): stdin, stdout, stderr = self.ssh.exec_command(input) output = stdout.read().decode() # prompt = '[%s@MikroTik] > %s\n' % (self.username, input) prompt = '[%s@%s] > %s\n' % (self.username, self.identity, input) print(prompt + output) return output def close(self): self.ssh.close() if __name__ == '__main__': ssh = SSHConnection('192.168.64.201', 22, '路由器账号', '路由器密码') output = ssh.run('int pppoe-client pr') if output.find('R name="pppoe-out27"') >= 0: ssh.run('/ip pool set test ranges=1.1.1.27') ssh.close()
相关文章
- python bottle框架 解决跨域问题的正确方式
- Python reStructuredText 帮助文件中文乱码
- 基于Python的学生兼职平台的设计和实现
- Python输出信息
- Python SSH登陆--pexpect,pxssh
- python爬虫:抓取下载视频文件,合并ts文件为完整视频
- Python 集合(set)类型的操作——并交差
- Python 自动化运维实现ssh 远程登录管理设备
- 使用Python,OpenCV,本地二进制模式(LBP)进行人脸识别
- Python编程functools模块
- 成功解决:Can‘t find Python executable “python“, you can set the PYTHON env variable.
- python nose测试框架全面介绍十四 --- nose中的只跑上次失败用例
- Python通过ssh连接服务器并执行命令
- Python Locust性能测试框架实践
- PyQt(Python+Qt)学习随笔:QListWidget的addItem方法
- 第9章 Python文件操作目录
- 第3.6节 Python字符串基础知识
- python中sqlite的基本调用和数据库类的封装案例(2)
- Python pandas.DataFrame.divide函数方法的使用
- Python pandas.DataFrame.append函数方法的使用
- FFMpeg无损合并视频的多种方法 python
- 请不要重复犯我在学习Python和Linux系统上的错误
- python 获取某月的第一天零点和最后一天的时间