Calling a function which uses expect from a shells script

Hi all,

This is the first time i am using expect.

I am trying to call a function with in the shell script. The function will shh to a new server and will pass the password using expect and send. I need help in calling the fuction i am getting follaowing errors...

here the script

#!/usr/bin/ksh
 
check_status() {
echo $1
spawn ssh user@$1
expect "user's Password:"
send "pwd\r"
expect "user@sv1"
send "uname -a\r"
send "/app/status.sh \r"
interact
}
 
 

check_status server ;

I am getting the following error.

user@server$ ./checkFunc.sh
server
./checkFunc.sh[4]: spawn: not found.
 

Thanks,
Firestar

Maybe this:

expect <<END >> ./expectExec.log
	spawn ssh user@$1
	expect "user's Password:"
	send "pwd\r"
	expect "user@sv1"
	send "uname -a\r"
	send "/app/status.sh \r"
	interact
END

Or put the script in another file, and:

#!/<path to>/expect -f
spawn ssh user@$1
expect "user's Password:"
send "pwd\r"
expect "user@sv1"
send "uname -a\r"
send "/app/status.sh \r"
interact

Check this link: Expect - Wikipedia, the free encyclopedia . You will find a SSH example on it!

I hope it helps!

You needed third-party hacking tools to inject passwords into ssh because ssh -- and any sane login like sudo, su, and so forth -- are all explicitly designed to prevent this. It's a small helpful hint, written in mile-high flashing neon letters, that it's a really bad idea.

This is because recoverable text passwords are incredibly difficult to keep safe. Passing them through variables, files, or arguments opens them up to interception in a huge variety of ways.

ssh has a much better alternative built right in: keys. Just having the right files in the right places lets ssh go through fully automatically with no modification to your program. See passwordless ssh.

@felipe.vinturin

Thank you the reply..

when i keep the script in a new ans try to execute the expect script is exiting after send "uname -a\" command. I couldn't find why this is happeneing ...

any ideas..

If you use ssh keys as intended instead of bruteforcing the password with third-party hacking tools, your program will go straight into ssh with no mysterious hangs or premature exits.

ssh username@host uname -a ';' /app/status.sh

@felipe.vinturin

Or can you help in writitng a while loop in the expect script so that the while will read a text file line by line and execute the script once per line.
some thing like this

while read line
do
{ script }
done < file.txt

If you use ssh keys as intended instead of insecurely kludging plaintext passwords with third-party brute-forcing tools, you can directly feed entire shell scripts into ssh and get them executed on the remote end.

ssh username@host /bin/sh -s arg1 arg2 <<"EOF"
echo "arg1 is $1"
echo "arg2 is $2"

for N in 1 2 3 4 5
do
        echo $N
done
EOF

@corona688

I have 38 servers .. how can I easily set the ssh keys for all the servers for password less logins.. and we have policy that passowds change for every 90 days ... will that effect the rsa keys ( do i need to create new again everytime i change my passwords)

populating authorized_keys with expect

You won't need to create new keys every time your password expires.

If they demand a password for the key, that's also possible. I have a ssh key which I use for passwordless authentication to my collection of servers. Using ssh-agent I can type the password in once at the beginning of the day, login to my servers passwordlessly as much as I like, then kill the agent and logout at the end of the day. If you need to change the password, it's a matter of changing the password for your own key, not changing the keys on 40 servers.

---------- Post updated at 01:57 PM ---------- Previous update was at 01:51 PM ----------

better way than the perl script, I think.