python - Sudo login and executing cat command using pexpect -
my requirement ssh remote machine. alter user using "sudo su usr2 -l" , cat file. of output in string using python. have done far , 1st ssh seems working fine. not execute commands after 1st login. also, not sure difference between send , sendline here.
import pexpect kid = pexpect.spawn('ssh usr1@host') child.expect('password:') child.sendline('mypassword\r') child.expect('usr1@') child.sendline('sudo su usr2 -l\r') child.expect('.*assword:') child.send('mypassword\r') child.expect('usr2') child.sendline('cat /path/to/file\r') cmd_show_data = child.before cmd_output = cmd_show_data.split('\r\n') info in cmd_output: print info
python ssh pexpect
No comments:
Post a Comment