ssh不允许用户名密码登录的,然后大神告诉我有个expect可以实现。
安装expect
ubuntu的安装,别的请自己百度
apt-get install expect
使用
大神当然有demo,嘿嘿,拿来直接用
传送门https://github.com/wuhp/tools/tree/master/script
粘过来一个:
./remote_bash [ip] [user] [passwd] “[cmd]”
在远程主机ip上以用户名user密码passwd登录并执行xxx命令
#!/usr/bin/expect -f
# Usage:
# ./remote_bash [ip] [user] [passwd] "[cmd]"
# In this case, the command line beginner should like '[root@localhost node]# ', ending with ']# '
if { $argc != 4 } {
send_user "usage: $argv0 ip user passwd cmd\n"
exit
}
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set cmd [lindex $argv 3]
set timeout -1
spawn ssh $user@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$password\r" }
}
expect "*# "
send "$cmd\r"
send "exit\r"
expect eof
注:倒数第四行可能报错,他匹配的是登录到命令行后的前缀,不是root用户要改成’&’