ssh操作及接受响应,自动获取test密码

举报
建帅小伙儿 发表于 2022/09/25 01:35:38 2022/09/25
【摘要】 一、ssh操作及接受响应 common.py # coding=utf-8""" @Project :pachong-master @File :common.py @Author :gaojs @Date :2022/7/9 14:01 @Blogs : https://...

一、ssh操作及接受响应

common.py


   
  1. # coding=utf-8
  2. """
  3. @Project :pachong-master
  4. @File :common.py
  5. @Author :gaojs
  6. @Date :2022/7/9 14:01
  7. @Blogs : https://www.gaojs.com.cn
  8. """
  9. import sys
  10. import logging
  11. import time
  12. import paramiko
  13. from time import sleep
  14. import re
  15. import requests
  16. from faker import Factory
  17. sys.setrecursionlimit(5000)
  18. class CLI:
  19. def ssh_ag(self, hostname='192.168.120.209', port=22, username='array', password='admin'):
  20. """
  21. :param self:
  22. :return:
  23. """
  24. # 创建ssh对象
  25. self.fin = open('log.txt', 'w')
  26. self.ssh = paramiko.SSHClient()
  27. self.logging = logging
  28. # 允许连接不在know_hosts文件中的主机
  29. self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  30. # 连接AG
  31. self.ssh.connect(hostname=hostname, port=port, username=username, password=password)
  32. sleep(5)
  33. channel = self.ssh.invoke_shell()
  34. self.channel = channel
  35. channel.settimeout(5)
  36. output = channel.recv(2048).decode('utf-8')
  37. time.sleep(1)
  38. self.fin.write(output)
  39. self.cli_cmd('enable')
  40. self.cli_cmd('')
  41. self.cli_cmd('config ter')
  42. self.cli_cmd('no page')
  43. def print_step(self):
  44. """
  45. :return:
  46. """
  47. result = self.channel.recv(2048)
  48. print(result.decode())
  49. def cli_cmd(self, cli, prompt="[#\$]", timeout=3):
  50. """
  51. :param cli:
  52. :return:
  53. """
  54. output = ''
  55. self.logging.info("AG execute command in enable mode: " + cli)
  56. self.channel.send(cli + '\n')
  57. output = self.read_until(prompt, timeout)
  58. return output
  59. def quit_enable(self):
  60. """
  61. :return:
  62. """
  63. self.cli_cmd('switch global')
  64. self.cli_cmd('exit')
  65. def clear_log(self):
  66. """
  67. :return:
  68. """
  69. sleep(2)
  70. self.cli_cmd('clear log b')
  71. def read_until(self, expected, timeout=10):
  72. """
  73. 等待回显:这个方法是从同事那里偷来的哈哈哈
  74. :return:
  75. """
  76. output = ''
  77. regexp = re.compile(expected)
  78. max_time = time.time() + timeout
  79. i = 0
  80. while time.time() < max_time:
  81. i = i + 1
  82. tmp = ""
  83. try:
  84. tmp = self.channel.recv(1024).decode('utf-8')
  85. except:
  86. pass
  87. self.fin.write(tmp)
  88. self.fin.flush()
  89. output += tmp
  90. if regexp.search(output):
  91. return output
  92. return output
  93. def switch_vsite(self, vsite):
  94. """
  95. 切换虚拟站点
  96. :param vsite:
  97. :return:
  98. """
  99. self.cli_cmd('sw %s' % vsite)
  100. def get_sn(self):
  101. """
  102. 获取sn码
  103. :return:
  104. """
  105. # with open('log.txt', mode='r') as fin:
  106. # resp = fin.read()
  107. resp = self.cli_cmd('show version')
  108. result = re.compile('Serial Number : [A-Z0-9]{31}')
  109. sn = result.findall(resp)[0]
  110. sn_number = sn.split(':')[1]
  111. # print(sn_number)
  112. return sn_number
  113. def create_test_password(self, sn):
  114. """
  115. 生成test用户密码
  116. """
  117. url = 'http://10.3.0.50/cgi-bin/passwd_res'
  118. f = Factory.create()
  119. ua = f.user_agent()
  120. headers = {
  121. 'User-Agent': ua
  122. }
  123. data = {
  124. 'serial': sn
  125. }
  126. rsp = requests.post(url=url, headers=headers, data=data)
  127. passwd = re.findall('password: (.*?)</pre>', rsp.text)[0]
  128. print(passwd)
  129. return passwd
  130. if __name__ == '__main__':
  131. """
  132. 自动获取test密码
  133. """
  134. ag_ip = input('请输入您AG管理IP:')
  135. test = CLI()
  136. test.ssh_ag(hostname=ag_ip)
  137. sn_number = test.get_sn()
  138. passwd = test.create_test_password(sn_number)
  139. print(f'*********************** 您的test账户密码是 {passwd} ************************\n')

2.云服务器操作


   
  1. # coding=utf-8
  2. """
  3. @Project :pachong-master
  4. @File :test01.py
  5. @Author :gaojs
  6. @Date :2022/7/9 15:40
  7. @Blogs : https://www.gaojs.com.cn
  8. """
  9. import sys
  10. import logging
  11. import time
  12. import paramiko
  13. from time import sleep
  14. import re
  15. sys.setrecursionlimit(5000)
  16. class CLI:
  17. def ssh_ag(self, hostname='101.4x.39.xxx', port=22, username='root', password='xxxxxx'):
  18. """
  19. :param self:
  20. :return:
  21. """
  22. # 创建ssh对象
  23. self.fin = open('log.txt', 'w+')
  24. self.ssh = paramiko.SSHClient()
  25. self.logging = logging
  26. # 允许连接不在know_hosts文件中的主机
  27. self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  28. # 连接AG
  29. self.ssh.connect(hostname=hostname, port=port, username=username, password=password)
  30. sleep(5)
  31. channel = self.ssh.invoke_shell()
  32. self.channel = channel
  33. channel.settimeout(5)
  34. output = channel.recv(2048).decode('utf-8')
  35. time.sleep(1)
  36. self.fin.write(output)
  37. def print_step(self):
  38. """
  39. :return:
  40. """
  41. result = self.channel.recv(2048)
  42. print(result.decode())
  43. def cli_cmd(self, cli, prompt="[#\$]", timeout=3):
  44. """
  45. :param cli:
  46. :return:
  47. """
  48. output = ''
  49. self.logging.info("cloud server execute command in enable mode: " + cli)
  50. self.channel.send(cli + '\n')
  51. output = self.read_until(prompt, timeout)
  52. return output
  53. def read_until(self, expected, timeout=10):
  54. """
  55. 等待回显
  56. :return:
  57. """
  58. output = ''
  59. regexp = re.compile(expected)
  60. max_time = time.time() + timeout
  61. i = 0
  62. while time.time() < max_time:
  63. i = i + 1
  64. tmp = ""
  65. try:
  66. tmp = self.channel.recv(1024).decode('utf-8')
  67. except:
  68. pass
  69. self.fin.write(tmp)
  70. self.fin.flush()
  71. output += tmp
  72. if regexp.search(output):
  73. return output
  74. return output
  75. if __name__ == '__main__':
  76. """
  77. 自动获取test密码
  78. """
  79. test = CLI()
  80. test.ssh_ag()
  81. test.cli_cmd('ll /opt')
  82. test.cli_cmd('cd /var/www/html/')
  83. test.cli_cmd('date')
  84. test.cli_cmd('ls')

文章来源: blog.csdn.net,作者:懿曲折扇情,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_41332844/article/details/126837410

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。