使用expect控制ssh自动登录连接Linux服务器方法

2013年12月24日

关键字:ssh自动输入密码 ssh输入密码 ssh免密码登录linux ssh一键登录脚本
原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/1667.html https://www.myzhenai.com/thread-16105-1-1.html
因为我自己有几台Linux的服务器需要管理,有时候管理的时候需要ssh手动登录并执行相应代码,操作非常繁锁,于是想到自动化脚本,写一个一键管理的脚本,以后只要运行相应的脚本就可以管理对应的服务器,这样的效率就会高很多了,本来Linux上有一个shell脚本,但是执行它还是需要手动登录ssh,能不能自动登录ssh呢? 我查找了网上很多资料,有的人说用ssh-keygen管理方便,但缺点是需要在本地保存Key验证码.于是便选择了expect来实现.方法也非常简单.

expect是Unix系统中用来进行自动化控制和测试的软件工具,由Don Libes制作,作为Tcl脚本语言的一个扩展,应用在交互式软件中如telnet,ftp,Passwd,fsck,rlogin,tip,ssh等等。该工具利用Unix伪终端包装其子进程,允许任意程序通过终端接入进行自动化控制;也可利用Tk工具,将交互程序包装在X11的图形用户界面中。

我们通过Shell可以实现简单的控制流功能,如:循环、判断等。但是对于需要交互的场合则必须通过人工来干预,比如普通用户使用sudo命令时就需要我们手动输入密码;expect就是能够完成这种自动交互任务,而无需人的干预。Expect的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:Expect是一个用来实现自动交互功能的软件套件(Expect [is a] software suite for automating interactive tools)。系统管理员可以使用它创建用来实现对命令或程序提供输入的脚本:一般来说这些输入都需要手工输入(比如之前提到的执行sudo程序期望用户从终端输入用户密码)进行的,Expect则可以根据程序的提示 模拟标准输入给程序提供信息来实现交互程序执行。
expect和sh的shell脚本不一样,expect是以 #!/usr/bin/expect 开始,而sh则是以# !/bin/bash开始.使用之前需要安装expect. 这里用到的只是expect的几个命令,expect,spawn和send. 回车符号分别是\r \n

#yum install expect*

 

新建一个shell脚本文件,以.sh为后缀名的文件.例如:AutoLogin.sh 然后将下边的内容保存到该文件中.

#chmod +x AutoLogin.sh
#./AutoLogin.sh

 
切记expect脚本的执行方式与sh的执行方式不一样,不能用 sh AutoLogin.sh 来执行,否则会出现以下的错误.

AutoLogin.sh: line 3: spawn: command not found
couldn't read file "password:": no such file or directory
AutoLogin.sh: line 5: send: command not found
AutoLogin.sh: line 6: interact: command not found

 

#!/usr/bin/expect
set timeout 30
spawn ssh -p 1238 110.110.110.110
expect "password:*"
send "0123456789\r"
expect "*]#"
send "rm -rf /usr/local/lxlabs/kloxo/serverfile/tmp/*\n"
expect "*]#"
send "rm -rf /tmp/*\n"
expect "*]#"
send "rm -f /home/admin/__processed_stats/*\n"
expect "*]#"
send "rm -rf /home/kloxo/httpd/lighttpd/*\n"
expect "*]#"
send "rm -rf /var/log/kloxo/*\n"
expect "*]#"
send "rm -f /home/httpd/*/stats/*log\n"
expect "*]#"
send "mysqlcheck -Ao -uroot -p0123456789\n"
expect "*]#"
send "echo 3 > /proc/sys/vm/drop_caches\n"
expect "*]#"
send "echo 0 > /proc/sys/vm/drop_caches\n"
expect "*]#"
send "echo 1 > /proc/sys/vm/drop_caches\n"
expect "*]#"
send "swapoff -a\n"
expect "*]#"
send "swapon -a\n"
expect "*]#"
send "yum clean all\n"
expect "*]#"
send "service mysqld restart\n"
expect "*]#"
send "service lighttpd restart\n"
expect "*]#"
send "exit\n"
expect eof

 

#!/usr/bin/expect
set timeout 30
#把超时时值设定为30秒
spawn ssh -p 1238 110.110.110.110
#使用ssh连接服务器,如果端口不是默认的22,则使用-p参数加上端口号,1238是端口号,110.110.110.110是服务器IP地址.
expect "password:*"
#提示输入密码
send "0123456789\r"
#输入密码,\r是回车
expect "*]#"
#返回操作符,如果不加这个会自动断开.
send "rm -rf /usr/local/lxlabs/kloxo/serverfile/tmp/*\n"
expect "*]#"
send "rm -rf /tmp/*\n"
expect "*]#"
send "rm -f /home/admin/__processed_stats/*\n"
expect "*]#"
send "rm -rf /home/kloxo/httpd/lighttpd/*\n"
expect "*]#"
send "rm -rf /var/log/kloxo/*\n"
expect "*]#"
send "rm -f /home/httpd/*/stats/*log\n"
#以上这几个是清除服务器临时文件或垃圾文件代码.
expect "*]#"
send "mysqlcheck -Ao -uroot -p0123456789\n"
#优化mysql数据库代码,0123456789是mysql数据库超级管理员密码.
expect "*]#"
send "echo 3 > /proc/sys/vm/drop_caches\n"
expect "*]#"
send "echo 0 > /proc/sys/vm/drop_caches\n"
expect "*]#"
send "echo 1 > /proc/sys/vm/drop_caches\n"
expect "*]#"
#以上这几行是清除服务器内存代码.
send "swapoff -a\n"
expect "*]#"
send "swapon -a\n"
expect "*]#"
send "yum clean all\n"
expect "*]#"
send "service mysqld restart\n"
#重启mysql数据库服务
expect "*]#"
send "service lighttpd restart\n"
#重启lighttpd编译器服务.
expect "*]#"
send "exit\n"
#断开与服务器的连接.
expect eof

 
当然也可以在linux服务器上创建一个shell脚本文件,例如OptimizationTools.sh 然后将所有操作代码都写在这里.例如将以下内容保存到OptimizationTools.sh文件里. 路径是/home/OptimizationTools.sh 那么,我们只需要在expect脚本里执行 send “sh /home/OptimizationTools.sh\n” 就可以了,就不需要把相应的代码写在expect脚本里.这样代码就简洁一些.

# Linux CentOS Server Shell Tools
# !/bin/bash
# Clear system temporary files and junk files. .
rm -rf /usr/local/lxlabs/kloxo/serverfile/tmp/*
rm -rf /tmp/*
rm -f /home/admin/__processed_stats/*
rm -rf /home/kloxo/httpd/lighttpd/*
rm -rf /var/log/kloxo/*
rm -f /home/httpd/*/stats/*log
# Mysql database optimization
mysqlcheck -Ao -uroot -p0123456789
# Clear system memory .
echo 3 > /proc/sys/vm/drop_caches
echo 0 > /proc/sys/vm/drop_caches
echo 1 > /proc/sys/vm/drop_caches
swapoff -a
swapon -a
yum clean all
# Restart the service component .
service mysqld restart
service lighttpd restart
echo '*********************************************************';
echo '****                                                 ****';
echo '****        cao zuo jie shu                          ****';
echo '****        ming ling yi jing zhi xing wan cheng .   ****';
echo '****                                                 ****';
echo '****     https://www.myzhenai.com.cn                  ****';

 

#!/usr/bin/expect
set timeout 30
spawn ssh -p 1238 110.110.110.110
expect "password:*"
send "0123456789\r"
expect "*]#"
send "sh /home/OptimizationTools.sh\n"
expect "*]#"
send "exit\n"
expect eof

 

expect 控制 ssh 自动 登录 连接 Linux 服务器 方法

使用expect控制ssh自动登录连接Linux服务器方法


expect 控制 ssh 自动 登录 连接 Linux 服务器 方法

使用expect控制ssh自动登录连接Linux服务器方法


sicnature ---------------------------------------------------------------------
Your current IP address is: 3.91.8.23
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://myzhenai.com.cn/post/1667.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注