制作SSH登录远程服务器的Shell脚本
Ubuntu环境需要安装expect安装包sudo apt-get install expect
使用shell脚本自动ssh登录远程服务器
login.sh
#!/usr/bin/expect -f# 设置ssh连接的用户名set user liuben# 设置ssh连接的host地址set host 10.211.55.4# 设置ssh连接的port端口号set port 9999# 设置ssh连接的登录密码set password admin# 设置ssh连接的超时时间set timeout -1spawn ssh $user@$host -p $portexpect \"*password:\"# 提交密码send \"$password\\r\"# 控制权移交interact
# 确定login.sh脚本有可执行权限chmod +x login.sh# 执行login.sh脚本./login.sh# 注意不能按照习惯来用sh login.sh来这行expect的程序,会提示找不到命令,如下:login.sh: line 3: spawn: command not foundcouldn\'t read file \"*password:\": no such file or directorylogin.sh: line 5: send: command not foundlogin.sh: line 6: interact: command not found因为expect用的不是bash所以会报错。因为bash和expect的脚本指定了不同的脚本解释器#!/usr/bin/expect -f#!/bin/bash执行的时候直接./login.sh就可以了。~切记!