How to get a program session with ssh state?

I am working on an application that needs to send commands to remote servers. Sending commands is quite simple using many SSH client libraries.

However, I would like the shell state to be maintained between each command (that is, the current working directory, environment variables, etc.). All the client libraries I have seen do not do this. For example, the following code example that does not do what I want:

use Net::SSH::Perl; my $server = Net::SSH::Perl->new($host); $server->login($user, $pass); $server->cmd('cd /var'); $server->cmd('pwd'); # I _would like_ this to output /var 

Other tasks will be performed between sending commands, so combining commands like $server->cmd('cd /var; pwd') not acceptable.

+6
source share
2 answers

Net :: SSH :: Expect does what you want, although the "Wait" method is not completely reliable, because it will analyze the output of your commands and attempts to detect when the shell prompt appears again.

+2
source

I'm not sure what you are doing exactly, but you can just start one SSH session. If you really cannot do this, perhaps you can just use absolute paths for everything.

0
source

Source: https://habr.com/ru/post/893199/


All Articles