现场服务器较多,密码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. 修改redhat7默认显示语言从中文为英文

    [delmore@localhost Desktop]$ su                               //切换到最高权限 Password:                    ...

  2. FastDFS+Nginx搭建Java分布式文件系统

    一.FastDFS FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS ...

  3. 一文看懂Java Worker 设计模式

    Worker模式 想解决的问题 异步执行一些任务,有返回或无返回结果 使用动机 有些时候想执行一些异步任务,如异步网络通信.daemon任务,但又不想去管理这任务的生命周.这个时候可以使用Worker ...

  4. DRF 有无外键操作实例

    models.py from django.db import models # Create your models here. class Category(models.Model): &quo ...

  5. navicat for mysql 链接时报错:1251-Client does not support authentication protocol requested by server

    客户端使用navicat for mysql.本地安装了mysql 8.0.但是在链接的时候提示: 主要原因是mysql服务器要求的认证插件版本与客户端不一致造成的. 打开mysql命令行输入如下命令 ...

  6. Android笔记(四十二) Android中的数据存储——SQLite(四)update

    update方法的四个参数: update()方法参数 对应的sql部分 描述 table update table_name 更新的表名 values set column=xxx ContentV ...

  7. h5中hash的用法实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...

  9. win10安装Pytorch【最新版】

    由于2019年4月16日和25日清华和中科大分别宣布停止Anaconda镜像服务,因此从2019年5月开始安装Pytorch都会出现一下错误: CondaHTTPError: HTTP 404 NOT ...

  10. Django之路——5 Django的模板层

    你肯能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...