shell script telnet multiple nodes and apply different command
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
#!/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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.