您的位置:首页 > 运维架构 > Linux

SSH自动登陆服务器--ARM linux

2017-03-06 10:45 197 查看

因为需要用到ssh自动登陆到远程服务器并执行相关命令,但是ssh不支持重定向和管道方式,所以只能使用expect,这里我使用的是ARM9的平台所以需要移植expect:

由于expect是基于tcl语言的,所以移植expect前,需先交叉编译tcl

1、下载地址

tcl:http://www.tcl.tk/software/tcltk/download.html

expect:http://sourceforge.net/projects/expect/files/latest/download?source=files

连接-移植过程

shell 脚本:

#!/usr/bin/expect
#-------------------------------------------------- about us
# product: SSH autologin
# Author:horse
# Last Modified: 2017/3/3
# version:001
# user:this script to shutdown remote linux(unix) machine

set ipaddr [lindex $argv 0]
set loginuser [lindex $argv 1]
set loginpass [lindex $argv 2]
set command [lindex $argv 3]

#set ipaddr 192.168.107.109
#set loginuser "root"
#set loginpass "12345678"

set timeout 300
set cmd_prompt "]#|~$]?"

puts "$ipaddr, $loginuser, $loginpass, $command"

#-------------------------------------------------- login by ssh
spawn ssh $loginuser@$ipaddr
set timeout 2
expect {
-re "*yes/no*" {
send "yes\r"
}  "*password:*" {
send "${loginpass}\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}

#---------------------------------------------------- now,we do some commands

expect "*$"
send "$command\r"

exec usleep 300

send  "exit\r"
exit


远程重启系统可以使用以下命令

./ssh.sh 192.168.107.109 root 12345678 reboot\&
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: