LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-14-2012, 12:33 PM   #1
Tarikc
Member
 
Registered: May 2009
Distribution: CentOS
Posts: 50

Rep: Reputation: 2
Auto configuring a whole subnet with Expect script?


Hi everybody!

I google it but I did not find anything fruitful.
I use expect to auto configure a single host using ssh and run the script with crontab.

What I want to do now is that I want to use the expect script on a whole subnet 192.168.0.0/24 for example, to execute one or two commands.

Is this possible? and how?

Looking very forward to your reply.

Thanks in advance guys.
 
Old 03-15-2012, 03:00 AM   #2
toddles13
LQ Newbie
 
Registered: Nov 2004
Location: Auckland NZ
Distribution: Gentoo
Posts: 4

Rep: Reputation: 1
Depends on how your expect script works but you can always call it from a bash script.

Code:
for i in $(seq 1 1 254);
do 
<your expect script> 192.168.1.$i
done
If you bash version is more modern then you can simply use

Code:
for i in {1..255};
do
<your expect script> 192.168.1.$i
done
See the examples here.

http://www.cyberciti.biz/faq/bash-for-loop/
 
Old 03-18-2012, 09:19 AM   #3
Tarikc
Member
 
Registered: May 2009
Distribution: CentOS
Posts: 50

Original Poster
Rep: Reputation: 2
Hello toddles13,

Thank you very much for your reply.
I'm new to to making scripts with bash.

When I did this:

for i in {1..255};
do
expect -f /etc/file
done

and pointed the and in the expect file I wrote within the file:
spawn ssh user@192.168.1.$i

The action got rejected with:

can't read "i": no such variable

How can I map between the two file??
or how can I write it in the correct way?

Thank you very much for your kind help.
 
Old 03-20-2012, 03:12 AM   #4
toddles13
LQ Newbie
 
Registered: Nov 2004
Location: Auckland NZ
Distribution: Gentoo
Posts: 4

Rep: Reputation: 1
Hi Tarikc,

I have never written anything in expect before now but this works just fine.

Code:
#!/usr/bin/expect
puts "This is arg0 [lindex $argv 0]"
puts "This is arg1 [lindex $argv 1]"
Which when run
Code:
./your_expect_script foo bar
The output would be:

This is arg0 foo
This is arg1 bar

So for the bash part you would simply be
Code:
for i in {1..255};
do
/path to your expect script/your_expect_script $i
done
If we used the example expect script from above you get the following.

Code:
$ for i in {1..255}; do ./test.expect $i; done
This is arg0 1
This is arg1 
This is arg0 2
This is arg1 
This is arg0 3
..
..
This is arg1 
This is arg0 255
This is arg1
That should get you going.

If not have a look for arguments, parameters and variables, most languages will have these and there are bound to be plenty of examples around.

Last edited by toddles13; 03-23-2012 at 04:51 PM.
 
1 members found this post helpful.
Old 03-20-2012, 03:54 AM   #5
Tarikc
Member
 
Registered: May 2009
Distribution: CentOS
Posts: 50

Original Poster
Rep: Reputation: 2
Hello toddles13,

You are the man!!
Thank you very much for your wonderful help.
It worked like a charm.

Thank you
 
Old 03-23-2012, 04:52 PM   #6
toddles13
LQ Newbie
 
Registered: Nov 2004
Location: Auckland NZ
Distribution: Gentoo
Posts: 4

Rep: Reputation: 1
No problem, glad its got you going.
 
  


Reply

Tags
expect


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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
Auto SSH-login expect script creatorrr Linux - Software 7 10-26-2011 03:09 AM
LXer: A script for auto-configuring saned network connections LXer Syndicated Linux News 0 10-08-2010 06:10 AM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
problem with configuring auto script badall Linux - Networking 4 06-17-2007 08:15 AM


All times are GMT -5. The time now is 07:50 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration