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

checking command suggestions with Net::SSH::Expect

by himanshu.chauhan (Initiate)
on Jul 02, 2020 at 15:20 UTC ( #11118824=perlquestion: print w/replies, xml ) Need Help??

himanshu.chauhan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm new here and need to implement a certain automation for linux process. But I'm stuck at a point where first I need to check a command suggestions before executing the actual command depending on certain condition. By command suggestion, I mean the suggestion or next steps displayed for a command when we press <TAB><TAB>. I have tried doing the following things but not even a single one worked:

$connection = Net::SSH::Expect(); #connection to a server $connection->send("\t\t"); $connection->get_expect()->send("\t\t"); $connection->send("011"); $connection->get_expect()->send("011"); # octal for tab

So basically once sending <TAB><TAB> characters, I can read suggestions using $connection->read_all() but not able to figure out how to get suggestions using tab. It would be great if any of you amazing monks can guide me through this mountain!

Thank you in advance!

Replies are listed 'Best First'.
Re: checking command suggestions with Net::SSH::Expect
by haukex (Bishop) on Jul 02, 2020 at 19:49 UTC

    I'm not sure whether this will help, but have a look at Important Nodes About Dealing with SSH and Pseudo-Termials in the module's docs.

    What you're trying to do is trigger the shell's autocompletion, which is very dependent on the shell, for example bash has Programmable Completion, but it's up to the vendor to distribute the scripts that feed this functionality, for example Debian-based systems have the package bash-completion that contains a very complex set of scripts. You'll probably see differences in the autocompletion from version to version, and very likely from vendor to vendor. Using this in an automated fashion is likely not going to be very robust.

    This seems to me like it might be an XY Problem. What exactly are you trying to figure out via the shell completion? If you want to know if a certain program is somewhere in the PATH, try which.

Re: checking command suggestions with Net::SSH::Expect
by perlfan (Priest) on Jul 04, 2020 at 07:05 UTC
    My initial thought was to search your PATH, but if you know what you want which is certainly the tool you should use as suggested below. I also thought at first you wished to trigger the the bash readline support for history, ctrl-r. If that's the case (probably not), then you may also wish to directly peek at $HOME/.bash_history.

    For a more portable approach, to the solution to the first problem, it's also rather trivial to break PATH into a list of directories, then to use find to facilitate the listing of their contents in terms of their full path location:

    for d in $(echo $PATH | tr ':' '\n'); do find $d -type f; done
    Or as sent via an ssh command,
    ssh you.remote.host "for d in \$(echo \$PATH | tr ':' '\n'); do find \ +$d -type f; done"
    find is also pretty darn flexible, and if you do not wish to use it then there are other utilities you may use to list the contents of each directory in PATH like ls.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2020-08-10 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?