Guest User

Untitled

a guest
Jan 10th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. from flask import Flask, render_template, request, redirect
  2. import paramiko
  3. import ssl
  4.  
  5. app = Flask(__name__)
  6.  
  7. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  8. context.load_cert_chain('certificates/cert.pem', 'certificates/key.pem')
  9.  
  10. SERVER_IP = "192.168.0.80"
  11. SERVER_USERNAME = "root"
  12. SERVER_PASSWORD = "secret123"
  13. SERVER_PORT = 22
  14. SERVER_IFCONFIG = "/sbin/ifconfig"
  15.  
  16. @app.route('/')
  17. def index():
  18. return render_template('index.html')
  19.  
  20. @app.route('/SubnetOverview')
  21. def SubnetOverview():
  22. #Try to SSH into the server and execute the command
  23. client = paramiko.SSHClient()
  24. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  25. client.connect(SERVER_IP, port=SERVER_PORT, username=SERVER_USERNAME, password=SERVER_PASSWORD)
  26. stdin, stdout, stderr = client.exec_command(SERVER_IFCONFIG, get_pty=True)
  27. output = stdout.readlines()
  28. output = ''.join(output)
  29. output = output.replace("rn", "<br />")
  30. client.close()
  31. return render_template('SubnetOverview.html', ssh_output=output)
  32.  
  33. @app.route('/AddSubnet', methods = ['POST', 'GET'])
  34. def AddSubnet():
  35. if request.method == 'POST':
  36. #Getting the data from the HTML form
  37. formdata = request.form.to_dict()
  38. vlanid = formdata.get('vlanid')
  39. address = formdata.get('address')
  40. command = " vlan" + vlanid + ":n id: " + vlanid + "n link: eth0n addresses: [ "" + address + "" ]n"
  41. #Opening an SFTP connection
  42. transport = paramiko.Transport((SERVER_IP, SERVER_PORT))
  43. transport.connect(username=SERVER_USERNAME, password=SERVER_PASSWORD)
  44. sftp = paramiko.SFTPClient.from_transport(transport)
  45. file=sftp.file('/etc/netplan/01-network-manager-all.yaml', "a", -1)
  46. file.write(command)
  47. file.flush()
  48. SERVER_COMMAND = "sudo netplan apply"
  49. sftp.close()
  50. transport.close()
  51. #Opening SSH connection to send a command that apply's the config
  52. client = paramiko.SSHClient()
  53. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  54. client.connect(SERVER_IP, port=SERVER_PORT, username=SERVER_USERNAME, password=SERVER_PASSWORD)
  55. stdin, stdout, stderr = client.exec_command(SERVER_COMMAND, get_pty=True)
  56. client.close()
  57. return redirect("/SubnetOverview")
  58. return render_template('AddSubnet.html')
  59.  
  60. if __name__ == "__main__":
  61. app.run(host='0.0.0.0', port=443, ssl_context=context)
Add Comment
Please, Sign In to add comment