由于要实现非交互ssh登陆,每台机器都去作公钥显然太麻烦,因此想简单的把密码写在脚本中,直接登陆执行命令。shell
sshpass原本是首选,可是部分机器须要安装C compiler, 这也不符合预期,因此决定采用expectbash
网上搜索了一下,基本上都是单独的expect脚本,不多有在shell中嵌套expect,这样的话须要两个脚原本执行。ssh
如今写Shell中嵌套expect的用法,首先要肯定/usr/bin/expect存在spa
#!/bin/bash passwd="12345678" expect <<! set time 5 spawn ssh user@192.168.1.2 expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$passwd\r" } } expect "*#" { send "df\r" } expect "*#" { send "exit\r" } expect eof !