[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

"/usr/bin/expect" -> check if login is required | speed up commands


i have many Proxmox servers with a lot of VMs. I want to use expect to connect to the VMs via serial console (qm terminal <vm_id>), and check if I need to login (expect "login: "), or if someone forgotten to logout and I have already a  prompt and I need to send an exit before and than try to login again.

My very first expect script looks like:

#!/usr/bin/expect -f
### Setting basics

# Show commands and output
log_user 1

# Set variable
set user root
set password testpass
set name [ exec hostname -f]
set prompt ": "

spawn qm terminal 101

#expect_before ": " {
#   send "exit\r"
   expect "login: "
   send "$user\r"
   expect "password:"
   send "$password\r"
   expect $prompt
   puts "---------- $name START ---------"
   expect $prompt
   send "hostname -f\r"
      expect eof
   puts $expect_out(buffer)
      expect $prompt
      send "cat /etc/debian_version\r"
      expect eof
   puts $expect_out(buffer)
      expect $prompt
      expect eof
      send "sed -i 's/root:\ nobody/root:\ sysops@example.com/g' /etc/aliases;newaliases\r"
      expect $prompt
      puts "--------- END -------"
      expect $prompt
      send "exit\r"
      expect "login:"
      send "\x1o\r"

it works, if I need to login first, but if I have a shell, I want to send an "exit" before and (re)login again.

The next point is: expect is very slow ... it seems, that there is a default delay .... every command needs 10 seconds :-) Can I remove the delay or make it shorter ?

cu denny

Reply to: