1

I had script on bash where I generated username, password, ssh-key for user. Part for creating of ssh-key:

su $user -c "ssh-keygen -f /home/$user/.ssh/id_rsa -t rsa -b 4096 -N ''" 

How can I do the same in Python with os.system? I tried this:

os.system('su %s -c "ssh-keygen -f /home/%s/.ssh/id_rsa -t rsa -b 4096 -N ''"', user) TypeError: system() takes at most 1 argument (2 given) 

Also I tried:

os.system('su user -c "ssh-keygen -f /home/user/.ssh/id_rsa -t rsa -b 4096 -N ''"') 

Of course, it doesn't work either.

6
  • note that the same command with $user would work with os.system since it creates a shell.CommentedNov 15, 2017 at 13:17
  • os.system('su $user -c "ssh-keygen -f /home/$user/.ssh/id_rsa -t rsa -b 4096 -N ''"')CommentedNov 15, 2017 at 13:17
  • @Jean-FrançoisFabre sorry, it does not see key -N option requires an argument -- N usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]
    – Piduna
    CommentedNov 15, 2017 at 14:06
  • ok we're getting close: os.system('su $user -c "ssh-keygen -f /home/$user/.ssh/id_rsa -t rsa -b 4096 -N \'\'"'CommentedNov 15, 2017 at 14:07
  • @Jean-FrançoisFabre good. How i forgot about this. And my example with python is working. Thanks
    – Piduna
    CommentedNov 15, 2017 at 14:09

3 Answers 3

2

Format your instructions with the os package; for instance:

import os user = 'joe' ssh_dir = "/home/{}/.ssh/id_rsa".format(user) os.system("ssh-keygen -f {} -t rsa -b 4096 -N ''".format(ssh_dir)) 
1
  • You spoke right, @JohnKugelman: I edited with the command from the question.CommentedNov 15, 2017 at 14:35
1

os.system is very close to a bash command line because it uses an underlying shell (like its cousins subprocess.call... using shell=True)

In your case, there's little interest using subprocess since your command runs a command, so you cannot really use argument protection by subprocess fully.

Pass the exact command, but the only change would be to protect the simple quotes, else python sees that as string end+string start (your string is protected by simple quotes already) and they're eliminated.

Check this simpler example:

>>> 'hello '' world' 'hello world' >>> 'hello \'\' world' "hello '' world" 

that's a kind of worst-case when you cannot use either double or simple quotes to protect the string because you're using the other flavour within. In that case, escape the quotes using \:

os.system('su $user -c "ssh-keygen -f /home/$user/.ssh/id_rsa -t rsa -b 4096 -N \'\'"') 
    1

    Use the subprocess module:

    import subprocess username = 'user' result, err = subprocess.Popen( 'su %s -c "ssh-keygen -f /home/%s/.ssh/id_rsa -t rsa -b 4096 -N ''"' % (username, username), stdout=subprocess.PIPE, shell=True ).communicate() if err: print('Something went wrong') else: print(result) 

    Edit: this is the 'fast' way to do that, you should't use shell=True if you can't control the input since it allows code execution as said here

    3
    • sorry, it does not see key -N option requires an argument -- N usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]
      – Piduna
      CommentedNov 15, 2017 at 14:07
    • look please ^ option requires an argument -- N
      – Piduna
      CommentedNov 15, 2017 at 14:07
    • Sorry I didn't try the snippet, I' ll fix that when i' m back home
      – M. Matt
      CommentedNov 15, 2017 at 14:18

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.