title: Paramiko ignores python script exception
body: <p>I have python code on my laptop using paramiko to send command through ssh on a remote RPi. I want to run 2 python scripts:</p>
  <ul>
  <li>create.py : to create a little json conf file on the RPi</li>
  <li>set.py : to update this conf file</li>
  </ul>
  <p>the paramiko code:</p>
  <pre><code>import paramiko
  
  server = &quot;192.168.1.94&quot;
  username = &quot;...&quot;
  password = &quot;...&quot;
  cmd_to_execute = 'cd argparse/; python create.py; python set.py -i test; cat sensor.conf'
  
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(server, port=22, username=username, password=password)
  ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)
  
  print(&quot; &quot;.join(ssh_stdout.readlines()))
  
  ssh.close()
  </code></pre>
  <p>In the set.py file, I used argparse to manually configure the conf file and allow something like:</p>
  <p><code>python set.py -i 10.10.10.10 -s 2 -r 0</code></p>
  <p>create.py:</p>
  <pre><code>import os
  
  path = &quot;sensor.conf&quot;
  
  with open(path, 'w') as f:
      f.write('{&quot;ip&quot;: null, &quot;room_id&quot;: null, &quot;sensor_id&quot;: null}')
  </code></pre>
  <p>set.py file:</p>
  <pre><code> import argparse
   import ipaddress
   import json
  
   path = &quot;sensor.conf&quot;
  
   parser = argparse.ArgumentParser(description='Set fields in sensor config file.')
  
   parser.add_argument('-i', '--ip', help='Sensor IP as string (xxx.xxx.xxx.xxx)')
   parser.add_argument('-r', '--room', type=int, help='Sensor room ID (integer from     server)')
   parser.add_argument('-s', '--sensor', type=int, help='Sensor ID (integer from        server)')
  
   args = parser.parse_args()
  
   with open(path, 'r') as f:
       data = json.load(f)
  
   if args.ip:
       try:
           ipaddress.IPv4Network(args.ip)
           data['ip'] = args.ip
       except Exception as e:
           print(e)
  
   if args.room or args.room == 0:
       data['room_id'] = args.room
  
   if args.sensor or args.sensor == 0:
       data['sensor_id'] = args.sensor
  
   with open(path, 'w') as f:
       json.dump(data, f)
  
   print(data)
  </code></pre>
  <p>I use ipaddress.IPv4Network() as a way to ensure the IP given is well formatted, within a try/except scope.</p>
  <p>When I am manually connected to my RPi with ssh, and try <code>python set.py -i test</code> I do have an exception handled and nothing is written in my file.</p>
  <p>But with paramiko, the exception seems to not be handled or not even occurring? because my file contains &quot;test&quot; in the 'ip' field.</p>
  <p><strong>EDIT</strong>
  I found a better way to check for a correct IP, and it fixed the problem with paramiko: I replaced the try/except with a function that returns True/False if the string follows the format of an IP address.</p>
  <p>I still don't understand why this happened with the try/except, any idea?</p>
  <p>Many thanks</p>