|
|
| Author |
Message |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 19:04 Post subject: complex programming help NWS b*****s
|
|
|
Hey guys .. hopefully someone can help.
I have a list of IP addresses that I need to telnet to and run a series of commands on. Which in and of itself is pretty simple but I have a few curves to throw into it.
There are multiple ways that the telnet session could respond: (ie it may just ask for a password and some may ask for a username and password).
So my question is this:
Is there a way say in perl to initiate a connection scrape what the prompt is, hold it in a buffer then iterate over a series of if statements until a match is made then execute a series of commands (after initiating a second telnet session)
Last edited by gotissues68 on 10/11/05 - 22:25; edited 1 time in total
|
|
|
Back to top
|
|
|
|
 |
kireol
RealPoor Master of Posts

Joined: 02 Aug 2003 Posts: 9517
Location: Royal Oak, MI
|
|
|
Back to top
|
|
|
|
 |
sinrakin
RealPoor Master of Posts

Joined: 11 Oct 2002 Posts: 7044
|
Posted: 10/10/05 - 19:13 Post subject:
|
|
|
|
My first thought would be to do it with Expect (which I think runs on top of Perl but I don't even remember anymore). I'd have to look it up though.
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 19:18 Post subject:
|
|
|
yea expect is the ticket. I have the expect perl module installed on my dev machine .. being able to write out the sub routines to run based on the prompt isn't a problem I don't think I just need to figure out if expect can handle iterations..
Let say I have something like
expect "Login:"
but receive "Username:" instead can it break from what its expecting and shift and run a sub routine based on what it got back or better yet can it determine which routine to run based on what it gets versus what it expects?
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 19:20 Post subject:
|
|
|
oh shitz this just may work
make a request, store the request as a variable and then do if tests until a match is found
|
|
|
Back to top
|
|
|
|
 |
kireol
RealPoor Master of Posts

Joined: 02 Aug 2003 Posts: 9517
Location: Royal Oak, MI
|
Posted: 10/10/05 - 19:25 Post subject:
|
|
|
|
yup. which is pretty much what I PMd you about the other day. but was working with web pages, not telnets. it's real ez. all returned data is stored in a variable that you can search on and shit.
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 19:27 Post subject:
|
|
|
| kireol wrote: | | yup. which is pretty much what I PMd you about the other day. but was working with web pages, not telnets. it's real ez. all returned data is stored in a variable that you can search on and shit. |
I am going to so be bugging you tonight :-p
|
|
|
Back to top
|
|
|
|
 |
kireol
RealPoor Master of Posts

Joined: 02 Aug 2003 Posts: 9517
Location: Royal Oak, MI
|
Posted: 10/10/05 - 19:33 Post subject:
|
|
|
|
heheh. it's just paybacks, that's all
|
|
|
Back to top
|
|
|
|
 |
kireol
RealPoor Master of Posts

Joined: 02 Aug 2003 Posts: 9517
Location: Royal Oak, MI
|
Posted: 10/10/05 - 19:47 Post subject:
|
|
|
|
actually after doing some reading, if your are telnetting, you may want to try PERLs $net::telnet
|
|
|
Back to top
|
|
|
|
 |
kireol
RealPoor Master of Posts

Joined: 02 Aug 2003 Posts: 9517
Location: Royal Oak, MI
|
|
|
Back to top
|
|
|
|
 |
lotek
RealPoor Sensei

Joined: 12 Oct 2002 Posts: 1598
|
Posted: 10/10/05 - 20:14 Post subject:
|
|
|
I'd do it with the perl expect route. Its real easy to use. few group.google searches and I had what I needed.
I used it to automate the generation of random assembly tests to run on a dev board. xmodem baby! ya!
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 20:19 Post subject:
|
|
|
| lotek wrote: | I'd do it with the perl expect route. Its real easy to use. few group.google searches and I had what I needed.
I used it to automate the generation of random assembly tests to run on a dev board. xmodem baby! ya! |
I think perl would be cleaner and I wouldn't get laughed at as much heh. I'll try some group searches tonight. As long as I can grab the data and store it then act upon that I should be good to go.
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 21:19 Post subject:
|
|
|
Alright well for some reason the perl gods are being nice to me tonight here's what I have so far that works...
#!/usr/bin/perl -w
use Net::Telnet;
@array = `cat blacklist.master`;
my $array_element;
foreach $array_element(@array)
{
$telnet = new Net::Telnet ( Timeout=>10,
Errmode=>'return');
$telnet->open('$array_element');
$telnet->waitfor('/Login: $/i');
$telnet->print('<password>');
$telnet->waitfor('/# $/i');
$telnet->print('rem ipf insert 0 input drop -p udp -da $array_element -dp 53 internet');
$output = $telnet->waitfor('/# $/i');
print "successfully patched $array_element";
}
Now with the Errormode set to "return" it acts as if all of the IP's in the list where successfully reached when I know for a fact they were, if I set Errormode to "die" then the script dies when it can't contact the first dead IP, I have been browsing the docs and can't find anything like a "continue" mode where if it where to be "dead" so to speak that it would print to stdout and continue .... Also I have one prompt defined, how can I tell the script to look at each prompt and run commands based on what the prompt is showing?
|
|
|
Back to top
|
|
|
|
 |
lotek
RealPoor Sensei

Joined: 12 Oct 2002 Posts: 1598
|
Posted: 10/10/05 - 21:28 Post subject:
|
|
|
| gotissues68 wrote: | Alright well for some reason the perl gods are being nice to me tonight here's what I have so far that works...
#!/usr/bin/perl -w
use Net::Telnet;
@array = `cat blacklist.master`;
my $array_element;
foreach $array_element(@array)
{
$telnet = new Net::Telnet ( Timeout=>10,
Errmode=>'return');
$telnet->open('$array_element');
$telnet->waitfor('/Login: $/i');
$telnet->print('<password>');
$telnet->waitfor('/# $/i');
$telnet->print('rem ipf insert 0 input drop -p udp -da $array_element -dp 53 internet');
$output = $telnet->waitfor('/# $/i');
print "successfully patched $array_element";
}
Now with the Errormode set to "return" it acts as if all of the IP's in the list where successfully reached when I know for a fact they were, if I set Errormode to "die" then the script dies when it can't contact the first dead IP, I have been browsing the docs and can't find anything like a "continue" mode where if it where to be "dead" so to speak that it would print to stdout and continue .... Also I have one prompt defined, how can I tell the script to look at each prompt and run commands based on what the prompt is showing? |
you could setup a file that contains your call/resonpse pairs. read thoes into a hash and itterate through the hash comparing what you expect the call to be against what the telnet session gave. If a match, send the responce. one of a million ways.
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/10/05 - 21:59 Post subject:
|
|
|
| lotek wrote: | | gotissues68 wrote: | Alright well for some reason the perl gods are being nice to me tonight here's what I have so far that works...
#!/usr/bin/perl -w
use Net::Telnet;
@array = `cat blacklist.master`;
my $array_element;
foreach $array_element(@array)
{
$telnet = new Net::Telnet ( Timeout=>10,
Errmode=>'return');
$telnet->open('$array_element');
$telnet->waitfor('/Login: $/i');
$telnet->print('<password>');
$telnet->waitfor('/# $/i');
$telnet->print('rem ipf insert 0 input drop -p udp -da $array_element -dp 53 internet');
$output = $telnet->waitfor('/# $/i');
print "successfully patched $array_element";
}
Now with the Errormode set to "return" it acts as if all of the IP's in the list where successfully reached when I know for a fact they were, if I set Errormode to "die" then the script dies when it can't contact the first dead IP, I have been browsing the docs and can't find anything like a "continue" mode where if it where to be "dead" so to speak that it would print to stdout and continue .... Also I have one prompt defined, how can I tell the script to look at each prompt and run commands based on what the prompt is showing? |
you could setup a file that contains your call/resonpse pairs. read thoes into a hash and itterate through the hash comparing what you expect the call to be against what the telnet session gave. If a match, send the responce. one of a million ways. |
thanks again lotek, this stuff really helps. I'd much rather have people point me in the right direction rather then doing it for me, thats the last thing I want =) thanks for the helpful hints....
as a side note this is for the "shady employer" who I leave at the end of the week, kinda of my final "ha I'm the better person in all of this" heh
|
|
|
Back to top
|
|
|
|
 |
lotek
RealPoor Sensei

Joined: 12 Oct 2002 Posts: 1598
|
Posted: 10/10/05 - 22:53 Post subject:
|
|
|
| gotissues68 wrote: | | lotek wrote: | | gotissues68 wrote: | Alright well for some reason the perl gods are being nice to me tonight here's what I have so far that works...
#!/usr/bin/perl -w
use Net::Telnet;
@array = `cat blacklist.master`;
my $array_element;
foreach $array_element(@array)
{
$telnet = new Net::Telnet ( Timeout=>10,
Errmode=>'return');
$telnet->open('$array_element');
$telnet->waitfor('/Login: $/i');
$telnet->print('<password>');
$telnet->waitfor('/# $/i');
$telnet->print('rem ipf insert 0 input drop -p udp -da $array_element -dp 53 internet');
$output = $telnet->waitfor('/# $/i');
print "successfully patched $array_element";
}
Now with the Errormode set to "return" it acts as if all of the IP's in the list where successfully reached when I know for a fact they were, if I set Errormode to "die" then the script dies when it can't contact the first dead IP, I have been browsing the docs and can't find anything like a "continue" mode where if it where to be "dead" so to speak that it would print to stdout and continue .... Also I have one prompt defined, how can I tell the script to look at each prompt and run commands based on what the prompt is showing? |
you could setup a file that contains your call/resonpse pairs. read thoes into a hash and itterate through the hash comparing what you expect the call to be against what the telnet session gave. If a match, send the responce. one of a million ways. |
thanks again lotek, this stuff really helps. I'd much rather have people point me in the right direction rather then doing it for me, thats the last thing I want =) thanks for the helpful hints....
|
no problems man. Glad to help.
|
|
|
Back to top
|
|
|
|
 |
Occulis
RealPoor Jedi

Joined: 11 Oct 2002 Posts: 13293
Location: Moral Relativity Central
|
Posted: 10/10/05 - 23:23 Post subject:
|
|
|
|
so anyway like i was saying. burger s***s.
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/11/05 - 00:09 Post subject:
|
|
|
| Occulis wrote: | | so anyway like i was saying. burger s***s. |
hahahah I was thinking to myself "Gee I wonder when Dunn is going to login and tell me I'm a poor coder and this is a piece of shit script and I should go die" ...
I love you Jason
|
|
|
Back to top
|
|
|
|
 |
Gethy
RealPoor Master of Posts

Joined: 11 Oct 2002 Posts: 5599
Location: Tallahassee, FL
|
Posted: 10/11/05 - 00:46 Post subject:
|
|
|
|
needs more nws or ANGRE
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
|
|
Back to top
|
|
|
|
 |
lotek
RealPoor Sensei

Joined: 12 Oct 2002 Posts: 1598
|
Posted: 10/11/05 - 18:52 Post subject:
|
|
|
havent run it, but my guess would be that if array_element doesnt work, but setting ip does, your cat is adding the newline to the ip.
try adding
chop($array_element);
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/11/05 - 19:23 Post subject:
|
|
|
| lotek wrote: |
havent run it, but my guess would be that if array_element doesnt work, but setting ip does, your cat is adding the newline to the ip.
try adding
chop($array_element); |
LOL yea I had some review it as well and thats exactly what it was.. I really appreciate the help...
Not that anyone cares but I'm fixing a bug on about 7500 routers... that we shipped to customers! thanks to f*****g Efficient Networks/Siemens heh.
|
|
|
Back to top
|
|
|
|
 |
motherface
RealPoor Guru

Joined: 12 Mar 2003 Posts: 3407
|
Posted: 10/11/05 - 20:09 Post subject:
|
|
|
| gotissues68 wrote: | | as a side note this is for the "shady employer" who I leave at the end of the week, kinda of my final "ha I'm the better person in all of this" heh |
Don't you mean kireol & Realpoor are "the better person" since they're the ones who solved it for you?
Edit: Misread your intent, I thought you were like gloating about being a super programmer. Yay!
|
|
|
Back to top
|
|
|
|
 |
gotissues68
RealPoor Sensei

Joined: 21 Aug 2003 Posts: 1866
|
Posted: 10/11/05 - 22:22 Post subject:
|
|
|
| motherface wrote: | | gotissues68 wrote: | | as a side note this is for the "shady employer" who I leave at the end of the week, kinda of my final "ha I'm the better person in all of this" heh |
Don't you mean kireol & Realpoor are "the better person" since they're the ones who solved it for you?
Edit: Misread your intent, I thought you were like gloating about being a super programmer. Yay! |
I would never gloat about being a super programmer since I'm not even close to being one. Kireol and Lotek helped me out by pointing me in the right direction, unless I misread something neither of them wrote it for me or gave me a solution, Lotek did provide an answer to a question I had though which I sincerly appreciate since I'm a perl virgin.
Time to NWS this b***h
|
|
|
Back to top
|
|
|
|
 |
kireol
RealPoor Master of Posts

Joined: 02 Aug 2003 Posts: 9517
Location: Royal Oak, MI
|
Posted: 10/11/05 - 23:07 Post subject:
|
|
|
|
so hawt
|
|
|
Back to top
|
|
|
|
 |
|
|