Python Forum
Trying to convert my Expect/bash to Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to convert my Expect/bash to Python
#1
Good Morning,

I am trying to gain some python knowledge and convert scripts Ive written in expect and bash.
I do not have permission to load modules so Netmiko/paramiko and Pexpect/pxxsh is not available to me.
I will need to find solutions using default modules where possible.

I often need to connect to network devices. The following works but Im not all that sure what is happening.
I found this script online.
My question is .. once I log in .. how do I execute commands on the network device.
Im also reviewing the subprocess module. I dont quite understand whats going on with the ClientSocket assignment. and I also dont understand whats going on with following portion of the ssh execution.



{0}".format(port).split()
In bash, I can use set -x to see the code execute. Is there a way I can do this in python ?
I am on a Centos 6 V .. old and crusty Python 2.6.4


#!/usr/bin/python
import socket
import subprocess

ClientSocket = socket.socket()
try:
    ClientSocket.connect(("10.10.10.10", 22))
    port = 22
except socket.error:
    ClientSocket.connect(("10.10.10.10", 23))
    port = 23
finally:
    command = "sshpass -pMYPASSWORD ssh -t -t [email protected] -p {0}".format(port).split()
    subprocess.call(command)
    subprocess.call("dir")
    ClientSocket.close()
Thanks for the help in advance.
Reply
#2
Not being able to load packages is a huge problem.
But you can connect using sockets?
doesn't make sense.
Have you tried pip?
Try: pip install expect

or pip3 install expect
Reply
#3
Yes. I do have root .. but the admin gods dont want anything loaded on the VM. Will get spanked if I do it and they find out.
Reply
#4
You don't need root access to install packages (or at least shouldn't)
Reply
#5
Then as user:
pip3 install expect --user
Or make first a virtual environment:

python3 -m venv env
source env/bin/activate
pip install expect
Before you run your script, you must have activated the virtual environment with
source path/to/env/bin/activate
Or you run your script with:
path/to/env/bin/python your_script.py
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 349 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,031 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Call a bash script from within a Python programme Pedroski55 6 2,436 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  Showing and saving the output of a python file run through bash Rim 3 2,426 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Please help me. about self.expect() leeyoung 0 1,302 Nov-23-2020, 10:25 PM
Last Post: leeyoung
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,881 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  can I use 2 child.expect at the same time? korenron 0 1,519 May-25-2020, 10:41 AM
Last Post: korenron
  Make bash call my own Python modules Pedroski55 10 5,139 Apr-22-2019, 04:04 PM
Last Post: snippsat
  Python and bash command confusion MuntyScruntfundle 0 2,051 Oct-11-2018, 04:52 PM
Last Post: MuntyScruntfundle
  Python bash application Ikitissimo 1 41,826 Apr-11-2018, 11:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020