0

I'm trying to write a script using Expect for monitoring multiple servers; the connections will be through ssh using a password.

My basic loop is self failing, I'm not sure whether Expect TCL language accepts or not Linux for loop syntax.

Code:

#!/usr/bin/expect -f  
server_list="/tmp/DR.txt"  
for HOST in $(cat $server_list) ; do  
spawn ssh -q $HOST  
expect "assword:"  
send "Abcd@123\r"  
interact  
echo ' '  
echo "====For space utilization details for server $HOST===" >/tmp/space_utilization.log  
Df -kh >> /tmp/space_utilization.log  
echo ' '  
done  

The file /tmp/DR.txt contains a list of 100 server names like the following:

hzavks01  
dzavks20  
dzavks03  
uzavks05  
acavks30  

The error I get is:

[root@dzavks20 tmp]# ./expect.sh  
invalid command name "server_list=/tmp/DR"  
    while executing  
"server_list=/tmp/DR"  
    (file "./expect.sh" line 2)  
  • Tcl (and expect) variable assignment looks like set varname value -- See the Tcl tutorial, chapter 4 – glenn jackman Sep 12 '18 at 15:31
  • Also, the for loop is shell syntax, not expect. Also echo is not an expect command. – glenn jackman Sep 12 '18 at 15:33
  • Please add more details such as how you came up with the ideas and solutions present in this script. – simlev Sep 12 '18 at 15:50
2

Do you have to use expect? Can you just have a simple bash shell that runs the df command remotely via ssh and collect the information into a single file?

If ssh keys are set up correctly from the invoking server to all of the servers in your configuration file, then you do not need to enter any passwords. The following loop will collect the df output from all of the servers

while read -r h; do
    echo "=== Space utilization for ${h} ==="
    ssh "$h" df -kh
    echo ""
done </tmp/DR.txt >/tmp/space_utilization.log
  • @muku, you will want to read about the ssh-keygen and ssh-copy-id commands. – glenn jackman Sep 12 '18 at 15:33
  • @Glenm- Thanks for your time!! I'm going with expect script because i cannot make production severs as sshless and also limitation of installing utility like pssh. It will be great help if you could help me with expect..Keep learning!!! – muku Sep 13 '18 at 8:10
0

Use ssh private key authentication if you can: once you set it up, you don't need to think about it. Also you don't need to store a password in plain text.

Also, the code is a lot more complicated:

#!/usr/bin/expect -f  

set server_list /tmp/DR.txt
set fh_hosts [open $server_list r]

# truncate the log file
set log_file /tmp/space_utilization.log  
close [open $log_file w]

set pass "Abcd@123"
set prompt_re {\$ $}

while {[gets $fh host] != -1} {
    spawn ssh -q $host  
    expect "assword:"  
    send "$pass\r"  
    expect -re $prompt

    set cmd "df -kh"
    send "$cmd\r"
    expect -re "$cmd\r\n(.*)\r\n$prompt"

    log_file $log_file
    send_log "\n"
    send_log "====Space utilization for server $host at [timestamp -format +"%Y-%m-%d %T"]===\n"
    send_log -- $expect_out(1,string)
    send_log "\n\n"
    log_file

    send "exit\r"
    expect eof
}
close $fh_hosts
  • Thanks a lot !! I will check this and will update. I'm having restriction on production server using ssh private key authentication. I need to implement monitoring of production server from non-production server which is my scope. – muku Sep 13 '18 at 10:48
  • Correct fc_host variable few places then executed. Getting below below, Error: [root@hzavks20 tmp]# ./S_expect.sh spawn ssh -q file4 send: spawn id exp5 not open while executing "send "$pass\r" " ("while" body line 4) invoked from within "while {[gets $fh_hosts] != -1} { spawn ssh -q $fh_hosts expect "assword:" send "$pass\r" expect -re $prompt set cmd "df -kh..." (file "./S_expect.sh" line 13) – muku Sep 13 '18 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.