1. LINUX 系统只能普通账号登录.
2.需要su - root权限才能执行脚本。
 代码无法实现,求大神解决

解决方案 »

  1.   

    <?php header("Content-type: text/html; charset=utf-8"); ?>
    <?php
    $ip="127.0.0.1";
    $port="22";
    $user="root";
    $passwd="123";
    $user1="soc";
    $passwd1="123";if ($user1!="")
    {    $connection = ssh2_connect($ip,$port);
        if (ssh2_auth_password($connection, $user1, $passwd1))
        {
            echo  "登录成功".$ip."<br/>";    }
        else {
            echo "登录失败" . $ip . "<br/>";
            exit;
        }    if (ssh2_scp_send($connection, 'd:/tmp/Linux/36c221be-6ab2-ef53-1589-fe16877914f4.pl', '/tmp/36c221be-6ab2-ef53-1589-fe16877914f4.pl', 0777))
        {        echo "上传文件成功".$ip."<br/>";
        }
        if (ssh2_scp_send($connection, 'd:/tmp/Linux/36c221be-6ab2-ef53-1589-fe16877914f4.sh', '/tmp/36c221be-6ab2-ef53-1589-fe16877914f4.sh', 0777))
        {        echo "上传文件成功".$ip."<br/>";
        }
         $sushell="su - root";
         $supasswd=$passwd;
         $conshell="sh /tmp/36c221be-6ab2-ef53-1589-fe16877914f4.sh ".$ip." ".$user." ".$passwd;
        if ($stream = ssh2_exec($connection,$conshell;"pwd",'xterm'))
        {
            stream_set_blocking($stream, true);
            if ($stream === FALSE) die("pwd failed");
            echo "Output: " . stream_get_contents($stream)."<br/>";
            if(ssh2_scp_recv($connection, '/tmp/'.$ip."_36c221be-6ab2-ef53-1589-fe16877914f4_chk.xml", 'd:/tmp/'.$ip."_36c221be-6ab2-ef53-1589-fe16877914f4_chk.xml"))
            { echo "取回成功".$ip."<br/>";}    }
        else {
            echo "执行失败" . $ip;
            exit;    }
    }$connection = ssh2_connect($ip,$port);
      if (ssh2_auth_password($connection, $user, $passwd))
      {
          echo  "登录成功".$ip."<br/>";   }
      else {
          echo "登录失败" . $ip . "<br/>";
          exit;
      }  if (ssh2_scp_send($connection, 'd:/tmp/Linux/36c221be-6ab2-ef53-1589-fe16877914f4.pl', '/tmp/36c221be-6ab2-ef53-1589-fe16877914f4.pl', 0777))
      {      echo "上传文件成功".$ip."<br/>";
      }
     if (ssh2_scp_send($connection, 'd:/tmp/Linux/36c221be-6ab2-ef53-1589-fe16877914f4.sh', '/tmp/36c221be-6ab2-ef53-1589-fe16877914f4.sh', 0777))
      {    echo "上传文件成功".$ip."<br/>";
       } if ($stream = ssh2_exec($connection, "sh /tmp/36c221be-6ab2-ef53-1589-fe16877914f4.sh ".$ip." ".$user." ".$passwd,'xterm'))
     {
      stream_set_blocking($stream, true);
      if ($stream === FALSE) die("pwd failed");
      echo "Output: " . stream_get_contents($stream)."<br/>";
         if(ssh2_scp_recv($connection, '/tmp/'.$ip."_36c221be-6ab2-ef53-1589-fe16877914f4_chk.xml", 'd:/tmp/'.$ip."_36c221be-6ab2-ef53-1589-fe16877914f4_chk.xml"))
         { echo "取回成功".$ip."<br/>";} }
    else {
        echo "执行失败".$ip;
        exit;
    }
    ?>
      

  2.   

    我想实现:
    1. su到root(未解决)请大神帮忙解决下2.执行脚本 (已解决)
    3.取回结果内容(已解决)
      

  3.   

    https://www.baidu.com/s?ie=UTF-8&wd=php+root
      

  4.   


    我是用 普通账号登录  意思是咋样 ssh2_exec  su root 在执行操作命令呢?
      

  5.   

    用sudo请求试试,输入你自己的密码就可以。