shell脚本实现ssh自动登录

功能:

shell脚本实现ssh自动登录, 里面有些注释掉的是编写时遇到的一些问题和调试信息

说明:

LOGIN_FWQ: 登录账户名

LOGIN_FWQ_IP: 登录IP

LOGIN_FWQ_PW: 登录密码

代码如下:

fwqs_ssh.sh 脚本内容,

LOGIN_FWQ=xcz
LOGIN_FWQ_IP=192.168.1.158
LOGIN_FWQ_PW=123
./_fwq_ssh-login_v2.sh $LOGIN_FWQ $LOGIN_FWQ_IP $LOGIN_FWQ_PW

_fwq_ssh-login_v2.sh 脚本内容

#!/usr/bin/expect

#send_user "argc: $argc\n"
if {$argc != 3} {
	send_user "argc: $argc\n"
	send_user "usage:\n"
	send_user "	./_fwq_ssh-login_v2.sh \$user \$ip \$pw\n"
	exit
}

set LOGIN_FWQ [lindex $argv 0]
set LOGIN_FWQ_IP [lindex $argv 1]
set LOGIN_FWQ_PW [lindex $argv 2]

spawn ssh $LOGIN_FWQ@$LOGIN_FWQ_IP

#expect "password: "
#==> compatible is not good

### 密码错误时,不会自动再次输入
#expect "assword"
#send "$LOGIN_FWQ_PW\n"

### 密码错误时,自动再次输入-1
### issue 等待时间timeout
#expect "assword" {
#	send "$LOGIN_FWQ_PW\n"
#	exp_continue
#}

### 密码错误时,自动再次输入-2
expect {
	"assword" {
		send "$LOGIN_FWQ_PW\n"
		exp_continue
	}

# no use -->why ???s
#	"again" {
#		send_user "====================================\n"
#		exp_continue
#	}

### 防止等待时间timeout
### 无\n, or is too much
	"$LOGIN_FWQ" {
		send_user "Success login"
	}
}

interact

你可能感兴趣的:(shell脚本)