Operating System - HP-UX
1753386 Members
5487 Online
108792 Solutions
New Discussion юеВ

Looking for assistance in getting this script working...

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Looking for assistance in getting this script working...

Can someone help me get this script working? I'm the guy that can't script my way out of a paper bag.

I got this script a few years back and used it to change telnet/enable passwords, SNMP community strings and a few other things. I'm trying to get it working at another site but 'am having problems. Here's the error message I'm getting and the script.

***********************************************
$ ./rcisco_dc.scr
socket: Protocol not supported at ./rcisco_dc.scr line 106.

$ cat rcisco_dc.scr
#!/usr/bin/perl
#
# $Header: /home/aggarwal/lsrc/nocol/src/perlnocol/RCS/rcisco,v 1.3 1993/10/17 11:55:33 aggarwal Exp $
#
# rcisco - perl routine to execute a Cisco router command remotely
#
# Programmer: Christopher Sedore
# Revised by: John Wobus, jmwobus@mailbox.syr.edu
#
# (c) Syracuse University Computing & Network Services 1993
#
# No warranty is expressed or implied. Permission to copy and use is
# extended to all. Permission to redistribute is granted under the
# following conditions: it is not sold for profit; this copyright
# notice remains intact; the same permissions extend to the recipient;
# and if any changes are made, a notice is added so stating.
#
# Command Format:
#
# rcisco_mod []
#
# Signs on to Cisco router, executes command, and returns the
# result.
#
# - the name or IP number of the Cisco router.
# - the low level password for the router.
# - the enable password for the router.
# - the Cisco router command, e.g. "show interfaces".
# - the second Cisco router command.
# - the TCP port: 23 by default.
#
# Example Call:
#
# rcisco mycisco.excellent.edu "show hardware"
#
# This command signs on to myscisco.excellent.edu, executes the
# command "show hardware" and sends the result to the standard
# output.
#
# Depends upon:
# hostname - Unix command to list a host's name.
#
# To install this:
# (1) Assign the perl variables below appropriately.
# (2) Put this file whereever you want.
#
########################################################
#
#
# The command format is now:
#
# rcisco_wrnet.scr
#
#
require "ctime.pl";
#

$defaultrouter="10.21.21.14"; # Default router.
$defaultpasswd="ciscoworks"; # PW of the router.
$default_en_passwd="ciscoworks password"; # PW of the router.
$defaultcommand="show version"; # Command to exec on router
$defaultport=23; # Default TCP port.

#
# main routine
#

($them,$passwd,$en_passwd,$command,$command1,$port) = @ARGV;
$them = $defaultrouter unless $them;
$passwd = $defaultpasswd unless $passwd;
$en_passwd = $default_en_passwd unless $en_passwd;
$command = $defaultcommand unless $command;
$port = $defaultport unless $port; # usually should be port 23
$date = &ctime(time);

$AF_INET = 2;
$SOCK_STREAM = 1;

$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child; }

# require "$socketph";

$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
unless $port =~ /^\d+$/;
($name, $aliases, $type, $len, $thisaddr) =
gethostbyname($hostname);
if ($them =~ /^\d+/) # IP address specified
{
local(@a) = split (/\./,$them);
$thataddr = pack ('C4', @a);
}
else {
($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
}

$thataddr || die "Unknown host: $!";

$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
bind(S, $this) || die "bind: $!";
connect(S, $that) || die $date,"connect: $!";

select(S); $| = 1; select(STDOUT); # set socket to be command buffered


if ($child = fork) {
print S "$passwd\n";
print S "terminal length 0\n"; # dont need a 'more'
sleep 3; #some routers work better with this.
print S "en\n"; # tac modified
sleep 3; # tac modified
print S "$en_passwd\n"; # tac modified
sleep 3;
print S "sh ver\n"; # start config mode
sleep 3;
# print S "line vty 0 4\n"; # enter line mode
# sleep 3;
# print S "password short\n"; # set base level password
# sleep 3;
# print S "exit\n"; # exit line mode
# sleep 3;
# print S "enable secret stop\n"; # set enable level password
# sleep 3;
# print S "exit \n"; # exit from enable mode
# print S "wr mem \n"; # write changes to router memory
# sleep 5;
print S "quit\n";
sleep 3;
do dokill();
}
else {
while () {print;}
}


$

5 REPLIES 5
Peter Godron
Honored Contributor
Solution

Re: Looking for assistance in getting this script working...

Rob,
shouldn't you be using "use socket"?
I suspect one of these parameteres (S, $AF_INET, $SOCK_STREAM, $proto) is wrong for the platform.
Regards
Peter Godron
Honored Contributor

Re: Looking for assistance in getting this script working...

As a qiuck attempt, change the SOCK_STREAM = 2 and try again. This MAY work.
Rob Johnson_3
Regular Advisor

Re: Looking for assistance in getting this script working...

Changing SOCK_STREAM = 2 worked.

Here's the issue I'm facing now though. When I used the script before, the Cisco devices weren't setup to use TACACs. Where I'm at now, they're using TACACs. The only difference is this; when telneting to the device you are prompted for a Username: - not just a Password:. I tried adding a variable for defaultuser but it's not working. What am I doing wrong? Again, I'm not a scripter...

*********************************************
$ cat rcisco_dc.scr
#!/usr/bin/perl
#
# $Header: /home/aggarwal/lsrc/nocol/src/perlnocol/RCS/rcisco,v 1.3 1993/10/17 11:55:33 aggarwal Exp $
#
# rcisco - perl routine to execute a Cisco router command remotely
#
# Programmer: Christopher Sedore
# Revised by: John Wobus, jmwobus@mailbox.syr.edu
#
# (c) Syracuse University Computing & Network Services 1993
#
# No warranty is expressed or implied. Permission to copy and use is
# extended to all. Permission to redistribute is granted under the
# following conditions: it is not sold for profit; this copyright
# notice remains intact; the same permissions extend to the recipient;
# and if any changes are made, a notice is added so stating.
#
# Command Format:
#
# rcisco_mod []
#
# Signs on to Cisco router, executes command, and returns the
# result.
#
# - the name or IP number of the Cisco router.
# - the low level password for the router.
# - the enable password for the router.
# - the Cisco router command, e.g. "show interfaces".
# - the second Cisco router command.
# - the TCP port: 23 by default.
#
# Example Call:
#
# rcisco mycisco.excellent.edu "show hardware"
#
# This command signs on to myscisco.excellent.edu, executes the
# command "show hardware" and sends the result to the standard
# output.
#
# Depends upon:
# hostname - Unix command to list a host's name.
#
# To install this:
# (1) Assign the perl variables below appropriately.
# (2) Put this file whereever you want.
#
########################################################
#
#
# The command format is now:
#
# rcisco_wrnet.scr
#
#
require "ctime.pl";
#

$defaultrouter="10.21.21.14"; # Default router.
$defaultuser="ciscoworks"; # UserID of the router.
$defaultpasswd="password for CW"; # PW for the router.
$default_en_passwd="en pwd for CW"; # Enable PW for the router.
$defaultcommand="show version"; # Command to exec on router
$defaultport=23; # Default TCP port.

#
# main routine
#

#($them,$passwd,$en_passwd,$command,$command1,$port) = @ARGV;
($them,$user,$passwd,$en_passwd,$command,$command1,$port) = @ARGV;
$them = $defaultrouter unless $them;
$defaultuser = $defaultuser unless $passwd;
$passwd = $defaultpasswd unless $passwd;
$en_passwd = $default_en_passwd unless $en_passwd;
$command = $defaultcommand unless $command;
$port = $defaultport unless $port; # usually should be port 23
$date = &ctime(time);

$AF_INET = 2;
$SOCK_STREAM = 2;

$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child; }

# require "$socketph";

$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
unless $port =~ /^\d+$/;
($name, $aliases, $type, $len, $thisaddr) =
gethostbyname($hostname);
if ($them =~ /^\d+/) # IP address specified
{
local(@a) = split (/\./,$them);
$thataddr = pack ('C4', @a);
}
else {
($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
}

$thataddr || die "Unknown host: $!";

$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
bind(S, $this) || die "bind: $!";
connect(S, $that) || die $date,"connect: $!";

select(S); $| = 1; select(STDOUT); # set socket to be command buffered


if ($child = fork) {
print S "$passwd\n";
print S "terminal length 0\n"; # dont need a 'more'
sleep 3; #some routers work better with this.
print S "en\n"; # tac modified
sleep 3; # tac modified
print S "$en_passwd\n"; # tac modified
sleep 3;
print S "sh ver\n"; # start config mode
sleep 3;
# print S "line vty 0 4\n"; # enter line mode
# sleep 3;
# print S "password short\n"; # set base level password
# sleep 3;
# print S "exit\n"; # exit line mode
# sleep 3;
# print S "enable secret stop\n"; # set enable level password
# sleep 3;
# print S "exit \n"; # exit from enable mode
# print S "wr mem \n"; # write changes to router memory
# sleep 5;
print S "quit\n";
sleep 3;
do dokill();
}
else {
while () {print;}
}


$
Rob Johnson_3
Regular Advisor

Re: Looking for assistance in getting this script working...

It looks like I have it now. I forgot to add the print S "$defaultuser\n"; line.
Rob Johnson_3
Regular Advisor

Re: Looking for assistance in getting this script working...

Peter,
Thanks for your help on this. Even though your second response fixed the script, I only submitted 7 points because I thought I would need more help but I was able to get it working. What I did was submit more points to your first response.

Again, Thank you so much.