欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

linux 使用expect执行多条命令 有大用 有大大用

使用expect执行多条命令


#!/bin/bash 
ip=$1  
user=$2 
password=$3 

expect <<EOF  
    set timeout 30
    spawn ssh $user@$ip 
    expect { 
        "yes/no" { send "yes\n";exp_continue } 
        "password" { send "$password\n" }
    } 
    expect "]#" { send "useradd zs\n" } 
    expect "]#" { send "touch /opt/zs.txt\n" } 
    expect "]#" { send "exit\n" } 
expect eof EOF


来自  https://www.cnblogs.com/frantz/p/14098233.html



linux expect执行多条命令

在 Linux 中,使用 Expect 脚本可以实现自动执行多条命令。Expect 是一种 Tcl 脚本语言,可以通过交互式的方式控制其他命令行程序。

下面是一个简单的 Expect 脚本,用于执行多条命令:

复制代码#!/usr/bin/expect

spawn ssh user@host

expect "password:"
send "your_password\r"

expect "*$"
send "command1\r"

expect "*$"
send "command2\r"

expect "*$"
send "exit\r"

interact

在这个脚本中,我们首先使用 spawn 命令登录到远程主机。然后,使用 expect 命令等待特定的输出,例如 "password:",并通过 send 命令发送相应的输入,例如密码。最后,使用 interact 命令允许我们与远程主机进行交互,直到退出。

请注意,本示例仅供参考,在实际使用中需要根据实际情况进行修改。


来自   https://juejin.cn/s/linux%20expect%E6%89%A7%E8%A1%8C%E5%A4%9A%E6%9D%A1%E5%91%BD%E4%BB%A4



普通分类: