user212051
user212051

Reputation: 93

issue in expect code on Linux server

I am trying to ssh server using the below script

exp_internal 1

    #!/usr/bin/expect

    grep "$1" serverlist.txt
    if [ $? = 0 ]
            then
            eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no ghayat@$server_ip
            expect "password: "
            send "Password\r"
            expect "$ "
            send "touch Deepo22_9\r"
            expect "$ "
            send "exit\r"
    interact
    fi

but I received the below errors:

[root@Server_name DMSSH]# ./testscript.sh $server_ip
./testscript.sh: line 1: exp_internal: command not found
172.24.194.50
./testscript.sh: line 8: spawn: command not found
couldn't read file "password: ": no such file or directory
./testscript.sh: line 10: send: command not found
couldn't read file "$ ": no such file or directory
./testscript.sh: line 12: send: command not found
couldn't read file "$ ": no such file or directory
./testscript.sh: line 14: send: command not found
./testscript.sh: line 15: interact: command not found

The code is correct without grep & if, So How can I add grep& if corrctly ?

Also I am trying to pass server ip while executing script "$1" to check in DB.txt file first before running ssh, but it seems not working as well.

Upvotes: 1

Views: 371

Answers (2)

glenn jackman
glenn jackman

Reputation: 247042

You probably meant to say

#!/usr/bin/expect
exp_internal 1
set server_ip [lindex $argv 0]
if {[catch {grep -q $server_id serverlist.txt} output] == 0} {
    spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no ghayat@$server_ip
    expect "password: "
    send "Password\r"
    expect "$ "
    send "touch Deepo22_9\r"
    expect "$ "
    send "exit\r"
    expect eof
}

Upvotes: 1

Etan Reisner
Etan Reisner

Reputation: 81012

You can't use grep and if inside an expect script. Those are external commands and shell constructs.

expect has its own if support if you want to use that (as expect is just tcl).

But if you want to do other things before calling your expect script you need a wrapper shell script for that.

Upvotes: 1

Related Questions