Bash/Expect Script for SSH

前端 未结 4 461
旧时难觅i
旧时难觅i 2021-01-05 06:36

I am new to Expect and scripting in general. I am trying to make a few scripts to make my life a bit easier when pulling network device configurations. I managed to create a

4条回答
  •  天涯浪人
    2021-01-05 07:22

    I would do this (untested):

    #!/usr/bin/expect -f
    
    set logfile "/home/text.cfg[clock format [clock seconds] -format %Y%m%d]"
    close [open $logfile w]         ;# truncate the logfile if it exists
    
    set ip_file "list.txt"
    set fid [open $ip_file r]
    
    while {[gets $fid ip] != -1} {
    
        spawn ssh $ip -l admin
        expect "Password:"
        send "password\r"
    
        expect "admin@"
        send "set cli pager off\r"
    
        log_file $logfile
        send "show config running\r"
    
        expect "admin@"
        log_file 
    
        send "exit\r"
        expect eof
    
    }
    close $fid
    

    Notes:

    • I removed all your comments for brevity
    • use \r to simulate hitting enter when you send commands.
    • I assumed you only want to log the "show config running" output
    • use expect eof after you send "exit"

提交回复
热议问题