carrotcakeslayer
carrotcakeslayer

Reputation: 1008

Open tabs in gnome-terminal from python script

I'd like to open different gnome-terminal tabs and connect via ssh to different servers. To do this, I used to use a little bash script.

#!/bin/sh

hosts=("172.16.1.183" "172.16.1.184")
pass=Password123!

for host in "${hosts[@]}"; do
  gnome-terminal --tab -- sshpass -p $pass ssh -o StrictHostKeyChecking=no root@$host
done

I'm forcing myself to start scripting in python, as I would like to learn it. That's why now I'm trying to do this with this python script instead.

#!/usr/bin/env python3

import subprocess

for servidor in ['172.16.1.183', '172.16.1.184']:
  subprocess.run(['gnome-terminal', '--tab', '--', 'sshpass', '-p', 'Password123!', 'ssh', '-o', 'StrictHostKeyChecking=no', 'root@',servidor])

It seems that for a second the tabs do open but then they close. As I don't know how to see any kind of log, it is hard for me to understand what's happening.

Could you please help me to understand what's happening? Is there any way to run these scripts with a "verbose" option like in bash with "-x" or things like -v, -vv, -vvv for instance?

Thank you very much!

edit: I'm using PYTHONVERBOSE=1 but I can't understand that output :/ is not like I can see an "error" anywhwere or even follow the step-by-step execution to understand what's happening.

Upvotes: 1

Views: 801

Answers (1)

Sunitha
Sunitha

Reputation: 12015

You are passing the username and hostname as two list elements. Instead it should be just one element. Change 'root@',servidor to 'root@'+servidor

Your code can be written in a better way as follows

cmd = 'gnome-terminal --tab -- sshpass -p sshpass ssh -o StrictHostKeyChecking=no root@{}'
for servidor in ['172.16.1.183', '172.16.1.184']:
    subprocess.run(cmd.format(servidor).split())

Upvotes: 1

Related Questions