Remote server login scripts

ssh remote login server script

The recent work requires frequent visit the company's springboard machine, enter the account password each time you log must therefore think of writing a script to automatically log



1) expect installation dependencies

Check whether the current environment there are dependencies

whereis expect



If you do not need to use the following command to install (ubuntu under)

sudo apt-get install expect



2) to write the automatic logon script login.sh

#!/usr/bin/expect -f

# 设置ssh连接的用户名
set user liuzz

# 设置ssh连接的host地址
set host 8.8.8.8

# 设置ssh连接的端口号(非必需)
set port 22

# 设置ssh连接的超时时间
set timeout 20

# 登录,端口号为默认端口号时可以省略
spawn ssh $user@$host -p $port

# 期待出现的提示(根据登录时的提示调整)
expect "*password:"

# 提交密码
send "$password\r"

# 控制权移交
interact



3) add executable permissions with the script

chmod +x login.sh



4) login script execution

./login.shs

Guess you like

Origin www.cnblogs.com/zzliu/p/11332369.html