Tuesday, March 29, 2022

[SOLVED] pexpect error when trying to scp in a nohup process

Issue

I have an scp function as follows:

def scp_to38():
    child = pexpect.spawn(f"scp {tar_file_path} [email protected]:/app2/upload/")
    child.expect(f"[email protected]'s password:")
    child.sendline("password")
    child.interact()
    print(f'{tar_file_path} SENT TO 38:/app2/upload/')

and it is throwing me an error saying a problem with child.interact()

 Traceback (most recent call last):
  File "scan_for_upload.py", line 39, in <module>
    scp_to38()
  File "scan_for_upload.py", line 26, in scp_to38
    child.interact()
  File "/app/anaconda3/envs/python37-1/lib/python3.7/site-packages/pexpect/pty_spawn.py", line 788, in interact
    mode = tty.tcgetattr(self.STDIN_FILENO)
termios.error: (25, 'Inappropriate ioctl for device')

I am running the script in a nohup process, I believe that is causing this issue. Because when I run the script in the console, it works fine. How do I interact with the scp password in a nohup process using pexpect module? (I can't install any other external modules in my environment)


Solution

interact() is for manual interaction with the spawned process and it requires to be running on a tty but nohup provides no tty. You can just replace child.interact() with child.expect(pexpect.EOF, timeout=None).



Answered By - pynexj
Answer Checked By - David Marino (WPSolving Volunteer)