linux 免输入密码脚本

时间:2023-03-08 17:28:50

#! /usr/bin/expect
set command [lindex $argv 0]
set passwd [lindex $argv 1]
spawn su - root -c $command
expect "password:|口令:"
send $passwd\r
expect eof
exit

#! /usr/bin/expect
set passwd [ lindex $argv 0 ]
set shell_path [ lindex $argv 1 ]
spawn su - root
expect "口令:|Password:"
send $passwd\r
expect {
        "su*" {
                #expect eof
                #exit 24
                #send "echo password error\r"
                expect eof
                exit 10
        }
        "*#*" {
                #send "echo login succ\r"
                #expect eof
                #exit 11
  send "echo \r"
  send "$shell_path\r"
        }
}
expect eof
#exit

例子:$argv 0 等是脚本传参数
#! /usr/bin/expect
set user_passwd [ lindex $argv 0 ]
set user_ip [ lindex $argv 1 ]
set user_path [ lindex $argv 2 ]
spawn scp  ../config.xml user@$user_ip:$user_path/user/
expect "?*"
send $user_passwd\r
expect eof