Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- dl('ssh2.so');
- $username = 'someone';
- $password = 'somewhere';
- class ssh_client
- {
- private $conn;
- private $shell;
- function connect($in_host, $in_port = 22)
- {
- $this->conn = ssh2_connect($in_host, $in_port);
- if (!$this->conn) {
- return false;
- }
- return true;
- }
- function authenticate($in_username, $in_password, $in_useShell=true)
- {
- if (!ssh2_auth_password($this->conn, $in_username, $in_password)) {
- return false;
- }
- if (!$this->shell) {
- $this->shell = ssh2_shell($this->conn, 'vt102', null, 80, 4096, SSH2_TERM_UNIT_CHARS);
- }
- // get the host name of this cisco machine
- #stream_set_blocking($this->shell, true);
- #fwrite($this->shell, $in_command."\n");
- #$this->prompt = stream_get_line($this->shell, 1024);
- #stream_set_blocking($this->shell, false);
- return true;
- }
- function runCommand($in_command, $in_returnData = true, $in_timeout = 10)
- {
- stream_set_blocking($this->shell,true);
- // send a command
- fwrite($this->shell, $in_command."\n");
- if (!$in_returnData) {
- stream_set_blocking($this->shell, false);
- return null;
- }
- $time_start = time();
- $data = '';
- while (true){
- $data .= fread($this->shell, 4096);
- if ((time() - $time_start) > 10 ) {
- break;
- }
- }
- $data = str_replace($this->prompt.$in_command,'', $data);
- $data = str_replace($this->prompt, '',$data);
- $data = str_replace($in_command, '',$data);
- return $data;
- }
- function runCommandExec($in_command, $in_returnData = true)
- {
- $o_stream = ssh2_exec($this->conn, $in_command);
- if (!$o_stream) {
- return false;
- }
- $s_data = '';
- if ($in_returnData) {
- stream_set_blocking($o_stream, true);
- while ($tmp = fread($o_stream, 4096)) {
- $s_data .= $tmp;
- }
- }
- fclose($o_stream);
- return $s_data;
- }
- }
- $a_cisco = array();
- foreach($a_cisco as $s_ip) {
- echo "$s_ip\n";
- $o_ssh = new ssh_client;
- if (!$o_ssh->connect($s_ip)) {
- die('No connection');
- }
- if (!$o_ssh->authenticate($username, $password)) {
- die('No Auth');
- }
- $o_ssh->runCommand('configure terminal', false);
- // flush the students acl
- $o_ssh->runCommand('no ip access-list extended myacl', false);
- // re-add the acl
- $o_ssh->runCommand("ip access-list extended myacl", false);
- $o_ssh->runCommand("permit tcp any host 8.8.8.8 eq www", false);
- $o_ssh->runCommand("permit tcp any host 8.8.8.8 eq 443", false);
- $o_ssh->runCommand('copy running-config startup-config',false);
- $o_ssh->runCommand('', false);
- $o_ssh->runCommand('exit',false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement