Calling a function which uses expect from a shells script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling a function which uses expect from a shells script
# 1  
Old 11-03-2011
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
Code:
#!/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.
Code:
user@server$ ./checkFunc.sh
server
./checkFunc.sh[4]: spawn: not found.

Thanks,
Firestar

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 11-03-2011 at 02:03 PM.. Reason: Next time use code tags rather than fancy fonts...
# 2  
Old 11-03-2011
Maybe this:
Code:
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:
Code:
#!/<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!
# 3  
Old 11-03-2011
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.

Last edited by Corona688; 11-03-2011 at 03:37 PM..
# 4  
Old 11-03-2011
@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..
# 5  
Old 11-03-2011
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.

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

# 6  
Old 11-03-2011
@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
# 7  
Old 11-03-2011
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.

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Password check in bash script calling on expect

password check in bash script calling on expect Background: I have to copy a file from one server, to over 100 servers in a test environment. once the file is copied, it requires to have the permissions on the file changed/verified. These are all linux servers. most of them have the same... (1 Reply)
Discussion started by: 2legit2quit
1 Replies

2. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

3. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

4. Shell Programming and Scripting

Calling Expect Script - Telnet

Hi All, I have an Expect script which logs into Cisco switch, performs a show interface command. I want to read a file of ip addresses which will be passed to the expect script. The script to read the file works, the expect script works on it's own but when i call the 'expect' script from the... (12 Replies)
Discussion started by: trinak96
12 Replies

5. Shell Programming and Scripting

Calling Shell Script from Expect...

Hi there, I need some help regarding the execution of shell script from expect as the method I am trying is giving me error. I wrote an shell program which takes two arguments to telnet to a device and saves the output in a file. Following is the script.... (0 Replies)
Discussion started by: cyberparanoid
0 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

calling expect script in ksh is failing via cron

I'm calling an expect script via a ksh script in cron and it is failing. The script runs fine if i run it manually. Does anyone know if it is an issue with compatibilty and if there is a way around it? (2 Replies)
Discussion started by: bhatia
2 Replies

8. UNIX for Advanced & Expert Users

How to set a label/number for a function in a shells script

Can any one let me know like how cn i set an label or number for each function whihc are there in an shells script: for example cd /opt/qcom/test/ function1() function2() function3() ------- i ahe a script like this .now i want to count each of this function like 1, 2, 3,............... (3 Replies)
Discussion started by: lalitka
3 Replies

9. Shell Programming and Scripting

Calling Expect script in Perl...

I call a EXPECT script from my perl script with machine IP and a FIle. The script logins to the machine and exports the value. The values to be exported or stored in a file. I have close to 10 machines and I have created 10 files and pass the corresponding files in command line, Now I could like... (4 Replies)
Discussion started by: ramkriz
4 Replies

10. Shell Programming and Scripting

Calling an Expect script from a Webpage using PHP

I have a webpage that is in HTML and PHP. In PHP I have tried using exec, system, shell_exec and passthru functions to call an Expect Script file (temp.exp). This Expect file spawns a telnet session that uses "expect/send" commands to retrieve information from an environmental unit (not a normal... (0 Replies)
Discussion started by: CCUSmith
0 Replies
Login or Register to Ask a Question