Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

subroutine For ssh

by GHMON (Novice)
on Oct 08, 2019 at 06:57 UTC ( [id://11107191]=perlquestion: print w/replies, xml ) Need Help??

GHMON has asked for the wisdom of the Perl Monks concerning the following question:

Hi

i want to write a subroutine for SSH Connection then call the sub in another line , now how put arguments in the sub

sub SSHApp { #SSH Connection to Applaince my $ssh_1 = Net::Appliance::Session->new({personality => 'ios', tr +ansport => 'SSH', host => "$MyIP",}) ; $ssh_1->connect({ username => "$MyUser", password => "$MyPass" + }) ; $ssh_1->begin_privileged({ password => 'ai'}) ; print $ssh_1->cmd('sh version') ; $ssh_1->end_privileged ; $ssh_1->close; } my $result = SSHApp(); print "$result" , "\n" ;

Replies are listed 'Best First'.
Re: subroutine For ssh
by marto (Cardinal) on Oct 08, 2019 at 07:18 UTC
Re: subroutine For ssh
by rjt (Curate) on Oct 08, 2019 at 13:54 UTC

    martos links and suggestions are spot on. Read them. In addition, whenever I need to teach myself a new language concept, I often find it helps to start with a simpler example.

    use Test::More; # Add two numbers, provided as arguments sub add_two_numbers { # TODO - Implement me! } is add_two_numbers(3,5), 8, '3 + 5 = 8';

    Once you figure out how to implement the body of add_two_numbers(), your SSHApp() sub will be easy. The advantage is add_two_numbers() is a lot easier to test and focuses on the thing you do not yet know how to do, which is subroutine arguments.

    There's a good chance you'll also want to figure out how to handle missing or undef arguments, or arguments that are otherwise invalid (what happens when you run add_two_numbers(3, 'banana'), for example. How (or indeed if) you handle invalid arguments depends on your specific requirements. It is easy to extend the above template with additional tests to handle those cases, as needed. There are other modules like Test::Exception that may also be of interest for writing good tests.

    use strict; use warnings; omitted for brevity.
Re: subroutine For ssh
by davido (Cardinal) on Oct 08, 2019 at 15:28 UTC

    In addition to the other resources mentioned in this thread, I would suggest the best document to read before going any further with Perl would be perlintro, which contains a section about writing subroutines. The value in perlintro is that it contains just enough on a broad range of Perl topics to inform the rest of your progress toward learning the language, yet takes less than a half hour to read in its entirety.

    Learn Perl in about 2 hours 30 minutes is another gem; one that the Mojolicious project lists in its documentation as reference material for people newly coming to Perl.

    perlintro takes 20-30 minutes to read. Learn Perl in about 2 hours 30 minutes takes...well, 2 hours and 30 minutes. But the sections on subroutines take about 5 minutes total, and should help you to come to a better understanding.

    For your specific case, you need something like this:

    sub SSHApp { my ($MyUser, $MyPass, $MyIP) = @_; # ...the rest of your code goes here. # You probably need: # my $output = $ssh_1->cmd('sh version'); return $output; }

    Dave

Re: subroutine For ssh
by GHMON (Novice) on Oct 09, 2019 at 05:15 UTC

    Hi

    TNX for all comments

    it was ok with this type

    sub SSHApp { my ($MyIP , $MyUser , $MyPass , $MyPass2 , $command ) = @_ ; #SSH Connection to Applaince my $ssh_1 = Net::Appliance::Session->new({personality => 'ios', tr +ansport => 'SSH', host => "$MyIP",}) ; $ssh_1->connect({ username => "$MyUser", password => "$MyPass" + }) ; $ssh_1->begin_privileged({ password => "$MyPass2"}) ; print $ssh_1->cmd("$command") ; $ssh_1->end_privileged ; $ssh_1->close; }

      Couple of style nits:

      It's not necessary to requote everything as you've done in several places (e.g. "$MyUser" or "$command"). It's extra, unneeded visual noise and (in weird corner cases) could trigger behavior you don't intend (quoting something which has stringification overloaded; alternately if down the road the underlying API expects a HASHREF for an argument where it used to expect a SCALAR you've now got to go through and remove them).

      And more of a personal preference but WRT your variable naming, the "My" prefix on everything doesn't really gain you anything. Were you to keep it it's more common to use lowercase underscore delimited local lexical variable names rather than CamelCase (e.g. $unprivileged_password or $privileged_password might be better than $MyPass and $MyPass2).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11107191]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found