LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2014, 07:08 AM   #1
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Rep: Reputation: 49
passing multiple argument to expect script


hi ,
i have written this script that runs particular set of commands after telnetting to remote machine (running embedded linux with busybox,uboot,ujffs2 etc.) from RHEL server
Code:
#!/usr/bin/expect -f
set timeout 2
spawn /usr/kerberos/bin/telnet "192.168.7.[lindex $argv 0]"
sleep 10
expect ".*login: "
send "root\r"
expect "Password: "
send "root\r"
expect ".*# "
send "ps | grep -i audio\r"
expect ".*# "
send "cat /tmp/CdotGponVoip.cfg | grep -i 2659\r"
expect ".*# "
send "ifconfig | grep -EA1 \'wan0\.\[0-9\]\[0-9\]\[0-9\]\[\^0-9\]\'\r"
expect ".*# "
send "ontwanip=\$(ifconfig|sed -rn \'/wan0\.\[0-9\]\[0-9\]\[0-9\]\[\^0-9\]/\{np\\
}\')\r"
send "ontwanip=\$(echo \$ontwanip | sed \'s/inet addr:\\\(\.\*\\\) Bcast\.\*/\\11
/\')\r"
expect ".*# "
send "echo \$ontwanip\r"
expect ".*# "
send "ontwangw=\$(echo \$ontwanip | sed \'s/\.\[0-\9]\$/\.1/\')\r"
[root@gpondell2 ~]# cat trail.sh
#!/usr/bin/expect -f
set timeout 2
spawn /usr/kerberos/bin/telnet "192.168.7.[lindex $argv 0]"
sleep 10
expect ".*login: "
send "root\r"
expect "Password: "
send "root\r"
expect ".*# "
send "ps | grep -i audio\r"
expect ".*# "
send "cat /tmp/CdotGponVoip.cfg | grep -i 2659\r"
expect ".*# "
send "ifconfig | grep -EA1 \'wan0\.\[0-9\]\[0-9\]\[0-9\]\[\^0-9\]\'\r"
expect ".*# "
send "ontwanip=\$(ifconfig|sed -rn \'/wan0\.\[0-9\]\[0-9\]\[0-9\]\[\^0-9\]/\{np\}\')\r"
send "ontwanip=\$(echo \$ontwanip | sed \'s/inet addr:\\\(\.\*\\\) Bcast\.\*/\\1/\')\r"
expect ".*# "
send "echo \$ontwanip\r"
expect ".*# "
send "ontwangw=\$(echo \$ontwanip | sed \'s/\.\[0-\9]\$/\.1/\')\r"
expect ".*# "
send "echo \$ontwangw\r"
expect ".*# "
send "ping -c 1 \$ontwangw\r"
expect ".*# "
send "exit\r"
expect ".*# "
Here is the sample output with command './run.sh 55'
Code:
spawn /usr/kerberos/bin/telnet 192.168.7.55
Trying 192.168.7.55...
Connected to 192.168.7.55 (192.168.7.55).
Escape character is '^]'.

BRG-ONT8 login: root
Password:

BROADLIGHT,2008 Linux ( based on Wind River Linux glibc_small (small) 2.0)
root@BRG-ONT8:/root # ps | grep -i audio
  387 root        444 S   /audiocodes/apps/acl_main -s
 6687 root       2872 S   /audiocodes/apps/voip_task -r
 9530 root        564 S   grep -i audio
root@BRG-ONT8:/root # cat /tmp/CdotGponVoip.cfg | grep -i 2659
voip/line/0/id=911126596601
voip/line/0/auth_name=911126596601
voip/line/1/id=911126596602
voip/line/1/auth_name=911126596602
root@BRG-ONT8:/root # ifconfig | grep -EA1 'wan0.[0-9][0-9][0-9][^0-9]'
wan0.170  Link encap:Ethernet  HWaddr 00:24:88:00:1A:35
          inet addr:172.16.240.2  Bcast:172.16.240.255  Mask:255.255.255.0
np}')BRG-ONT8:/root # ontwanip=$(ifconfig|sed -rn '/wan0.[0-9][0-9][0-9][^0-9]/{
*/\1/')G-ONT8:/root # ontwanip=$(echo $ontwanip | sed 's/inet addr:\(.*\) Bcast.
root@BRG-ONT8:/root # echo $ontwanip
172.16.240.2
root@BRG-ONT8:/root # ontwangw=$(echo $ontwanip | sed 's/.[0-9]$/.1/')
root@BRG-ONT8:/root # echo $ontwangw
172.16.240.1
root@BRG-ONT8:/root # ping -c 1 $ontwangw
PING 172.16.240.1 (172.16.240.1): 56 data bytes
exit
64 bytes from 172.16.240.1: icmp_seq=0 ttl=255 time=85.8 ms

--- 172.16.240.1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 85.8/85.8/85.8 ms
My problem is that I wish to give script multiple arguments (quantity not fixed) & want it to telnet-and-run-command on each of them .
How can I do that ?
 
Old 04-30-2014, 09:35 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,116
Blog Entries: 10

Rep: Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815
I've held off commenting because I haven't scripted in expect; however it seemed unusual that a scripting language would not allow for arguments, expect seems to be no exception there, a simple google found this reference:

Expect Script Command Line Argument Examples

So I'd suggest that you play with the variable names argv[0], argv[1], and so forth to accomplish your intentions. First write a test script like shown in these examples to validate that however you're running the script, that you see the arguments. Then write your script to expect (anticipate) the command line arguments to be in fixed order so that you don't have to first worry about parsing complicated option variations. There are some ways to parse options using the getopts utility for bash or other shell scripts, but I'm not sure if this capability exists for expect scripting, therefore be simple and look only for arguments either there, or not there and make it so that you want the options to always be in a certain order; at least to get you started before getting more complicated as to how you parse options.
 
Old 04-30-2014, 02:01 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
You can also do it using a shell "here" document, which allows you to use shell variables placed within the expect script:

Code:
#!/bin/sh
# parameters:
#    0 - host to connect to
#    1 - something else
expect -f - <<EOF
set timeout 2
spawn /usr/kerberos/bin/telnet "192.168.7.${0}"
sleep 10
expect ".*login: "
send "root\r"
...
EOF
Though you sometimes have to be careful about quoting escaping things in the right place. (not sure about he \r things - the shell might convert them to a return, so it might need to be \\r so that the \ is escaped with a \).

I have a STRONG tendency to avoid expect as it requires embedded passwords which is a security weakness.
 
Old 05-01-2014, 01:00 AM   #4
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
to rtmistler ,
I want to ask about running expect script with all arguments one at a time .
Logically
Code:
while ( number_of_arguments > 0)
{ expect_script < last_argument
  number_of_arguments = number_of_arguments - 1 ;
}
meanwhile , I will try to find way to check whether lindex $argv 1 , lindex $argv 2 ... so on exists and read for loop tidbits mentioned at http://www.thegeekstuff.com/2011/01/...ps-conditions/.

to jpollard ,
I agree this script is for internal network use as it exposes password.
Your code does not loop through all arguments (one at a time) , leaving aside escaping quotes issue. Instead
it did
telnet 192.168.7../script_name

Last edited by sumeet inani; 05-01-2014 at 01:21 AM. Reason: clarity
 
Old 05-01-2014, 05:58 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
I don't follow...

You can even do things like:

Code:
( cat <<EOF
set timeout 2
spawn /usr/kerberos/bin/telnet "192.168.7.${1}"
sleep 10
expect ".*login: "
send "root\r"
...
EOF
shift 1    # discard first parameter

for i in $*; do
   cat <<EOF2
<things to do for each argument>
EOF2
done
) | expect -f -
 
Old 05-01-2014, 06:54 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,116
Blog Entries: 10

Rep: Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815
Quote:
Originally Posted by sumeet inani View Post
to rtmistler ,
I want to ask about running expect script with all arguments one at a time .
Logically
Code:
while ( number_of_arguments > 0)
{ expect_script < last_argument
  number_of_arguments = number_of_arguments - 1 ;
}
Appreciate the vote of confidence, as I've said I've not used expect scripting, however I can google See Answer #1, just to advise the google string used there was "expect script number of arguments". And what I think you ought to do if you're going to continue scripting with expect is to search for one or more expect scripting guides until you find enough resources to give you these flexibilities. Obviously based on your question there, the next thing you wish to do is to create a for loop or determine if your arguments are considered a list by the scripting language and then you can refer to each entry in that list. So, search for some good expect script references to help you further.

Is this a case where:
  1. You're working at something and expect is the primary avenue offered to you?
  2. You've inherited ownership of something and there's a bunch of expect scripts there and now you're the maintainer and trying to get up to speed?
I was sort of looking at jpollard's answers thinking that they're probably correct, I looked at your words versus the script contents you posted because you asked how to get the command line arguments and therefore did not look at your script's contents. Meanwhile jpollard has looked at your script and has offered BASH script, or also some expect script examples. Therefore wondering how you've come to go down a path using expect scripting, and being in a situation where some fundamental script operations are unknowns. This is the first I've heard of expect scripting, although I've had to work with Tcl a bit for test scripts and I wonder if this is similarly related.

This is not to admonish or offer insult, but rather to get information so that forum posters can understand your experience level and understand how to help you best. Can you share your experience levels with scripting, expect syntax, and information about why you're proceeding with this?
 
Old 05-04-2014, 07:29 AM   #7
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
Finally I got the solution
Here we pass ip_addresses separated by spaces to loop.sh script which checks if they can be pinged . If yes , then ip address given to trial.sh script using expect.
This is simple without going into details of loop , if..else in 'expect'.


USAGE loop.sh ip1 ip2 ip3 ... so on
Code:
#!/bin/bash
for (( i=1; i<=$#; i++ )); do
    eval arg=\$$i
    echo "$arg"
        ping -c 1 192.168.7.$arg
        if [[ $? == 0 ]]
        then
                ./trial.sh $arg
        else
                echo CANNOT PING 192.168.7.$arg
        fi
        read -n1 -r -p "PRESS ANY KEY TO CONTINUE" key
done
USAGE trial.sh ip_address
Code:
#!/usr/bin/expect -f
set timeout 2
spawn /usr/kerberos/bin/telnet "192.168.7.[lindex $argv 0]"
sleep 10
expect ".*login: "
send "root\r"
expect "Password: "
send "root\r"
expect ".*# "
send "ps | grep -i audio\r"
expect ".*# "
send "cat /tmp/CdotGponVoip.cfg | grep -i 2659\r"
expect ".*# "
send "ifconfig | grep -EA1 \'wan0\.\[0-9\]\[0-9\]\[0-9\]\[\^0-9\]\'\r"
expect ".*# "
send "ontwanip=\$(ifconfig|sed -rn \'/wan0\.\[0-9\]\[0-9\]\[0-9\]\[\^0-9\]/\{np\}\')\r"
send "ontwanip=\$(echo \$ontwanip | sed \'s/inet addr:\\\(\.\*\\\) Bcast\.\*/\\1/\')\r"
expect ".*# "
send "echo \$ontwanip\r"
expect ".*# "
send "ontwangw=\$(echo \$ontwanip | sed \'s/\.\[0-\9]\$/\.1/\')\r"
expect ".*# "
send "echo \$ontwangw\r"
expect ".*# "
send "ping -c 1 \$ontwangw\r"
expect ".*# "
send "exit\r"
expect ".*# "
 
Old 05-04-2014, 07:35 AM   #8
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
to jpollard
I saved your script as
Code:
( cat <<EOF
set timeout 2
spawn /usr/kerberos/bin/telnet "192.168.7.${1}"
sleep 10
expect ".*login: "
send "root\r"
expect "Password: "
send "root\r"
expect ".*# "
send "exit\r"
expect ".*# "
EOF
shift 1    # discard first parameter

for i in $*; do
   cat <<EOF2
EOF2
done
) | expect -f -
Here we are just logging in & out .

Ran
./jpollard.sh 73 76 77 78
The output was
Quote:
[root@gpondell2 ~]# ./jpollard.sh 73 76 77 78
spawn /usr/kerberos/bin/telnet 192.168.7.73
Trying 192.168.7.73...
Connected to 192.168.7.73 (192.168.7.73).
Escape character is '^]'.

BRG-ONT3 login: root
Password:

BROADLIGHT,2008 Linux ( based on Wind River Linux glibc_small (small) 2.0)
root@BRG-ONT3:/root # exit
logout
Connection closed by foreign host.
[root@gpondell2 ~]#
 
Old 05-04-2014, 09:08 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Worked didn't it.

All you have to do is add whatever handling you want.
 
Old 05-04-2014, 10:47 PM   #10
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
to jpollard ,
sorry , I meant it did not try on 76 , 77 & 78 .
Did I do anything wrong while running jpollard.sh script ?
 
Old 05-05-2014, 04:54 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
No, you just didn't put whatever you wanted to do in it. That is what you were to put in "<things to do for each argument>". Now if you wanted to do the same things every time, then there would be no environment variables there.

I just thought you wanted to go somewhere (specified by the first argument), then do different things (specified by the other arguments).
 
Old 05-06-2014, 08:12 AM   #12
sumeet inani
Member
 
Registered: Oct 2008
Posts: 898
Blog Entries: 26

Original Poster
Rep: Reputation: 49
to jpollard ,
Sorry , I could not make myself clear .

Still , solution obtained using external bash script(post #7).
Not elegant , but workable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing arguments from expect script to a shell script schoedel Programming 3 06-29-2013 10:59 AM
Passing Single Argument to Multiple Scripts prravin1 Programming 13 02-29-2012 06:49 AM
[SOLVED] expect help (multiple commands in argument) vikas027 Programming 10 03-17-2010 01:19 AM
[SOLVED] (Something like...) passing an argument to a MATLAB script Mike_V Programming 1 12-21-2009 04:04 PM
expect script - for argument loop ? nilleso Linux - Software 3 09-16-2009 08:51 PM


All times are GMT -5. The time now is 08:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration