LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-12-2012, 01:47 AM   #1
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Rep: Reputation: Disabled
shell script telnet multiple nodes and apply different command


actually I am new with shell script i want to make script telnet multiple nodes and apply different command


for example

site A IP 10.0.0.1 command on site B is
“configure xdsl line 1/1/10/27 service-profile 15

site B IP 10.0.0.2 command on site B is
“configure xdsl line 1/1/11/11 service-profile 16


i think i need to have two file contain Hosts IPs and another file for command need to apply


i try to use this code and it was nice if you have only one command need to apply


Code:
#!/bin/bash
while read line
do
# -c 1 means ping only once
# -w means time-out, so if nothing is received within 5 seconds then it's a fail
if ! ping -c 1 -w 5 $line &>/dev/null ;
then
echo "Failed to connect to $line"
else
echo "Connected to $line!"
username=admin
passwd=admin
port=23
cmd1="ter le 0"
cmd2="sh run"
cmd3="sh ru int di0"
( echo open ${line} ${port}
sleep 2
echo ${username}
sleep 2
echo ${passwd}
sleep 2
echo ${cmd1}
sleep 3
echo ${cmd2}
sleep 3
echo ${cmd3}
sleep 3
echo exit ) | telnet
fi
done < ip.txt



anyone can help me to editable above script to do my requirement
 
Old 11-12-2012, 02:20 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The script posted does not seem to use the command shown in the example ... ?

Here's a modified version of the script. Loop-invariant variable assignments have been moved before the loop and the code indented. The my.conf file has both the IP address and the command ...
Code:
10.0.0.1 configure xdsl line 1/1/10/27 service-profile 15
10.0.0.2 configure xdsl line 1/1/11/11 service-profile 16
... which the script reads as $ip and $command but $command is not used:
Code:
#!/bin/bash
username=admin
passwd=admin
port=23
cmd1="ter le 0"
cmd2="sh run"
cmd3="sh ru int di0"
while read ip command
do
    # -c 1 means ping only once
    # -w means time-out, so if nothing is received within 5 seconds then it's a fail
    if ! ping -c 1 -w 5 $ip &>/dev/null ;
    then
        echo "Failed to connect to $ip" >&2
    else
        echo "Connected to $ip!"
        ( echo open ${ip} ${port}
            sleep 2
            echo ${username}
            sleep 2
            echo ${passwd}
            sleep 2
            echo ${cmd1}
            sleep 3
            echo ${cmd2}
            sleep 3
            echo ${cmd3}
            sleep 3
            echo exit ) | telnet
    fi
done < my.conf
Incidentally there is no need for the {} around variable names unless the following character could be part of a variable name.

EDIT: and the error message has been redirected to stderr instead of stout as is conventional.

Last edited by catkin; 11-12-2012 at 02:22 AM.
 
Old 11-12-2012, 03:34 AM   #3
Tobler
LQ Newbie
 
Registered: Oct 2007
Distribution: Ubuntu, RedHat Enterprise Linux
Posts: 14

Rep: Reputation: 0
Hi
I have been doing same some years ago. There I was using expect -command and with autoexpect I was create script which recorded my example session.

$ expect -f testhost telnet testhost1
That would start telnet session to testhost1. And expect script is written to file testhost.

So then type all you should run on remote end. And finally quit your telnet session. That would save your script and you can start modifying it. Just replace host names, possible passwords and you may clean it more. And then integrate it with your shell script.
There was way to get shell environment variables as a host name and password but I don't remember that right now.

There's more documentation with just one search word: autoexpect

Br, Tobler
 
Old 11-12-2012, 05:54 AM   #4
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
thank you for all

Dear catkin


Code:
#!/bin/bash
username=admin
passwd=admin
port=23
cmd1="ter le 0"
cmd2="sh run"
cmd3="sh ru int di0"
while read ip command
do
    # -c 1 means ping only once
    # -w means time-out, so if nothing is received within 5 seconds then it's a fail
    if ! ping -c 1 -w 5 $ip &>/dev/null ;
    then
        echo "Failed to connect to $ip" >&2
    else
        echo "Connected to $ip!"
        ( echo open ${ip} ${port}
            sleep 2
            echo ${username}
            sleep 2
            echo ${passwd}
            sleep 2
            echo ${cmd1}
            sleep 3
            echo ${cmd2}
            sleep 3
            echo ${cmd3}
            sleep 3
            echo exit ) | telnet
    fi
done < my.conf

i tried this above script it still given me my old commands sequence
cmd1="ter le 0"
cmd2="sh run"
cmd3="sh ru int di0"

which it dose not in my.conf

my.conf is
10.0.0.1 configure xdsl line 1/1/10/27 service-profile 15


your help is highly appreciated



dear Tobler
if you have your old script by except please send it to me
many thanks for your support
 
Old 11-12-2012, 06:47 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
That's right -- you have not yet explained what sequence of commands you want to pipe to telnet. Do you want to send the open, username and password commands as before and then the command from my.conf? If so, try to code it and post what you write. If it doesn't work for you, explain how it doesn't work.
 
Old 11-12-2012, 07:07 AM   #6
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Dear catkin

exactly

I want to send the open, username and password commands as before and then the command from my.conf

is it possible to modify the script to do it like this


my script now is
Code:
#!/bin/bash
username=admin
passwd=admin
port=23
while read ip command
do
    # -c 1 means ping only once
    # -w means time-out, so if nothing is received within 5 seconds then it's a fail
    if ! ping -c 1 -w 5 $ip &>/dev/null ;
    then
        echo "Failed to connect to $ip" >&2
    else
        echo "Connected to $ip!"
        ( echo open ${ip} ${port}
            sleep 2
            echo ${username}
            sleep 2
            echo ${passwd}
            sleep 3
            echo exit ) | telnet
    fi
done < my.conf


and the result is

Code:
Username: admin
Password: 

alhokama#Connection closed by foreign host.
[root@proxy newtelnet]#


my.conf file
Code:
10.0.0.1 configure xdsl line 1/1/10/27 service-profile 15

still above command remaining which on red color and need to apply before exit the session



thanks again
 
Old 11-12-2012, 07:23 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Getting there but t doesn't send $command., it just logs in and exits. You can use the same technique that the original script used to send $cmd1.
 
Old 11-12-2012, 09:45 AM   #8
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Seeking your support to modify it to be send $command
 
Old 11-12-2012, 07:50 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Seeking your initiative to follow the pattern in the script and do it yourself.

The LQ ethos is to help questioners learn. "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime".
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to apply a command to a file to be printed linuxandtsm Linux - Newbie 7 09-28-2011 01:02 PM
HELP Using telnet command in shell script saurabhmehan Linux - Newbie 1 08-10-2010 01:00 AM
Modifying Specific Child Nodes In XML using Shell Script senthilmuthiah Linux - Newbie 1 04-20-2009 04:38 AM
bash script to apply sed command only to a specific text area mauran Programming 6 07-13-2007 04:38 PM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:51 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
Open Source Consulting | Domain Registration