0

I want to use an expect or Pexpect file to automate a shell command like:

 for host in HostList:
      ssh -t kitty@host 'sudo nohup bash -c "./tcp_sender > /dev/null 2>&1 &"'

if I manually input the command, there are 3 possibilities for case 2, I have to input a yes, for the other cases I don't need to input anything password is not needed.

1)

   [kitty@goeland kitty]$ ssh -t kitty@202.171.61.205 'sudo nohup bash -c "./tcp_sender > /dev/null 2>&1 &"' 
    nohup: ignoring input and appending output to `nohup.out'
    Connection to 202.171.61.205 closed.

2)

    [kitty@goeland kitty]$ ssh -t kitty@202.171.61.205 'sudo nohup bash -c "./tcp_sender > /dev/null 2>&1 &"' 
    The authenticity of host '202.171.61.205 (202.171.61.205)' can't be established.
    RSA key fingerprint is ff:0b:9c:a9:72:52:8f:53:0d:04:d5:ea:d9:3c:56:37.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '202.171.61.205' (RSA) to the list of known hosts.
    nohup: ignoring input and appending output to `nohup.out'
    Connection to 202.171.61.205 closed.

3) different errors, at the moment I haven't got yet

for 3, I want to know the errors

so how to use expect or Pexpect to do this? preferably Pexpect thanks!

misteryes
  • 2,167
  • 4
  • 32
  • 58

1 Answers1

0

You have use pxssh modules, this is the This class extends pexpect.spawn to specialize setting up SSH connections. This adds methods for login, logout, and expecting the shell prompt. that module handle in automatically in your 2 question 'yes'

example code:- Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko

and cache your error login:- sample code is like

host = pxssh.pxssh()
host.login (hostname, username, password)
host.logfile = open(/tmp/logfile.txt, "w")
host.sendline("ls -l")

open the logfile.txt your terminal output are store in text file

Community
  • 1
  • 1
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55