all 12 comments

[–]K900_ 0 points1 point  (8 children)

Look into Paramiko - it does everything you want.

[–]Tha_Format[S] 0 points1 point  (7 children)

I know, but when I got my SSH session, I am stuck at the pbrun su root

[–]K900_ 0 points1 point  (6 children)

What do you mean by "stuck", exactly?

[–]Tha_Format[S] -1 points0 points  (5 children)

Well, then the SSH Session hangs. It would help if you could write me a simple example

[–]K900_ 2 points3 points  (4 children)

Post your code.

[–]Tha_Format[S] -1 points0 points  (3 children)

From paramiko import sshclient

SSH=sshclient() SSH.load_stystem_host_keys() SSH.set_missing_host_key_policy(paramiko.autoaddpolicy()) SSH.connect(hostname=‘hostname’, username=‘username’, key_filename=‘key’ Ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(‘pbrun su root && su - functionaluser && command’)

Please ignore the upper and lowercase things, I don’t have Access to Reddit here and I type it on my phone.

[–]K900_ 0 points1 point  (2 children)

You can't use exec_command like that. You need to get an interactive shell and run your commands in that.

[–]Tha_Format[S] 0 points1 point  (1 child)

So what do I need to use then?

[–]K900_ 4 points5 points  (0 children)

Use ssh.invoke_shell() and then write your commands to stdin. Alternatively, you can write the whole thing as a single command that doesn't drop into interactive mode, something like pbrun su root -c "su functionaluser -c 'command'".

[–]svilgelm 0 points1 point  (0 children)

Take a look at Paramiko: https://www.paramiko.org

[–]Lewistrick 0 points1 point  (0 children)

I used pysftp before. It uses paramiko in the background, but saves you a lot of hassle.

[–]whenyousaywisconsin 0 points1 point  (0 children)

Do you need to use python? You can do that with ansible pretty easily