Shell脚本实现自动输入密码登录服务器 | 您所在的位置:网站首页 › 自动打开网页并登录脚本 › Shell脚本实现自动输入密码登录服务器 |
原文地址::https://blog.csdn.net/lufeisan/article/details/53488395 相关文章 1、Shell脚本实现自动输入密码登录服务器----https://www.jb51.net/article/62349.htm 2、如何在linux下shell编写脚本中模拟键盘输入----https://blog.csdn.net/quicmous/article/details/77602810 3、shell实现自动回车输入----https://blog.csdn.net/weixin_34199335/article/details/92545192?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param 4、shell read 交互键入回车默认输入内容----https://blog.csdn.net/Jimmy_xmb/article/details/84837568?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param 5、shell脚本增加回车符----https://blog.csdn.net/Evils798/article/details/7202778?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param 6、echo -n 和echo -e 参数意义----https://blog.csdn.net/qq_37595946/article/details/77962963 7、echo -e 命令详解----http://eleaction01.spaces.eepw.com.cn/articles/article/item/183171 8、SHELL脚本自动登录远程主机执行脚本或命令----https://blog.csdn.net/qq_33195791/article/details/89552596 9、教你如何在Linux中通过expect工具实现脚本的自动交互----https://blog.csdn.net/Linuxprobe18/article/details/100532764
这里我们主要讲使用expect实现自动输入功能: 安装: CentOS下安装命令,如下: yum install expect 开始之前的一个简单实例: #!/usr/bin/expect spawn ssh [email protected] expect "*password:" send "123\r" expect "*#" interact
Expect是一个用来处理交互的命令。我们可以使用expect完成ftp和scp登陆的时候密码的自动输入。
expect基础 expect常用的四个命令: spawn 启动新的进程 expect 从进程接收字符串 send 用于向进程发送字符串 interact 允许用户交互 详细解释: spawn命令用来启动新的进程,spawn后的send和expect命令都是和使用spawn打开的进程进行交互。 send命令接收一个字符串参数,并将该参数发送到进程。 expect命令和send命令相反,expect通常用来等待一个进程的反馈,我们根据进程的反馈,再发送对应的交互命令。 interact命令用的其实不是很多,一般情况下使用spawn、send和expect命令就可以很好的完成我们的任务;但在一些特殊场合下还是需要使用interact命令的,interact命令主要用于退出自动化,进入人工交互。比如我们使用spawn、send和expect命令完成了ftp登陆主机,执行下载文件任务,但是我们希望在文件下载结束以后,仍然可以停留在ftp命令行状态,以便手动的执行后续命令,此时使用interact命令就可以很好的完成这个任务。
栗子: #!/bin/expect set timeout 30 set host "101.200.241.109" set username "root" set password "123456" spawn ssh $username@$host expect "*password*" {send "$password\r"} interact
这是一段非常简单的expect示例代码,演示了expect的基本使用方法。 #!/usr/tcl/bin/expect:使用expect来解释该脚本;set timeout 30:设置超时时间,单位为秒,默认情况下是10秒;set host "101.200.241.109":设置变量;spawn ssh $username@$host:spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。它主要的功能是给ssh运行进程加个壳,用来传递交互指令;expect "*password*":这里的expect也是expect的一个内部命令,这个命令的意思是判断上次输出结果里是否包含“password”的字符串,如果有则立即返回;否则就等待一段时间后返回,这里等待时长就是前面设置的30秒;send "$password\r":当匹配到对应的输出结果时,就发送密码到打开的ssh进程,执行交互动作;interact:执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。 这就是对上述这段简单简单脚本的分析,在上述的示例中,涉及到expect中一个非常重要的概念——模式-动作;即上述expect "*password*" {send "$password\r"}这句代码表达出来的含义。 模式-动作 结合着expect "*password*" {send "$password\r"}这句代码来说说“模式-动作”。简单的说就是匹配到一个模式,就执行对应的动作;匹配到password字符串,就输入密码。你可能也会看到这样的代码: expect { "password" { send "$password\r" exp_continue } eof { send "eof" } } 其中exp_continue表示循环式匹配,通常匹配之后都会退出语句,但如果有exp_continue则可以不断循环匹配,输入多条命令,简化写法。 传参 很多时候,我们需要传递参数到脚本中,现在通过下面这段代码来看看如何在expect中使用参数: #!/usr/tcl/bin/expect if {$argc < 3} { puts "Usage:cmd " exit 1 } set timeout -1 set host [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn ssh $username@$host expect "*password*" {send "$password\r"} interact 在expect中,$argc表示参数个数,而参数值存放在$argv中,比如取第一个参数就是[lindex $argv 0],以此类推。 小栗子: 1.scp 1 2 3 4 5 6 7 8 #!/bin/bash expect -c " spawn scp [email protected]:/root/1.log /root expect { \"*assword\" {set timeout 300; send \"password\r\";} \"yes/no\" {send \"yes\r\"; exp_continue;} } expect eof"
2.ssh 1 2 3 4 5 6 7 8 9 |
CopyRight 2018-2019 实验室设备网 版权所有 |