Link to home
Start Free TrialLog in
Avatar of Shannon Adams
Shannon Adams

asked on

Help with shell script

I need to have a script that will automatically log into a network device, reset a port, and exit the network device.  The script will be passed three values - ip address, device password and port number.  The contents of the shell script are as follows:

telnet $1
<need a carriage return here>
cli                                    
su                                      
$2                  
admin                        
reset po = $3
hangup
exit  

I am not sure how to add a carriage return after each command.  I would appreciate any advice on how to do that and if there are any other commands I need to add to the script.
Avatar of Tintin
Tintin

You are probably going to need to use 'expect' here, but it does depend on how your network device handles passwords.

Try

telnet $1 `cat <<EOF

cli                                    
su                                      
$2                  
admin                        
reset po = $3
hangup
exit  
EOF`

Ignore the above, it is incorrect.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

It could easily be done in perl with the Net::Telnet module, see the simple example below:
#!/usr/bin/perl
use strict;
use Net::Telnet;

my $telnet = new Net::Telnet( Errmode=>'die',
                                       Prompt => '/\# $/i');
$telnet->open('yourhostname');
$telnet->login('user', 'password');
print $telnet->cmd('/usr/bin/pwd; ls');