linux shell ssh实现自动登录,并且执行一些操作并返回到当前主机
2015-03-01 00:48
726 查看
#!/usr/bin/expect -f
#-------------------------------------------------- about us
# product: monitorone
# Author:matthew
# Last Modified: xxx
# version:xxx
# user:this script will help you to monitor many linux(unix) machine
# license: this script is based GPL
set loginuser "user"
set loginpass {password}
set ipaddr [lrange $argv 0 0]
set timeout 300
set cmd_prompt "]#|~]?"
#-------------------------------------------------- login by ssh
spawn ssh $loginuser@$ipaddr
set timeout 300
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
} -re "assword:" {
send "${loginpass}\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
expect {
-re "assword:" {
send "$loginpass\r"
}
-re $cmd_prompt {
send "\r"
}
}
#---------------------------------------------------- now,we do some commands
#exec sleep 1
expect {
-re $cmd_prompt {
send "sudo su -\r"
send "touch /opt/2222222222222.c\r"
}
}
exit
interact
#-------------------------------------------------- about us
# product: monitorone
# Author:matthew
# Last Modified: xxx
# version:xxx
# user:this script will help you to monitor many linux(unix) machine
# license: this script is based GPL
set loginuser "user"
set loginpass {password}
set ipaddr [lrange $argv 0 0]
set timeout 300
set cmd_prompt "]#|~]?"
#-------------------------------------------------- login by ssh
spawn ssh $loginuser@$ipaddr
set timeout 300
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
} -re "assword:" {
send "${loginpass}\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
expect {
-re "assword:" {
send "$loginpass\r"
}
-re $cmd_prompt {
send "\r"
}
}
#---------------------------------------------------- now,we do some commands
#exec sleep 1
expect {
-re $cmd_prompt {
send "sudo su -\r"
send "touch /opt/2222222222222.c\r"
}
}
exit
interact
相关文章推荐
- LINUX下使用Shell自动监控tomcat并且执行重启操作
- shell实现ssh自动登录远程主机
- LINUX下使用Shell自动监控tomcat并且执行重启操作
- java实现ssh登录执行shell命令及一些问题。
- Python 使用paramiko实现远程登录Linux主机并执行命令和操作
- LINUX下使用Shell自动监控tomcat并且执行重启操作
- LINUX下使用Shell自动监控tomcat并且执行重启操作
- LINUX下使用Shell自动监控tomcat并且执行重启操作 --谷营中西软件科技园
- [置顶] Linux下用expect实现ssh自动登录并执行脚本
- LINUX下使用Shell自动监控tomcat并且执行重启操作
- shell实现ssh自动登录远程主机
- linux操作之:设置系统启动时,自动启动一些程序,或者执行一些命令
- 用了一天的时间,linux下expect实现ssh自动登录服务器记,鄙视下网上各种抄来抄去残段子
- linux系统中中php实现ssh自动登录
- Linux下实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本分享
- shell脚本实现ssh自动登录功能分享
- linux 下ssh自动登录到其他主机
- Linux下实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本分享
- shell脚本实现ssh自动登录功能分享
- linux下两台主机通过ssh实现无密码登录