Advertisement
Guest User

Untitled

a guest
Feb 17th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <?php
  2. dl('ssh2.so');
  3. $username = 'someone';
  4. $password = 'somewhere';
  5.  
  6. class ssh_client
  7. {
  8. private $conn;
  9. private $shell;
  10.  
  11. function connect($in_host, $in_port = 22)
  12. {
  13. $this->conn = ssh2_connect($in_host, $in_port);
  14. if (!$this->conn) {
  15. return false;
  16. }
  17.  
  18. return true;
  19. }
  20.  
  21. function authenticate($in_username, $in_password, $in_useShell=true)
  22. {
  23. if (!ssh2_auth_password($this->conn, $in_username, $in_password)) {
  24. return false;
  25. }
  26.  
  27. if (!$this->shell) {
  28. $this->shell = ssh2_shell($this->conn, 'vt102', null, 80, 4096, SSH2_TERM_UNIT_CHARS);
  29. }
  30.  
  31. // get the host name of this cisco machine
  32. #stream_set_blocking($this->shell, true);
  33. #fwrite($this->shell, $in_command."\n");
  34. #$this->prompt = stream_get_line($this->shell, 1024);
  35. #stream_set_blocking($this->shell, false);
  36. return true;
  37. }
  38.  
  39. function runCommand($in_command, $in_returnData = true, $in_timeout = 10)
  40. {
  41. stream_set_blocking($this->shell,true);
  42. // send a command
  43. fwrite($this->shell, $in_command."\n");
  44.  
  45. if (!$in_returnData) {
  46. stream_set_blocking($this->shell, false);
  47. return null;
  48. }
  49. $time_start = time();
  50. $data = '';
  51. while (true){
  52. $data .= fread($this->shell, 4096);
  53. if ((time() - $time_start) > 10 ) {
  54. break;
  55. }
  56. }
  57.  
  58. $data = str_replace($this->prompt.$in_command,'', $data);
  59. $data = str_replace($this->prompt, '',$data);
  60. $data = str_replace($in_command, '',$data);
  61. return $data;
  62. }
  63. function runCommandExec($in_command, $in_returnData = true)
  64. {
  65. $o_stream = ssh2_exec($this->conn, $in_command);
  66. if (!$o_stream) {
  67. return false;
  68. }
  69.  
  70. $s_data = '';
  71. if ($in_returnData) {
  72. stream_set_blocking($o_stream, true);
  73. while ($tmp = fread($o_stream, 4096)) {
  74. $s_data .= $tmp;
  75. }
  76. }
  77.  
  78. fclose($o_stream);
  79. return $s_data;
  80. }
  81.  
  82. }
  83.  
  84. $a_cisco = array();
  85. foreach($a_cisco as $s_ip) {
  86. echo "$s_ip\n";
  87. $o_ssh = new ssh_client;
  88. if (!$o_ssh->connect($s_ip)) {
  89. die('No connection');
  90. }
  91.  
  92. if (!$o_ssh->authenticate($username, $password)) {
  93. die('No Auth');
  94. }
  95.  
  96. $o_ssh->runCommand('configure terminal', false);
  97.  
  98. // flush the students acl
  99. $o_ssh->runCommand('no ip access-list extended myacl', false);
  100.  
  101. // re-add the acl
  102. $o_ssh->runCommand("ip access-list extended myacl", false);
  103.  
  104. $o_ssh->runCommand("permit tcp any host 8.8.8.8 eq www", false);
  105. $o_ssh->runCommand("permit tcp any host 8.8.8.8 eq 443", false);
  106.  
  107. $o_ssh->runCommand('copy running-config startup-config',false);
  108. $o_ssh->runCommand('', false);
  109. $o_ssh->runCommand('exit',false);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement