shell 期望从pfsense路由器得到脚本多行响应

dzjeubhm  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(109)

我正试图写一个期望脚本登录到一个pfsense路由器,并运行一些命令。我似乎无法正确匹配来自pfsense设备的多行响应,并以运行autowexpect并从那里返回时看到响应的方式正确回答它们。我认为我的脚本是在传递密码,但大墙的文字,以'输入一个选项结束:登录后看到的“”未匹配。这就是我希望运行的:

# Main script
set prompt {[$>?#]: }
puts "Connecting to pfSense router..."
spawn ssh -p $ssh_port -o StrictHostKeyChecking=no $username@$pfsense_router
expect {
    -re "assword.*:" {
        send "$password\r"
        expect $prompt
#        exp_continue
    }
   -re ".*Enter an option: " {
            send "8\r"
            expect $prompt
#            exp_continue
       }
    "/root:.*" {
        puts "SSH login successful."
        check_gateway_status $target_host
    }
    "Permission denied" {
        send_user "Authentication failed. Please check your credentials.\n"
        exit 1
    }
    timeout {
        send_user "Timeout while trying to connect to the router.\n"
        exit 1
    }
}

字符串
为了匹配看起来像这样的文本:

ssh $pfsense_ip -p 3456
Password for root@$pfsense_hostname:
KVM Guest - Netgate Device ID: fa1a75cc70c259bca849

*** Welcome to pfSense 2.6.0-RELEASE (amd64) on edge1 ***

 WAN (wan)       -> vtnet3     -> v4: $pfsense_ip/29
 LAN (lan)       -> vtnet1     -> v4: 10.0.192.253/24
 OPT1 (opt1)     -> vtnet2     -> v4: 10.0.182.253/24
 OPT2_FORMER_192_168_2_244 (opt2) -> vtnet0     ->
 VTI01 (opt3)    -> ipsec9     -> v4: 10.6.106.1/30

 0) Logout (SSH only)                  9) pfTop
 1) Assign Interfaces                 10) Filter Logs
 2) Set interface(s) IP address       11) Restart webConfigurator
 3) Reset webConfigurator password    12) PHP shell + pfSense tools
 4) Reset to factory defaults         13) Update from console
 5) Reboot system                     14) Disable Secure Shell (sshd)
 6) Halt system                       15) Restore recent configuration
 7) Ping host                         16) Restart PHP-FPM
 8) Shell

Enter an option:


我该如何正确地做到这一点?

uidvcgyl

uidvcgyl1#

我假设在您输入8以获取shell之后,这就是您想要运行更多命令的地方。
我会从

spawn ssh -p $ssh_port -o StrictHostKeyChecking=no $username@$pfsense_router

expect {
    -re "assword.*:" {
        send "$password\r"
        exp_continue
    }
    "Permission denied" {
        send_user "Authentication failed. Please check your credentials.\n"
        exit 1
    }
    timeout {
        send_user "Timeout while trying to connect to the router.\n"
        exit 1
    }
    -re {Enter an option:\s*$} {
        send "8\r"
    }
}
expect -re $prompt

# now start sending commands and expecting the prompt.

字符串

相关问题