nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Taking command line argument in expect script

This is a discussion on Taking command line argument in expect script within the Shell scripting forums, part of the Development/Scripting category; Hi frnds, This is my first post..... I want to take IP address, username and password as command line arguments ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-04-2009, 07:49 PM
Junior Member
User
 
Join Date: Apr 2009
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
arvindkp is on a distinguished road
Default Taking command line argument in expect script

Hi frnds,
This is my first post.....
I want to take IP address, username and password as command line arguments to a expect script to telnet. My script is :
__________________________________________________ ____________________
##file name is : script.sh
#!/bin/bash
#!/usr/bin/expect -- ##/usr/bin/expect is the directory where expect was installed

log_user 0 ##this command is used to hide the conversationn between the script and the other machine
set address [lrange $argv 0 0] ##assign the first passed parameter while calling the script to $address
set username [lrange $argv 1 1] ##assign the 2nd passed parameter while calling the script to $username
set password [lrange $argv 2 2] ##assign the 3rd passed parameter while calling the script to $password
spawn telnet ${address} ##start the telnet session to machine with IP=$address

##start conversation with the machine:
expect "login:"
send -- "$username\r"
expect "Password:"
send -- "$password\r"
expect "$ "
send --"exit\r"
__________________________________________________ ____________________
im running script as:
expect script.sh 10.1.101.80 root password

but it is showing the error :
can't read "address": no such variable
while executing
"set address [lrange $argv 0 0]
(file "script.sh" line 6)
__________________________________________________ ____________________
please help me.....I hv expect and Tcl packages installed on my machine (OS is REL-4)
Reply With Quote
  #2 (permalink)  
Old 10-04-2009, 12:20 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Remove #!/bin/bash and try again!
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 10-04-2009, 12:38 PM
Junior Member
User
 
Join Date: Apr 2009
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
arvindkp is on a distinguished road
Default

I removed #!/bin/bash but still showing same error
Reply With Quote
  #4 (permalink)  
Old 28-04-2009, 01:35 AM
Junior Member
User
 
Join Date: Apr 2009
OS: Ubuntu
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
fcojocaru is on a distinguished road
Default

# works as a comment only if it's the first character on the line, so basically your comments are messing you script.Move each and every one of them to their own line and it should work(or at least spit a different error).
Reply With Quote
  #5 (permalink)  
Old 28-04-2009, 04:45 PM
Junior Member
User
 
Join Date: Apr 2009
OS: Debian
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
arvindkp is on a distinguished road
Default

Thanks for your help fcojocaru ...its working now.
I hv one more issue:
I want to store the logs for some commands which i am executing in expect script, to a txt file. I want to check whether the command was wuccessful or it was failed. Log file format:
command <3> - successful
command <8> - failed
...................................
command <27> - failed


How to do this ...plz help.
Reply With Quote
  #6 (permalink)  
Old 28-04-2009, 05:01 PM
Junior Member
User
 
Join Date: Apr 2009
OS: Ubuntu
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
fcojocaru is on a distinguished road
Default

This is trickier; Check the manpage of expect to see how to make a simple decision structure ( if - then - else ) , and check specifically for the log_file directive. There is no simple way to check the $? like in bash , but rather check the output of the executed command ( telnet in your case ).
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Script to open and read a file that is supplied as a command line arg blingbling Shell scripting 3 31-03-2009 10:54 AM
Shell script to count number of lines in file specified by the second command-line seaman77 Shell scripting 1 16-03-2009 07:46 PM
expect script hangs antzjos Shell scripting 0 22-05-2008 06:37 PM
Expect and tcl - manymaint.exp script Intelligen Shell scripting 11 21-11-2007 02:48 PM
Command line remote access angelus_kit Networking, Firewalls and Security 7 05-09-2007 06:30 PM


All times are GMT +5.5. The time now is 02:17 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38