你能 运行 期待 bash shell 循环脚本中的命令吗

can you run expect command in a bash shell script for loop

目标:我在网络上有多个基于 linux 的设备。我正在尝试将密码传递给我的多个设备的用户名登录名。登录后,我想从用户那里提取信息。将该信息输出到我机器上的日志文件中。 (通常是日志文件的猫或尾巴)然后我想转到网络上的下一个设备。重复直到网络上的所有内容都已 运行 通过。

我的结果:

35:xmgEXfinder.sh:语法错误:文件结尾意外(预期“完成”)

这是我目前拥有的代码:

 #!/bin/bash
    
    IP=$(sudo arp-scan --localnet --numeric --ignoredups --quiet | grep -i ac:3f:a4 | awk '{print}')
    
    Dumpdir='/home/location/logs/'
    
    for x in $IP
    do

    /usr/bin/expect<<EOF
    
    spawn ssh -i /home/location/id_rsa root@172.17.26.$x
    expect "Enter passphrase for key 'id_rsa':"
    send "password\n"
    sleep 3
    log_file XMGcheck.log
    expect "~"
    send "cat /reg/nv/system/serial|sed \x22s/\x24/,/g\x22 ; tail -n 50 /usr/log/ams.log|grep -i xmg|wc -l \n"
    expect eof
    
    EOF
 
done

使用此处文档 (<<EOF) 时,匹配字符串必须从一行的开头开始。你有:

for x in $IP
do

    /usr/bin/expect<<EOF

    spawn ssh -i /home/location/id_rsa root@172.17.26.$x
    expect "Enter passphrase for key 'id_rsa':"
    send "password\n"
    sleep 3
    log_file XMGcheck.log
    expect "~"
    send "cat /reg/nv/system/serial|sed \x22s/\x24/,/g\x22 ; tail -n 50 /usr/log/ams.log|grep -i xmg|wc -l \n"
    expect eof

    EOF

完成

你需要:

for x in $IP
do
    /usr/bin/expect<<EOF

    spawn ssh -i /home/location/id_rsa root@172.17.26.$x
    expect "Enter passphrase for key 'id_rsa':"
    send "password\n"
    sleep 3
    log_file XMGcheck.log
    expect "~"
    send "cat /reg/nv/system/serial|sed \x22s/\x24/,/g\x22 ; tail -n 50 /usr/log/ams.log|grep -i xmg|wc -l \n"
    expect eof

EOF
done

或者您需要使用<<-EOF格式,然后使用制表符而不是空格来标识。

您可以在 bash man pageHere Documents 部分阅读更多内容。