期待脚本检查和更改固件

时间:2015-06-26 14:18:43

标签: unix scripting terminal expect script#

您好我正在尝试创建一个期望脚本来检查计算机是否已有固件密码,如果它没有,则允许更新密码。任何帮助将不胜感激。

#!/usr/bin/expect
#check to see if firmware password exists

set verifyPassword 'spawn firmwarepasswd -check'

#if no firmware password exists
if {verifyPassword != ""} {

    spawn firmwarepasswd -setpasswd

    expect "Enter new password:"

    send "1sCrowStrong[\r";

    expect "Re-enter new password:"

    send "1sCrowStrong[\r";

    puts "Password set"

    interact

#if password 1 exists
} else if { verifyPassword == "isfr33d0mfr33?\r" } {

   spawn firmwarepasswd -delete

   expect "Enter password:"

   send "isfr33d0mfr33?\r";

   puts "Password Changed"

   interact

#if password 2 exists

} else {

    spawn firmwarepasswd -delete

    expect "Enter password:"

    send "rapt0r samm1j\r";

    puts "Password Updates"

    interact
}

我建立在给出的建议和答案的基础上,这是我迄今为止所拥有的。但它仍然给我错误。

#!/usr/bin/expect


#Create variable to check to see if firmwarepassword is set Y/N
set verifyPassword [exec firmwarepasswd -check]

#If no firmwarepasswd exists then hardset password
if {$verifyPassword eq "Password Enabled: No"} 

{
spawn firmwarepasswd -setpasswd
expect {Enter new password:}
send {1sCrowStrong[\r};
expect "Re-enter new password:"
send {1sCrowStrong[\r};
expect eof
puts ["Password set"]
}

#If 2nd gen password exists then delete the password and hardset the         designated password
else {$verifyPassword eq "Password Enabled: Yes"} 

{
spawn firmwarepasswd -delete
expect "Enter password:"
send {isfr33d0mfr33?\r};
expect eof
puts ["Password Deleted"]

spawn firmwarepasswd -setpasswd
expect {Enter new password:}
send {1sCrowStrong[\r"}
expect {Re-enter new password:}
send {1sCrowStrong[\r};
expect eof
puts ["Password set"]
}

#If 3rd gen password exists then delete the password and hardset the designated password
if {$verifyPassword eq "Password Enabled: Yes"} {

spawn firmwarepasswd -delete
expect "Enter password:"
send {rapt0r samm1j\r};
expect eof
puts ["Password Deleted"]

spawn firmwarepasswd -setpasswd
expect "Enter new password:"
send {1sCrowStrong[\r};
expect "Re-enter new password:"
send {1sCrowStrong[\r};
expect eof
puts ["Password set"]

}

2 个答案:

答案 0 :(得分:0)

有点不清楚你要求的是什么。

无论如何,我有一些技巧可以帮助你编写脚本。

您应该只生成check命令,而不是使用verifyPassword的变量

spawn firmwarepasswd -check 

expect { 
    "expected output here"{
        //code
    }
    "next expected ouput" {
        //code
    }
    "last expected output" {
       //code
    }
}

如果要将输出发送给用户,请使用send_user

e.g。

send_user "hello world"

最后,将密码硬编码到脚本中是一个安全问题和不良做法。如果您需要向用户索要密码,请在脚本中使用类似stty -echo的内容,这样您就可以安全地要求输入密码。

答案 1 :(得分:0)

我认为firmwarepasswd -check根本不是互动的,所以你可以这样做:

#!/usr/bin/expect

set verifyPassword [exec firmwarepasswd -check]

#if no firmware password exists
if {$verifyPassword eq ""} {

    spawn firmwarepasswd -setpasswd
    expect "Enter new password:"
    send "1sCrowStrong[\r";
    expect "Re-enter new password:"
    send "1sCrowStrong[\r";
    expect eof
    puts "Password set"

#if password 1 exists
} elseif { $verifyPassword eq "isfr33d0mfr33?" } {

   spawn firmwarepasswd -delete
   expect "Enter password:"
   send "isfr33d0mfr33?\r";
   expect eof
   puts "Password Changed"

#if password 2 exists
} else {

    spawn firmwarepasswd -delete
    expect "Enter password:"
    send "rapt0r samm1j\r";
    expect eof
    puts "Password Updated"

}

我修复了语法错误并更改了条件以符合您的评论所说的内容。