php使用账号密码登录linux执行命令
来源:原创
时间:2021-03-05
作者:脚本小站
分类:PHP
php登录linux脚本:
<?php
#$cmd1 = "if [ ! -f '/root/.ssh/authorized_keys' ]; then mkdir /root/.ssh/ -pv; touch /root/.ssh/authorized_keys; chmod 600 /root/.ssh/authorized_keys; fi";
$cmd1 = "ls /root/.ssh/authorized_keys";
$file = fopen("pve-ips.txt", "r") ;
while(!feof($file))
{
$ip = trim(fgets($file));
echo $ip."\n\r";
loginExec($ip, $cmd1);
}
fclose($file);
function loginExec($ip, $cmd){
$connection = ssh2_connect($ip, 22);
if (ssh2_auth_password($connection, 'root', '123456')) {
echo "Authentication Successful!\n";
} else {
echo "Authentication Failed...\n";
file_put_contents("./notokip.txt", $ip.PHP_EOL, FILE_APPEND);
return false;
}
$stream = ssh2_exec($connection, $cmd);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
$ret = stream_get_contents($stream);
echo $ret;
echo stream_get_contents($errorStream);
fclose($errorStream);
fclose($stream);
}脚本使用命令行方式执行,否者没有及时的错误输出,执行环境可使用docker部署。
