I am connecting to linux machine via below code
<code>client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port, username=username, password=password)
</code>
<code>client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port, username=username, password=password)
</code>
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port, username=username, password=password)
I want to run below script.sh file on that server, and receive output of $max_session variable in python program.
<code>#!/bin/bash
cd /
config_file="/etc/ssh/sshd_config"
if [ ! -f "$config_file" ]; then
echo "File $config_file does not exist."
exit 1
fi
max_sessions=$(sudo grep -i "^MaxSessions" "$config_file" | awk '{print $2}')
if [ -z "$max_sessions" ]; then
echo "MaxSessions is not set in the configuration file."
else
echo "$max_sessions"
fi
</code>
<code>#!/bin/bash
cd /
config_file="/etc/ssh/sshd_config"
if [ ! -f "$config_file" ]; then
echo "File $config_file does not exist."
exit 1
fi
max_sessions=$(sudo grep -i "^MaxSessions" "$config_file" | awk '{print $2}')
if [ -z "$max_sessions" ]; then
echo "MaxSessions is not set in the configuration file."
else
echo "$max_sessions"
fi
</code>
#!/bin/bash
cd /
config_file="/etc/ssh/sshd_config"
if [ ! -f "$config_file" ]; then
echo "File $config_file does not exist."
exit 1
fi
max_sessions=$(sudo grep -i "^MaxSessions" "$config_file" | awk '{print $2}')
if [ -z "$max_sessions" ]; then
echo "MaxSessions is not set in the configuration file."
else
echo "$max_sessions"
fi
This script file is in same folder as python program.
4
Note that it could be easier to just use the Paramiko SFTP client to
read /etc/ssh/sshd_config directly from the server and do the “grep”
in Python code.
–@AKX
And took reference from this post for implementation