linux中spawn远程执行,Linux 远程执行命令,expect

本地交互执行:

1. 修改shell

#!/usr/bin/expect

set USER [lindex $argv 0]

set SHELL [lindex $argv 1]

set timeout 3

spawn chsh $USER

expect "*]:*" { send "$SHELL\r" }

expect eof

# ./chsh.sh user1 /bin/tcsh

2. 修改密码

#!/usr/bin/expect

set USER [lindex $argv 0]

set PASS "1q2w#E\$R"

set timeout 3

spawn passwd $USER

expect "*Password:*" { send "$PASS\r" }

expect "*Password:*" { send "$PASS\r" }

expect eof

# ./pass.sh user1

或把用户和密码都作为参数

#!/usr/bin/expect

set USER [lindex $argv 0]

set PASS [lindex $argv 1]

set timeout 3

spawn passwd $USER

expect "*Password:*" { send "$PASS\r" }

expect "*Password:*" { send "$PASS\r" }

expect eof

# ./pass.sh ttt 1q2w#E$R

# ./pass.sh ttt "1q2w#E\$R"

总结:expect 必须要匹配最后一个输出字符。

远程交互ssh 登录:

1. 设置变量,执行多命令。

#!/usr/bin/expect

set IP "10.85.138.42"

set timeout 3

spawn ssh ${IP}

expect "*yes/no*" { send "yes\r" }

expect "*assword*" { send "root\r"}

expect "*#*" { send "ls\r" }

expect "*#*" { send "touch /tanjiyong/newfile\r" }

expect eof

#./exp.sh

2. 增加参数。

#!/usr/bin/expect

set IP [lindex $argv 0]

set USER [lindex $argv 1]

set timeout 3

spawn ssh $USER@${IP}

expect "*yes/no*" { send "yes\r" }

expect "*assword*" { send "root\r"}

expect "*#*" { send "ls\r" }

expect "*#*" { send "touch /tanjiyong/newfile\r" }

expect eof

#./exp.sh 10.85.138.42 root

3. ssh 登录,执行时间超过timeout时间,设定timeout为-1(无限制)。

#!/usr/bin/expect

set IP [lindex $argv 0]

set USER [lindex $argv 1]

set IP2 [lindex $argv 2]

set timeout 3

spawn ssh $USER@${IP}

expect "*yes/no*" { send "yes\r" }

expect "*assword*" { send "root\r"}

expect "*#*" { send "ls\r" }

expect "*#*" { send "ping $IP2\r" }

set timeout -1

expect "*#*" { send "exit 1\r" }

expect eof

4. ssh 登录,使用循环,在30台机器执行相同命令。

#!/usr/bin/expect

set USER root

set PASS root

for {set i 1} {$i<=30} {incr i} {

spawn ssh -l $USER 125.1.1.$i

expect "*yes/no*" { send "yes\r" }

expect "*assword*" { send "$PASS\r"}

expect "*#*" { send "find / -name hao.txt\r" }

expect eof

}

#./exp.sh

本地远程交互执行:

1. spawn 执行scp

#!/usr/bin/expect

set PASS root

set timeout 3

spawn scp /etc/passwd root@10.85.138.42:/tanjiyong

expect "*yes/no*" { send "yes\r" }

expect "*Password:*" { send "$PASS\r" }

set timeout -1

expect "*#*" { send "exit 1\r" }

# ./scp.sh

使用-- send

#!/usr/bin/expect --

set PASS root

set USER root

set IP 10.85.138.42

set env(SHELL) /bin/bash

set timeout 1

spawn $env(SHELL)    #spawn /bin/bash

expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }

expect "*yes/no*" { send "yes\r" }

expect "*Password:*" { send "$PASS\r" }

set timeout -1  #复制的时间较长,设置为timeout无限制

expect "*#*" { send "exit 1\r" }

#!/usr/bin/expect

set PASS root

set USER root

set IP 10.85.138.42

set env(SHELL) /bin/bash

set timeout 1

spawn $env(SHELL)

expect "*#*" { send -- "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }

expect "*yes/no*" { send "yes\r" }

expect "*Password:*" { send "$PASS\r" }

set timeout -1  #复制的时间较长,设置为timeout无限制

expect "*#*" { send "exit 1\r" }

#!/usr/bin/expect

set timeout 3

set env(SHELL) /bin/bash

spawn \$env(SHELL)

expect -exact "# "

send -- "scp $TAR_NAME ${USERNAME}@${DESTIP}{DESTDIR}\r"

expect {

"*yes/no*" { send "yes\r";exp_continue }

"assword: " { send "hw2009\r" }

}

set timeout -1  #复制的时间较长,设置为timeout无限制

expect "# "

send "exit\r"

expect eof

2. expect 搜索块

#!/usr/bin/expect

set PASS root

set timeout 3

spawn scp /etc/passwd root@10.85.138.42:/tanjiyong

expect {

"*yes/no*" { send "yes\r";exp_continue }

"assword: " { send "$PASS\r" }

}

set timeout -1

expect "*#*" { send "exit 1\r" }

# ./scp.sh

3. 判断

#!/usr/bin/expect

set PASS [lindex $argv 0]

set USER root

set IP 10.85.138.42

set env(SHELL) /bin/bash

set timeout 1

spawn $env(SHELL)

expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }

expect {

"*yes/no*" { send "yes\r" }

"*Password:*" { send "$PASS\r" }

}

expect "*Password:*" { send_user "\nPasswd error!\n";exit 1 }

set timeout -1

expect "# " { send "exit 1\r" }

expect eof

# ./scp.sh

4. rsync 备份使用。

#!/usr/bin/expect --

spawn ssh backup@10.85.138.212

expect {

"(yes/no)?" {

send "yes\r"

}

"assword" {

send "123456\r"

}

}

send "rsync -avz rsync@10.85.138.212:/home/html /opt\r"

expect "total size"

expect {

"rsync error" {

exit 1

}

}

expect "# "

send "exit\r"

interact  #expect eof

脚本中使用:

#!/bin/bash

echo "Start..."

cat << EOF > /expectfile

#!/usr/bin/expect

set PASS root

set USER root

set IP 10.85.138.42

set env(SHELL) /bin/bash

set timeout 1

spawn \$env(SHELL)  #必须加上\,不然会被置换为空。

expect "*#*" { send "/usr/bin/scp /etc/passwd \$USER@\$IP:/tanjiyong\r" }

expect "*yes/no*" { send "yes\r" }

expect "*Password:*" { send "\$PASS\r" }

set timeout -1

expect "# " { send "exit 1\r" }

expect eof

EOF

expect -f /expectfile

echo "$?"

echo "finished backup.."

附:修改SSH Client为非ask模式

vi /etc/ssh/ssh_config

#   StrictHostKeyChecking ask

StrictHostKeyChecking no

参考:自动登录

#!/usr/bin/expect

if {$argc!=3} {   # 疑问:参数个数($#)

send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"

exit

}

set IP [lindex $argv 0]

set Password [lindex $argv 1]

set CMD [lindex $argv 2]

spawn ssh admin@$IP

expect {

"assword:" {

exec sleep 1

send "${Password}\r"

}

"*continue connecting*" {

exec sleep 1

send "yes\r"

expect  "*Password:" {

exec sleep 1

send "${Password}\r"

}

}

}

expect {

"*Password*" { send_user "\n1assword error!\n"

exit

}

"*already*" { send_user "\n2:Repeated login!\n"

exit

}

"OceanStor: admin>" { send "${CMD}\r"

}

}

expect         "*>"

send "exit\r"

expect "*closed*"

exit

脚本2

#!/usr/bin/expect

set ipaddress [lindex $argv 0]

set passwd [lindex $argv 1]

spawn ssh -p 22 root@$ipaddress

expect {

"want"        {send -- "yes\r"; exp_continue}

"password:"   {send -- "$passwd"}

"No route"    { exit }

}

set timeout 5

send "\n"

expect "*justin*"

send "pwd\r"

expect "*OK*"

send "exit\r"

expect eof

相关:

exit 1/exit  #停止执行,退出脚本

expect eof #脚本结束

expect -exact "# " #精确匹配

expect "# "

expect "*#*" #模糊匹配

exp_continue #在同一个expect块里,做多次匹配。

send #发送命令。

send_user #打印终端信息。用法与send一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]和\[2\]提供了关于expect命令的详细说明。expect是一个用于自动化交互式任务的工具,可以通过编写expect脚本来实现自动化操作。它可以与交互式程序进行交互,并根据程序的输出进行相应的操作。 expect命令的一些常用参数包括: - spawn:启动一个交互式程序,并执行后面的命令或程序。 - set timeout n:设置超时时间,表示该脚本代码需在n秒钟内完成,如果超过,则退出。这可以防止在网络不可达或远程主机执行命令时卡住。 - expect:从交互式程序指定接收信息,如果匹配成功,则执行send指令进行交互;否则等待timeout秒后自动退出expect语句。 - send:如果匹配到expect接收到的信息,就将send的指令进行交互传递,执行相应的动作。 - exp_continue:表示循环式匹配,可以不断循环匹配,输入多条命令,简化写法。 - exit:退出expect脚本。 - expect eof:表示进程结束后会向expect发送eof,接收到eof代表该进程结束。 - interact:执行完代码后保持交互状态,将控制权交给用户。 通过编写expect脚本,可以实现自动化操作,如自动执行git pull命令或自动登录ssh并执行命令等。具体的脚本示例可以参考引用\[3\]的实例展示。 总结起来,expect命令是一个用于自动化交互式任务的工具,可以通过编写expect脚本来实现自动化操作,包括与交互式程序的交互、设置超时时间、发送指令等。 #### 引用[.reference_title] - *1* [Linux学习之expect操作详解](https://blog.csdn.net/jh035/article/details/127977231)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Linuxexpect命令,以及实战使用!!!](https://blog.csdn.net/m0_53396354/article/details/125511170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值