Python raw_input faults and returns - bash: line 1: <INPUT>: command not found

I wrote a script that sets up an SSH tunnel and connects to the database over that tunnel.

Extremely simplified short description (obvious parameters and additional logic omitted):

sshTunnelCmd = "ssh -N -p %s -L %s:127.0.0.1:%s -i %s %s@%s" % (
   sshport, localport, remoteport, identityfile, user, server
)
args = shlex.split(sshTunnelCmd)
tunnel = subprocess.Popen(args)
time.sleep(2)
con = MySQLdb.connect(host="127.0.0.1", port=localport, user=user, passwd=pw, db=db)
## DO THE STUFF ##
con.close()
tunnel.kill()

      

Below are the shell-equivalent commands , and I've tested both commands and script to work in a "clean client" environment, that is, after a reboot.

ssh -N -p 22 -L 5000:127.0.0.1:3306 user@server
mysql --port 5000 -h 127.0.0.1 -u dbuser -p

      

SSH login with keys and ~ / .ssh / config server configured as

Host server
  Hostname F.Q.D.N
  Port 22
  User user
  ControlMaster auto
  ControlPath ~/.ssh/master-%r@%h:%p
  ControlPersist 600
  IdentityFile ~/.ssh/id_rsa

      

The ## DO THE STUFF ## section has code that tries to connect to the database as regular users. If an exception is thrown, manual root credentials are prompted, normal users are created and continue to do stuff (normal queries, all manually checked and running in python code in a pure client environment).

ruz = raw_input('root user? ')
print (ruz)
rup = raw_input('root password? ')
print (rup)

print ("Root connecting to database.")
try:
  cxroot = MySQLdb.connect(host=host, port=port, user=ruz, passwd=rup)
  cur = cxroot.cursor()
except MySQLdb.Error, e:
  print ("Root failed, sorry.")
  print "Error %d: %s" % (e.args[0],e.args[1])
  print ("GAME OVER.")
  return -1

      

In a pure client, the first and several subsequent executions work well, including when I try to validate the script and delete the user's backend. However, at some point it hangs strangely after the second raw_input in the code block above . Output:

root user? root
root
root password? s3cReTsTr1n9
-bash: line 1: s3cReTsTr1n9: command not found

      

The only thing I can do at this point is kill the process or press CTRL + C followed by the following trace:

^CTraceback (most recent call last):
  File "./initdb.py", line 571, in <module>
    main()
  File "./initdb.py", line 526, in main
    connection = connectDB ('127.0.0.1', localport, dbuser, dbpw, db)
  File "./initdb.py", line 128, in connectDB
    rup = raw_input('root password? ')
KeyboardInterrupt

      

Another unexpected symptom I noticed is that keyboard input into a terminal window (I run this in bash terminal in Xubuntu 14.04LTS) becomes falsely unresponsive so I have to close the terminal tab and start a new tab. This clears up the keyboard input, but not the script.

I tried to find a solution, but the usual search engines did not work for me, perhaps because I do not quite understand what is going on. I suspect that keyboard input is somehow redirected to a process, perhaps a tunnel sub-process, but I cannot explain why the first raw_input is working as expected and the second is not.

I am also uncomfortable with how I create the tunnel, so any advice for making tunnels more reliable is welcome . In particular, I would like to have finer grained control over the creation of the tunnel, rather than waiting an arbitrary two seconds for the creation of the tunnel, because I have no feedback from this subprocess.

Thank you for sharing your time and experience.

+3


source to share


3 answers


There are two sections to my answer: how would I diagnose this, and how would I do it.

To begin with, I suggest using a hint that prevents you from doing some research.

There are two approaches here:

  • Just type hostname

    (or whatever) to see where it works
  • Enter bash

    , or if the remote end has an X server, add -X

    ssh to your command, then enter a terminal program ( xterm

    , gnome-terminal

    etc.). In your new shell, you can poke around to see what happens.

If you have determined that it is working on the client side, you can diagnose it with strace

:

strace -f -o blah.log yourscript.py

      



... where you have to enter a search string for the password and then search for it in blah.log

. Because of the flag, -f

it will print the PID of the process that tried to execute it; from there from there, you will probably find that the PID started at a fork from a different PID. This PID is what it was trying to accomplish, so you should be able to investigate from there.


How I did it: I am still pretty new to python, so I would tend to use perl or expect. Down the perl path you can look at:

  • Net :: SSH :: Tunnel ; this is probably the first one I would use.
  • Use open or open3 , then do something hacky:
    • wait for stdout in the process to have available text; you will have to get rid of -N

      for it and you will be at the mercy of remote automatic logoff.
    • One of the answers to ssh-check-if-a-tunnel-is-alive
  • Net :: SSH :: Expect (like this post , although I haven't looked at its implementation, so you'll have to make your choice on this one). This or the "real" expectation is probably too high, but you can find a way that I'm sure.

Although ruby ​​has a gem as perl Net :: SSH :: Tunnel, I don't see a pip for python. This question and this one both discuss this, and they seem to indicate that you are limited to either start, subprocess, or with paramiko .

0


source


Can you customize the server the way you like? Then try vpn connection instead of ssh port forwarding. This will make it easier to reconnect without affecting your application, so the tunnel can be more stable.



For the raw_input problem, I can't figure out why this is happening, but maybe the ssh command in the shell is interfering with your terminal? If you really want to integrate ssh tunnel, you can look at some python modules for handling ssh.

0


source


-bash: line 1: s3cReTsTr1n9: command not found

I got the same error as the -bash not found command, although I was just accepting raw_input () / input (). I've tried this in 2.7 and 3.7.

I was trying to run a client-server program on the same Mac. I had two files server.py and client.py. Each time in the terminal, I first ran server.py in the background and then ran client.py.
Terminal 1: python server.py &
Terminal 2: python client.py
Every time I get the error "-bash: xxxx: command not found". xxxx that's what I gave.

Finally, after spending 5 hours on it, I stopped running server.py in the background.
Terminal 1: python server.py
Terminal 2: python client.py

And viola it worked. raw_input and input no longer threw this error.

I'm not sure if this will help. But this is the only post I found on the internet and I had the same problem as me. And I thought maybe it would help.

0


source







All Articles