Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

telnet script while read

Highlighted
Super Advisor

telnet script while read

I have a flat file with multiple lines of IP  PORT.

I am reading the flat file to telnet to the IP and PORT and log the session.

 

These are not interactive ports so as soon as I connect I get raw data coming that I need to log.

 

For some reason I can not get this to work.

I need to open a telnet to each line and continue collecting the data.

Question, on connection, will it move onto the next one?

 

 

exec < /var/isnms/switch.ip
while read switch port host log; do

if [ -d /var/isnms/$host ]
then
echo $host exist
else
mkdir /var/isnms/$host
fi

if [ -f /var/isnms/$host/$log ]
then
echo $log exist
echo "telnet $switch $port"
telnet $switch $port >/var/isnms/$host/$log
else
touch /var/isnms/$host/$log
echo "telnet $switch $port"
telnet $switch $port >/var/isnms/$host/$log
fi

9 REPLIES
Valued Contributor Valued Contributor

Re: telnet script while read

Hi Ratzie,

 

so far, I can not see a big error in your script. Can you post the logging of the script? include a set -x on top ov the script and send the a log.

 

You check, whether the directorie /var/isnms/$host already exists. You do not check, whether any file or link or ... wis that name exists. If so, your script will fail.

After your first else, you may add the following command:

[ /var/isnms/$host ] && mv /var/isnms/$host /var/isnms/$host.$$.bak

 

May be, this already  solves your problem.

 

Bye

Ralf

Re: telnet script while read

Hi

Answer is no

once your telnet command is executed the script will wait for the termination of the telnet.

You might get it to work by running telnet in the background.

Cheers,

Per

Acclaimed Contributor Acclaimed Contributor

Re: telnet script while read

[ Edited ]

The problem with ssh, rlogin and most likely telnet in a while loop is that they will continue to read from stdin and eat up the data that's driving the while read loop.

 

With ssh and rlogin, there is a -n option that redirects stdin to /dev/null.

 

I'm not sure what you are doing in your while loop with /var/isnms/switch.ip?  Is this only driving the while loop or does it also have commands to telnet?

 

If you are only proving data to the while, you shouldn't be using exec to redirect stdin.  You should redirect it directly:

while read foo; do

..

done < input-file

 

Unfortunately I don't see a -n option to telnet but you can do the obvious:

telnet $switch $port < /dev/null >/var/isnms/$host/$log

 

>You might get it to work by running telnet in the background.

 

In that case, you would have to redirect stdin and probably stderr too.  The latter to the same logfile.

Super Advisor

Re: telnet script while read

Yes, I see that when I run the script it just hits the first login and stays there. It will not open up muitlple telnet sessions to each line in file.

I can not use ssh, since these devices do not support ssh.

 

I just want to open up the "pipe" and collect the data.

Trusted Contributor

Re: telnet script while read

would 'expect' work? 

Super Advisor

Re: telnet script while read

I have used expect, but would expect open up multiple telnets for logging each ip/port.

How would this be coded?

Trusted Contributor

Re: telnet script while read

if the behavior for these ports is consistent, a "skeleton" expect script could be used.

 

looking at one of my expect scripts -- there's only one spot that would need to be "customized" if i wanted to reuse it for another system -- and it's the "telnet" line ("spawn telnet <hard-coded system name>").

 

so if i took my working expect script and changed the hard-coded system name into a "variable" (eg, "spawn telent &system&") and saved it as my "skeleton", then i could do something like:

 

foreach $system do

  change &system& to $system in expect.skel > /tmp/somefile

  run /tmp/somefile

end

 

you get the idea....i hope....

 

(i guess your case "system" would be "port"...but...details, details...)

Acclaimed Contributor Acclaimed Contributor

Re: telnet script while read

>I just want to open up the "pipe" and collect the data.

 

Is there any interaction?  If so, then possibly use expect, unless there is some simple fixed input.

 

telnet $switch $port < /dev/null >/var/isnms/$host/$log 2>&1 &

 

Of course this never stops unless you kill it.

Honored Contributor Honored Contributor

Re: telnet script while read

Doing

exec < /var/isnms/switch.ip

telnet input is also that file so first you may do

 

exec 4< /var/isnms/switch.ip

 

while read -u 4 switch port host log; do

 

Then like already seen in the discussion you should have

telnet $switch $port </dev/null >.......      &

but redirecting </dev/null will send a fin, so depending on the peer, may makes telnet ends.

an other way is to make a very long sleep as input

sleep10000 | telnet  .$switch $port >......    &

 

 

if you want to pass something to telnet like a password you can also do

(sleep 1; echo passwd ; sleep10000) | telnet  .$switch $port >......    &

 

an other way is to launch it as a coprocess

 

exec telnet $switch $port |&

exec 5<&p 6>&p

cat <&5 >/var/isnms/$host/$log &

 

if you want to send something to the telnet then:

echo 'mypasswd' >&6

 

to close telnet pipes:

exec 5<&- 6>&-