我是在centos上測試的
安裝必須要的包
首先yum search expect
,查看相關(guān)的expect包焙蹭,
yum install expect.x86_64
yum install expect-devel.x86_64
新建腳本文件何荚,如下
#!/usr/bin/expect
set timeout 30
spawn ssh user@ip
expect "password:"
send "pwd\r"
expect "]*" # 這句不能少须教,好多教程都是少了這句,實際測試的時候都不通
send "<your command>\r"
send "exit\r" # 退出
interact
執(zhí)行腳本
需要給腳本增加執(zhí)行權(quán)限chmod +x shell.sh
秘车,不能使用sh shell.sh
的方式啟動,否則會報如下錯誤:
ssh_shell.sh: line 4: spawn: command not found
參考文章: