Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Switch user command not working user Net:SSH2 module

by salva (Abbot)
on Oct 29, 2013 at 09:21 UTC ( #1060127=note: print w/replies, xml ) Need Help??


in reply to Switch user command not working user Net:SSH2 module

Net::SSH2 runs every command in a different shell session. Read the FAQ entry "Can't change working directory" from the Net::OpenSSH docs for a more detailed explanation.

There are two ways to do what you want:

One is to start a shell on the remote shell and then simulate an interactive dialogue with it (usually, you use Expect for that, though Expect doesn't work on Windows*). This is hard and error prone, as you will have to look into the command output for the prompt in order to detect when some command is done.

The other way is to combine the sequence of commands into a single one. But, su usually asks for the password and so you will have to send it through its tty.

su root -c "whoami"

An easier way is to use sudo, that can be configured to not ask for a password, or alternatively, in recent versions, to get the passwords from stdin. For instance, using Net::SSH::Any:

my $ssh2 = Net::SSH::Any->new('192.168.XX.XXX, user => 'root', passwor +d => 'root'); print $ssh2->capture({stdin_data => "$sudo_passwd\n"}, 'sudo', '-Sk', '-U', $user, '-p', '', '--', 'whoami');

*) unless you use the perl from Cygwin.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060127]
help
Chatterbox?
[1nickt]: marto would like to hear that. Of course he speaks very deliberately and dramatically, allowing pauses between words, so if anybody could pull it off it would be him, or someone impersonating him ...
[Eily]: Discipulus that's a chess joke BTW right? Because bishops don't walk straight :D
[choroba]: stop making chessy jokes about bishops!
[choroba]: I mean cheesy
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-09-26 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (294 votes). Check out past polls.

    Notices?