现场服务器较多,密码3个月过期,在到期时需更改密码。

  使用expect编写,尝试登陆2次后退出(防止密码错误时账号锁定),超时重试一次。

  shell脚本调用并定时执行,登陆成功后执行一条命令,如:hostname、uname等,根据退出状态判断密码是否到期。

  0--正常

  1--传入参数错误

  2--timeout

  3--密码错误或到期

  脚本如下: 

#!/usr/bin/expect

###############################################################
# 连接远程主机
proc do_login {passwd} {
set timeout
set done
set timeout_case
set ps1 {PS1="doraemon#";export PS1}
while {$done<} {
expect {
*assword* {
send $passwd\r
incr done
}
\[$%>#] {
set done
send $ps1\r\r
break
}
timeout {
set done
switch -- $timeout_case {
{ send_user "try again ...\n" }
{ exit
expect eof
}
}
incr timeout_case
}
}
}
if {$done==} {
exit
expect eof
}
}
###############################################################
# 执行命令
proc exec_cmd {cmd} {
expect -re "doraemon#$"
send_user "\$cmd: $cmd\n"
send $cmd\r
}
###############################################################
# 退出
proc logout {} {
expect -re "doraemon#$"
send exit\r
expect eof
}
###############################################################
if {$argc < } {
send_user "Usage:$argv0 user pass ip command\n"
exit
}
set user [lindex $argv ]
set pass [lindex $argv ]
set ip [lindex $argv ]
set num spawn ssh -o StrictHostKeyChecking=no -l $user $ip
do_login $pass
while {$num<$argc} {
set proc [lindex $argv $num]
# 捕获输出用
set cmd "echo ==$ip==$user==`$proc`=="
exec_cmd $cmd
incr num
}
logout

  测试输出:

[root@centos1 shcript]# ./logon.expect
Usage:./logon.expect user pass ip command
[root@centos1 shcript]# ./logon.expect root root123 127.0.0.1 hostname
spawn ssh -o StrictHostKeyChecking=no -l root 127.0.0.1
root@127.0.0.1's password:
Last login: Fri Aug :: from 127.0.0.1
[root@centos1 ~]# PS1="doraemon#";export PS1
doraemon#
doraemon#$cmd: echo ==127.0.0.1==root==`hostname`==
echo ==127.0.0.1==root==`hostname`==
==127.0.0.1==root==centos1==
doraemon#exit
logout
Connection to 127.0.0.1 closed.
[root@centos1 shcript]# echo $?

Shell登陆远程服务器的更多相关文章

  1. ssh免密码登陆远程服务器

    ssh免密码登陆远程服务器 在使用windows下的cygwin或者在linux下使用Terminal进行远程服务器登陆测试的时候总是会要求输入账号密码,对于此我们可以使用ssh将公钥放在服务器上的方 ...

  2. 通过expect免自动输入密码登陆远程服务器

    通过expect免自动输入密码登陆远程服务器 1.前提必须已经安装expect 2.新建login.sh,文件内容如下 #!/usr/bin/expect -f spawn ssh root@140. ...

  3. Linux 系统免密码登陆远程服务器

    在当前Linux生成公钥私钥ssh-keygen公钥私钥存放路径为:~/root/.ssh/id_rsa id_rsa.pub 登陆被远程主机,进入目录~/root/.ssh/是否存在authoriz ...

  4. [ SHELL编程 ] 远程服务器传输文件

    在shell编程中经常需要获取远程服务器文件.手工操作中使用scp命令完成.为避免脚本执行scp输入密码进行交互,需先建立本机服务器当前用户和远程服务器指定用户的信任关系.具体代码见操作实例,重点关注 ...

  5. linux expect自动登陆远程服务器 批量管理服务器

    #!/usr/bin/expect set ipaddress [lindex $argv 0] set passwd [lindex $argv 1] set timeout 3 spawn ssh ...

  6. 登陆远程服务器的Tomcat 8的manger的页面403

    访问远程服务器Tomcat 8的管理页面报错 在远程服务器上安装了一个tomcat8.5,配置好用户后重新启动tomcat ,发现:8080可以访问,但是进入:8080/manager/html页面报 ...

  7. linux ssh免密登陆远程服务器

    10.170.1.18服务器免密登录到10.170.1.16服务器 首先登入一台linux服务器(10.170.1.18),此台做为母机(即登入其他linux系统用这台做为入口):执行一行命令生成ke ...

  8. xmanger图形化登陆远程服务器

    由于网上的资料比较杂,经过本人整理实际操作验证,保证ok  本人的服务器系统为centos5.8 下面的都是centos服务器上的操作,需要简单的配置下: win客户端使用xmanger软件:首先是服 ...

  9. 【转】使用shell登录远程服务器执行多条命令,ssh登录之后执行脚本文件

    原文:https://blog.csdn.net/qq_36622490/article/details/100773589 这个需求主要是我在jenkins中pipeline的代码里,需要使用she ...

随机推荐

  1. 如何导出ane所需的swc

    来源:http://blog.sina.com.cn/s/blog_6471e1bb01012ard.html 打包.ane文件的时候需要用到ActionScript的扩展库(swc文件),那么如何生 ...

  2. Step by Step to create orders by consuming SAP Commerce Cloud Restful API

    Recently Jerry is working on an integration project about creating orders in Wechat platform by cons ...

  3. Windows环境下大数据处理的构想(一)

    为什么不呢?我们有了RPC/RMI和MAP,为什么不能在windows环境下处理大数据呢?windows是迄今为止最普及的操作系统,据市调公司NetMarketShare最新(2019年5月)统计数据 ...

  4. c# 计算目录的大小

  5. KVM虚拟化之嵌套虚拟化nested

    本文测试物理机为centos6.5 物理机使用Intel-V虚拟化架构,安装qemu-kvm版本0.12 我们知道,在Intel处理器上,KVM使用Intel的vmx(virtul machine e ...

  6. 使用Junit测试框架学习Java

    前言 在日常的开发中,离不开单元测试,而且在学习Java时,特别是在测试不同API使用时要不停的写main方法,显得很繁琐,所以这里介绍使用Junit学习Java的方法.此外,我使用log4j将结果输 ...

  7. HTML&CSS基础-常用选择器

    HTML&CSS基础-常用选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!DOCTYPE html> <html> & ...

  8. Spring Boot SockJS应用例子

    1.SockJS用javascript实现的socket连接,兼容各种浏览器的WebSocket支持库2.WebSocket是H5的,不支持H5的浏览器没法使用.3.SockJS它提供类似于webso ...

  9. LG4720 【模板】扩展卢卡斯定理

    扩展卢卡斯定理 求 \(C_n^m \bmod{p}\),其中 \(C\) 为组合数. \(1≤m≤n≤10^{18},2≤p≤1000000\) ,不保证 \(p\) 是质数. Fading的题解 ...

  10. python爬虫——爬取网页数据和解析数据

    1.网络爬虫的基本概念 网络爬虫(又称网络蜘蛛,机器人),就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序.只要浏览器能够做的事情,原则上,爬虫都能够做到. 2 ...