- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: telnet script while read
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-04-2012 10:35 PM
тАО10-04-2012 10:35 PM
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
- Tags:
- telnet
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 12:33 AM
тАО10-05-2012 12:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 02:22 AM
тАО10-05-2012 02:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 02:59 AM - edited тАО10-05-2012 03:02 AM
тАО10-05-2012 02:59 AM - edited тАО10-05-2012 03:02 AM
Re: telnet script while read
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 07:24 AM
тАО10-05-2012 07:24 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 07:25 AM
тАО10-05-2012 07:25 AM
Re: telnet script while read
would 'expect' work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 07:55 AM
тАО10-05-2012 07:55 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 08:11 AM
тАО10-05-2012 08:11 AM
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...)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-05-2012 03:05 PM
тАО10-05-2012 03:05 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2012 01:04 AM
тАО10-09-2012 01:04 AM
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>&-
- Tags:
- coprocess