Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何以root?;_Linux_Expect - Fatal编程技术网

Linux 如何以root?;

Linux 如何以root?;,linux,expect,Linux,Expect,我正在尝试以userA身份登录服务器,然后切换到root帐户并运行一些命令。步骤应如下所示: sshuserA@10.0.0.1 苏根 哇 我用下面的代码实现了步骤1,但不知道如何实现步骤2和3 #!/usr/bin/expect -f set timeout 12 set password_root 12345678 set password_A 12345678 spawn ssh -t sflow@10.0.0.1 expect -re ".*password:" send "$pass

我正在尝试以userA身份登录服务器,然后切换到root帐户并运行一些命令。步骤应如下所示:

  • sshuserA@10.0.0.1
  • 苏根
  • 我用下面的代码实现了步骤1,但不知道如何实现步骤2和3

    #!/usr/bin/expect -f
    set timeout 12
    set password_root 12345678
    set password_A 12345678
    
    spawn ssh -t sflow@10.0.0.1
    expect -re ".*password:"
    send "$password_sflow\r"
    expect eof
    

    最后一行,不要使用expect eof,而是在新提示符上使用expect,并继续使用send/expect语句。阅读一些教程,有一些很好的教程介绍您的用例。一个样本:
    #!/usr/bin/expect
    set timeout 12
    set password_root 12345678
    set password_A 12345678
    
    set prompt "#|>|\\\$ $"
    spawn ssh -t user1@xxx.xxx.x.xx
    expect  {
            timeout {puts TIMEOUT}
            "yes/no" {send "yes\r";exp_continue}
            "password:" {send "user1password\r";exp_continue}
            -re $prompt
    }
    send "su - root\r"
    expect "Password:"
    send "rootpassword\r"
    expect -re $prompt
    send "whoami\r"
    expect -re $prompt
    
    # and to exit
    send "exit\r"      ;# exit su
    expect -re $prompt
    send "exit\r"      ;# exit ssh
    expect eof