期望在ssh到设备后发送命令不起作用

时间:2019-06-01 06:46:56

标签: expect

脚本成功登录到palo-alto设备后,我需要在该设备上运行一些命令并将其输出保存在bash文件中。     #期望的send命令似乎不起作用。     在执行命令之前,我的脚本就这样退出了。

    [root@host user]# ./pa.sh 
    ++ cat list.txt
    + for i in 'cat list.txt'
    + ping -c 2 10.44.128.23
    + '[' 0 == 0 ']'
    + echo 'XX.XX.XXX.XX up and running'
    + ./pa1.sh XX.XX.XX.XX
    spawn ssh -o StrictHostKeyChecking=no -l user XX.XX.XX.XX
    Password: 
    Last login: Fri May 31 22:37:16 2019 from sec01

Number of failed attempts since last successful login: 0


user@pa200> show ses[root@host]

pa1.sh-

`#!/usr/bin/expect -f

set timeout 120
set IPadress [lindex $argv 0]
set Username "user"
set Password "pass"
log_file -a ~/results.log
spawn ssh -o StrictHostKeyChecking=no -l $Username $IPadress
expect "Password:"
        send "$Password\r"
expect "*>"
set output [open "outputfile.txt" "a+"]
send "show session info\r"
set outcome $expect_out(buffer)
puts $output $outcome
close $output
expect "\n"
send "exit\r"

bash脚本:

#!/bin/bash
set -x
for i in `cat list.txt`
do
ping -c 2  $i >/dev/null 2>&1;
if [ $? == 0 ]; then
echo "$i up and running" >> /home/user/uphost.txt
./pa1.sh $i
else
echo "$i is down" 
fi
done`

0 个答案:

没有答案