Shell脚本之expect

linux远程登录,此脚本可直接进行测试ip、username、password替换正确的即可


#!/usr/bin/expect

# set:设置变量
set timeout 30
set host "172.17.0.5"
set username "root"
set password "test@123"

# spawn:启动新的进程

spawn ssh $username@$host

# expect:从进程接收字符串(捕获字符串:*password*),\r字段,表示换行
expect "*password*" {send "$password\r"}
expect "*connecting*" {send "yes\r"}

# send:用于向进程发送字符串
send "touch test.txt\r\n"
send "touch test1.txt\r\n"

# interact:允许用户交互
interact

# 执行完需要等一会
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容