Guest User

Untitled

a guest
Dec 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'connection.ui'
  4. #
  5. # Created by: PyQt5 UI code generator 5.11.3
  6. #
  7. # WARNING! All changes made in this file will be lost!
  8.  
  9. from PyQt5 import QtCore, QtGui, QtWidgets
  10. from PyQt5 import QtCore, QtGui, QtWidgets
  11. from PyQt5 import *
  12. from PyQt5 import QtCore, QtGui, QtWidgets
  13. import sys
  14. from PyQt5.QtWidgets import *
  15. import getpass
  16. import telnetlib
  17. import paramiko
  18. import time
  19.  
  20.  
  21. class Ui_Form(object):
  22. def setupUi(self, Form):
  23. Form.setObjectName("Form")
  24. Form.resize(695, 431)
  25. Form.setStyleSheet("background-color: rgb(85, 87, 83);\n"
  26. "background-color: rgb(46, 52, 54);")
  27. self.exit_btn = QtWidgets.QPushButton(Form)
  28. self.exit_btn.setGeometry(QtCore.QRect(240, 310, 141, 41))
  29. self.exit_btn.setObjectName("exit_btn")
  30. self.startipedit = QtWidgets.QLineEdit(Form)
  31. self.startipedit.setGeometry(QtCore.QRect(150, 80, 125, 27))
  32. self.startipedit.setObjectName("startipedit")
  33. self.endipedit = QtWidgets.QLineEdit(Form)
  34. self.endipedit.setGeometry(QtCore.QRect(314, 76, 131, 31))
  35. self.endipedit.setObjectName("endipedit")
  36. self.netmaskedit = QtWidgets.QLineEdit(Form)
  37. self.netmaskedit.setGeometry(QtCore.QRect(465, 76, 131, 31))
  38. self.netmaskedit.setObjectName("netmaskedit")
  39. self.onlyipedit = QtWidgets.QLineEdit(Form)
  40. self.onlyipedit.setGeometry(QtCore.QRect(150, 40, 125, 27))
  41. self.onlyipedit.setObjectName("onlyipedit")
  42. self.conect_btn = QtWidgets.QPushButton(Form)
  43. self.conect_btn.setGeometry(QtCore.QRect(240, 270, 141, 31))
  44. self.conect_btn.setObjectName("conect_btn")
  45. self.splitter = QtWidgets.QSplitter(Form)
  46. self.splitter.setGeometry(QtCore.QRect(39, 40, 111, 71))
  47. self.splitter.setOrientation(QtCore.Qt.Vertical)
  48. self.splitter.setObjectName("splitter")
  49. self.only_ip_rd = QtWidgets.QRadioButton(self.splitter)
  50. self.only_ip_rd.setObjectName("only_ip_rd")
  51. self.range_ip_rd = QtWidgets.QRadioButton(self.splitter)
  52. self.range_ip_rd.setObjectName("range_ip_rd")
  53. self.splitter_2 = QtWidgets.QSplitter(Form)
  54. self.splitter_2.setGeometry(QtCore.QRect(90, 124, 521, 21))
  55. self.splitter_2.setOrientation(QtCore.Qt.Horizontal)
  56. self.splitter_2.setObjectName("splitter_2")
  57. self.ssh_rd = QtWidgets.QRadioButton(self.splitter_2)
  58. self.ssh_rd.setObjectName("ssh_rd")
  59. self.telnet_rd = QtWidgets.QRadioButton(self.splitter_2)
  60. self.telnet_rd.setObjectName("telnet_rd")
  61. self.console_rd = QtWidgets.QRadioButton(self.splitter_2)
  62. self.console_rd.setObjectName("console_rd")
  63. self.splitter_3 = QtWidgets.QSplitter(Form)
  64. self.splitter_3.setGeometry(QtCore.QRect(90, 216, 491, 31))
  65. self.splitter_3.setOrientation(QtCore.Qt.Horizontal)
  66. self.splitter_3.setObjectName("splitter_3")
  67. self.label = QtWidgets.QLabel(self.splitter_3)
  68. self.label.setObjectName("label")
  69. self.useer = QtWidgets.QLineEdit(self.splitter_3)
  70. self.useer.setObjectName("useer")
  71. self.label_2 = QtWidgets.QLabel(self.splitter_3)
  72. self.label_2.setObjectName("label_2")
  73. self.paass = QtWidgets.QLineEdit(self.splitter_3)
  74. self.paass.setObjectName("paass")
  75. self.splitter_4 = QtWidgets.QSplitter(Form)
  76. self.splitter_4.setGeometry(QtCore.QRect(130, 160, 411, 31))
  77. self.splitter_4.setOrientation(QtCore.Qt.Horizontal)
  78. self.splitter_4.setObjectName("splitter_4")
  79. self.brwose = QtWidgets.QPushButton(self.splitter_4)
  80. self.brwose.setObjectName("brwose")
  81. self.brooo = QtWidgets.QLineEdit(self.splitter_4)
  82. self.brooo.setObjectName("brooo")
  83. self.paass.setEchoMode(QLineEdit.Password)
  84. self.retranslateUi(Form)
  85. QtCore.QMetaObject.connectSlotsByName(Form)
  86.  
  87. def retranslateUi(self, Form):
  88. _translate = QtCore.QCoreApplication.translate
  89. Form.setWindowTitle(_translate("Form", "fast-connect"))
  90. self.exit_btn.setText(_translate("Form", "exit"))
  91. self.conect_btn.setText(_translate("Form", "connect"))
  92. self.only_ip_rd.setText(_translate("Form", "only-ip"))
  93. self.range_ip_rd.setText(_translate("Form", "range-of-ip"))
  94. self.ssh_rd.setText(_translate("Form", "ssh"))
  95. self.telnet_rd.setText(_translate("Form", "telnet"))
  96. self.console_rd.setText(_translate("Form", "console "))
  97. self.label.setText(_translate("Form", "username"))
  98. self.label_2.setText(_translate("Form", "password"))
  99. self.brwose.setText(_translate("Form", "browse"))
  100.  
  101. class mainapp (QWidget,Ui_Form):
  102. def __init__(self):
  103. QWidget.__init__(self)
  104. self.setupUi(self)
  105. self.exit_handle()
  106. self.connect_()
  107. self.browsing()
  108. # self.for_consle()
  109.  
  110.  
  111.  
  112.  
  113. def test(self):
  114. self.only_ip_rd.setChecked(True)
  115. self.only_ip_rd.setChecked(False)
  116. def connect_(self):
  117. self.conect_btn.clicked.connect(self.get_ip)
  118. def exit_handle(self):
  119. self.exit_btn.clicked.connect(exit)
  120.  
  121. def browsing(self):
  122. self.brwose.clicked.connect(self.real_browse)
  123. def real_browse(self):
  124. global fname
  125. fname = QFileDialog.getOpenFileName(self,'open file' ,'/')
  126. self.brooo.setText(fname[0])
  127.  
  128. def get_ip (self,):
  129. if self.console_rd.isChecked()==True:
  130. print (" can not support serial connection now ")
  131. #######only ip for ssh and telnet acceptable work ###################################
  132. if self.only_ip_rd.isChecked()== True :
  133. if self.telnet_rd.isChecked() == True:
  134. print (" now we are using telnet and only ip ")
  135. HOST=self.onlyipedit.text()
  136. print (HOST)
  137. username=self.useer.text()
  138. print (username)
  139. password=self.paass.text()
  140. print (password)
  141. tn = telnetlib.Telnet(HOST.encode('ascii'))
  142. tn.read_until(b"Username: ")
  143. tn.write(username.encode('ascii') + b"\n")
  144. if password:
  145. tn.read_until(b"Password: ")
  146. tn.write(password.encode('ascii') + b"\n")
  147. with open(fname[0].encode('ascii'), 'r') as f:
  148. for line in f:
  149. tn.write(line.encode('ascii'))
  150. #tn.write(b"wr")
  151. tn.write(b"exit \n")
  152. print(tn.read_all())
  153.  
  154. if self.only_ip_rd.isChecked() ==True :
  155. if self.ssh_rd.isChecked()==True:
  156. print (" now we are using ssh and only ip ")
  157. ssh_client = paramiko.SSHClient()
  158. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  159. ssh_client.connect(hostname=self.onlyipedit.text(), username=self.useer.text(), password=self.paass.text())
  160. print("Successful connection", )
  161. remote_connection = ssh_client.invoke_shell()
  162. with open (fname[0], 'r') as f :
  163. for line in f :
  164. remote_connection.send(line ) #note you will write commands from first line t wr
  165. time.sleep(1)
  166. output = remote_connection.recv(65535)
  167. print(output)
  168. ssh_client.close
  169.  
  170. if self.range_ip_rd.isChecked()==True:
  171. if self.telnet_rd.isChecked()==True:
  172. print (" now we will config more than ip using telnet ")
  173. start = self.startipedit.text()
  174. end = int (self.endipedit.text())+1
  175. networkname=str(self.netmaskedit.text())
  176. file = fname[0]
  177. user = self.useer.text()
  178. password = self.paass.text()
  179.  
  180. for n in range(int(start), int(end), 1):
  181. print(n)
  182. HOST = networkname + "." + str(n)
  183. print(HOST)
  184. tn = telnetlib.Telnet(HOST)
  185. tn.read_until(b"Username: ")
  186. tn.write(user.encode('ascii') + b"\n")
  187. if password:
  188. tn.read_until(b"Password: ")
  189. tn.write(password.encode('ascii') + b"\n")
  190. tn.write(b"ena\n")
  191. tn.write(b"mano")
  192. with open(file.encode('ascii'), 'r') as f:
  193. for line in f:
  194. tn.write(line.encode('ascii'))
  195. tn.write(b"end \n")
  196. # tn.write(b"wr")
  197. tn.write(b"exit \n")
  198. print(tn.read_all())
  199.  
  200. if self.range_ip_rd.isChecked()==True:
  201. if self.ssh_rd.isChecked()==True:
  202. print (" nwo we will config more than ip ssh ")
  203. start=self.startipedit.text()
  204. end=int (self.endipedit.text())+1
  205. networkname = str(self.netmaskedit.text())
  206. for i in range(int (start),int (end),1):
  207. hosname1 =networkname+"."+str(i)
  208. ssh_client = paramiko.SSHClient()
  209. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  210. ssh_client.connect(hostname=hosname1, username=self.useer.text(),password=self.paass.text())
  211. print("Successful connection", )
  212. remote_connection = ssh_client.invoke_shell()
  213. with open(fname[0], 'r') as f:
  214. for line in f:
  215. remote_connection.send(line) # note you will write commands from first line t wr
  216. time.sleep(1)
  217. output = remote_connection.recv(65535)
  218. print(output)
  219. ssh_client.close
  220. def run ():
  221. app = QApplication(sys.argv)
  222. window = mainapp()
  223. window.show()
  224. app.exec_()
  225. if __name__ == '__main__':
  226. run()
Add Comment
Please, Sign In to add comment