osx - Running Multiple PHP SSH2 Script -
i have shop having 10 macs. current able shutdown/restart them remotely through php ssh2 function using code
<?php include('net/ssh2.php'); $server = "hostname"; $username = "user"; $password = "pwd"; $command = "sudo shutdown -r now"; $ssh = new net_ssh2($server); if (!$ssh->login($username, $password)) { exit('login failed'); } echo $ssh->exec($command); echo "sucessfully restarted blah blah blah"; ?>
but in order shutdown/restart 10 of terminals, have run 10 different script achieve that. there methods can connect multiple server , run same command?
you store hostnames , credentials in multi-dimensional array. allow iterate through each item using foreach , execute required command on each host. here's example of you'd need do:
<?php include('net/ssh2.php'); $hosts = array( array( 'hostname' => 'hostname1', 'username' => 'user1', 'password' => 'pwd1' ), array( 'hostname' => 'hostname2', 'username' => 'user2', 'password' => 'pwd2' ) ); $command = "sudo shutdown -r now"; foreach ($hosts $host) { $ssh = new net_ssh2($host['hostname']); if (!$ssh->login($host['username'], $host['password'])) { echo "login failed host '{$host['hostname']}'\n"; continue; } echo $ssh->exec($command); echo "sucessfully restarted {$host['hostname']}\n"; }
hope helps.
on security: it's recommended use ssh keys rather usernames , passwords. also, make sure keep macs on network not open, example, customers, should use private network.
Comments
Post a Comment