1

I'm using pexpect to make a ssh connection to a server. Login shell of the server is not bash. I want to execute multiple commands with one connection. I tried sendline() but it doesn't treat it as a command it just enters the text. So maybe I need a way to send "Enter" signal through pexpect. The next use case for this is to execute commands from certain directories so I first have to go there and execute the command. is there any way to do this? or is there any better way to execute multiple commands on remote server through password authentication?

yabhishek
  • 408
  • 4
  • 14
Uday Swami
  • 395
  • 8
  • 22
  • look at [fabric](http://www.fabfile.org/) – jfs Sep 26 '14 at 10:50
  • I couldn't find python code for how to provide password with fabric.they just say there is --password option. pls provide if you have any – Uday Swami Sep 26 '14 at 10:55
  • what happens if you search for "fabric password" e.g., in google? – jfs Sep 26 '14 at 10:59
  • fab doesn't support password auth through env variables from version 1.7. and fab executes shell commands with /bin/bash and my target server doesn't have unix os. so shell commands don't work – Uday Swami Sep 26 '14 at 13:10
  • [the latest (1.10) docs](http://docs.fabfile.org/en/latest/usage/env.html#environment-as-configuration) still mentions `env.password`. You can [specify your custom command instead of `/bin/bash` (`env.shell`).](http://www.fabfile.org/faq.html#faq-bash). – jfs Sep 26 '14 at 19:55
  • I tried that it still prompts for password and what should I set env.shell, I don't have shell in that OS it is custom OS. I tried it setting empty but gave parsing error. Can't I use pexpect? – Uday Swami Sep 29 '14 at 06:33
  • set `env.shell` to whatever thing that allows you to execute multiple commands on your custom OS. If there is no such thing then how do you expect to execute multiple commands using `fabric`, `pexpect`, [`paramiko`](http://stackoverflow.com/q/6203653/4279), or whatever in a single ssh session? Password is a separate issue, unrelated to executing multiple commands (create a minimal complete code example that tries to execute a single command and post it as a separate question if there is none yet). – jfs Sep 29 '14 at 07:33
  • fabric asks me to specify which shell to use but I don't know that and pexpect just runs command as user would without bothering over which shell to use, so I would just like to continue that with one connection variable – Uday Swami Sep 29 '14 at 17:36
  • yes, you can execute exactly *one* command in a single session without a shell (or its substitute). Your question is about *multiple* commands in a single ssh session. You have to bother if you want multiple commands per session – jfs Sep 29 '14 at 17:42

1 Answers1

3

One way to get around this issue is to first start a bash and then run your commands. One thing to remember is that it is good to call child.expect after each command sent. Below is an example of how to connect to a server then run ls. Trivial yes but hopefully is helpful. For commands like 'cd' you should just be able to expect and empty string since there is not typically output

import pexpect
import sys

child = pexpect.spawn("bash")
child.logfile = sys.stdout
child.sendline("ssh user@hostname")
child.expect("Some string to match")
child.sendline("ls")
child.expect("Some directory or file")
child.sendline("cd /path/to/file/or/directory")
child.expect("")
child.sendline("ls")
child.expect("A certain filename")

When you run your ssh command you can also do something like

ssh -t user@hostname 'cd /path/to/directory'

That will take you right to a directory when your ssh connection starts

dbogle
  • 46
  • 5