Search code examples
bashshellaliasparamikoalmalinux

How to use alias command with a paramiko SSHClient connection?


I am trying to execute the following, with no success getting either the 'py3start' or the 'py3test' alias commands recognized - (I introduced the 'py3test' for testing purposes to check if setting the alias explicitly, just prior to using it would make any difference):

command = "echo $SHELL; py3start; alias py3test='source ~/.venv/python3/bin/activate'; py3test"
stdout, stderr, status = connection.exec_command(command=command, timeout=timeout)

Please see the output below: Command stdout as obtained from a debug session.

Please could someone help me figure out why the aliases are not being recognized even though the shell used is /bin/bash, the aliases are defined as below in both the ~/.bashrc and ~/.bash_profile files AND, the output of 'alias -p' includes the said aliases through the same paramiko session (refer last screenshot).

These are the contents of the ~/.bashrc and ~/.bash_profile files on the target VM - where I'm setting the alias py3start.

[root@VM ~]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias py3start='source ~/.venv/python3/bin/activate'


# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@VM ~]# cat ~/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

alias py3start='source ~/.venv/python3/bin/activate'

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

[root@VM ~]#

Please see output below when 'alias -p' is included in the command - clearly, it finds the aliases here, but still does not find them when I try to use them:

command = "echo $SHELL; py3start; alias py3test='source ~/.venv/python3/bin/activate'; alias -p; py3test"
stdout, stderr, status = connection.exec_command(command=command, timeout=timeout)


stderr = {str} 'bash: py3start: command not found\nbash: py3test: command not found\n'

stdout: Command stdout obtained from debug session.


Solution

  • Aliases are only expanded in interactive shells by default. Either execute an interactive shell, or more simply in this case, explicitly enable alias expansion using shopt -s expand_aliases.