0

I want to change PS1 when i ssh another centos

    strshell = 'PS1="remote!"'
    os.system("ssh -i %s -l%s -p%s %s %s"%(SSH_KEY,SSH_USER,SSH_PORT,des,strshell))  

ssh quit automatically. I want to stay in this shell.How to achieve it.

I just change another way to do this

 tin = ' sudo -i ; export PS1="Remote! \W :"; exec /bin/bash -i'
 os.system("ssh  -i%s -l%s -p%s %s -t%s" % (SSH_KEY, SSH_USER, SSH_PORT, des, tin))  

but i changed my local machine prompt not the remote one. why?
os.system is a python function

3
  • I think to get a shell that you can actually use you'll need to invoke a terminal emulator such as xterm/gnome-terminal/terminator from within that os.system, and then launch your SSH command from within that. Otherwise only your Python program will have access to the os.system(..ssh..) process that it launched.
    – slm
    Commented Aug 26, 2014 at 3:39
  • Have you checked pexpect?
    – Simply_Me
    Commented Aug 26, 2014 at 3:46
  • I achieved the same by changing the PS1 setting in the .bashrc of all remote machines I was working on (30+ servers). This means some setup work, but then you can just ssh into the machines.. Commented Aug 26, 2014 at 8:21

2 Answers 2

3

sudo -i runs a login shell as the target user. The rest export PS1... would only be executed after that shell has terminated.

Also, your ~/.bashrc is likely to override PS1 to passing it in the environment will probably not help.

You could try:

ssh -t host 'sudo env PROMPT_COMMAND="PS1=\"Remote! \W: \"
             unset PROMPT_COMMAND" bash -l'

(assuming the target user's ~/.bashrc doesn't set $PROMPT_COMMAND).

2
  • Yeah,it's right.When i run my script,export PS1...would be executed after terminated. Why? can you explain it more details? Commented Aug 26, 2014 at 7:22
  • I want to still link to this remote machine after i change PS1, a little confused ... Commented Aug 26, 2014 at 7:59
0

This appears to work:

ssh -t localhost "echo '. /dev/fd/2'|sudo bash -sil 2<<\FILE        
PS1='my prompt : '
exec 2>/dev/tty
exec </dev/tty
FILE
"
[sudo] password for mikeserv: 
my prompt : id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),19(log)
my prompt : 

sudo will close all file descriptors but 0,1,2 by default and it wants <&0 on a terminal for the password, but you can often sneak by with hijacking stderr. And it's not gone for good, anyway - practically the first thing I do above is reconnect it to ssh's terminal. More realistically you'd have some actual script that you would pass through like $(cat file) where my $PS1 definition is and the rest. It's a lot of annoying indirection, though; it would be better I think to instead handle that stuff as neccessary in remote profiles.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .