Ssh 通过Shell脚本登录

Ssh 通过Shell脚本登录,ssh,Ssh,我的问题是,我想从root运行一个脚本,对于这个脚本,我必须通过在命令行上键入“su-”手动使用root登录 我的问题是,我正在执行的脚本只需提示我输入密码,就可以使用root自动登录。帮帮我 :脚本: if [ "$(whoami)" != "root" ]; then echo -e '\E[41m'"\033[1mYou must be root to run this script\033[0m" **su - # at this line I want to login as ro

我的问题是,我想从root运行一个脚本,对于这个脚本,我必须通过在命令行上键入“su-”手动使用root登录

我的问题是,我正在执行的脚本只需提示我输入密码,就可以使用root自动登录。帮帮我

:脚本:

if [ "$(whoami)" != "root" ]; then

echo -e '\E[41m'"\033[1mYou must be root to run this script\033[0m"

**su - # at this line I want to login as root but it is not working**

exit 1

fi

sleep 1

if [ "$(pwd)" != "/root" ]; then

echo -e '\E[41m'"\033[1mCopy this script to /root & then try again\033[0m"

cd /root

exit 1

fi

sleep 1

echo -e '\E[36;45m'"\033[1mDownloading Flash Player from ftp.etilizepak.com\033[0m"

sleep 2

wget -vcxr ftp://soft:S0ft\!@ftp.abc.com/ubuntu/ubuntu\ 12.04/adobe-flashplugin=/install_flash_player_11_linux.i386.tar.gz

cd ftp.abc.com/ubuntu/ubuntu\ 12.04/adobe-flashplugin/

sleep 1

echo -e '\E[42m'"\033[1mUnzipping .tar File...\033[0m"

sleep 1

tar -xzf install_flash_player_11_linux.i386.tar.gz

echo "Unzipping Compeleted"

sleep 2

echo -e '\E[42m'"\033[1mCopying libflashplayer.so\033[0m"

cp libflashplayer.so /usr/lib/mozilla/plugins/

我不确定我是否理解你的问题,但我想你想在你的脚本中以root权限运行一些东西-然后你应该使用“sudo”命令。
if [ "$(whoami)" != "root" ]; then

echo -e '\E[41m'"\033[1mYou must be root to run this script\033[0m"

**su - # at this line I want to login as root but it is not working**

exit 1

fi

sleep 1

if [ "$(pwd)" != "/root" ]; then

echo -e '\E[41m'"\033[1mCopy this script to /root & then try again\033[0m"

cd /root

exit 1

fi

sleep 1

echo -e '\E[36;45m'"\033[1mDownloading Flash Player from ftp.etilizepak.com\033[0m"

sleep 2

wget -vcxr ftp://soft:S0ft\!@ftp.abc.com/ubuntu/ubuntu\ 12.04/adobe-flashplugin=/install_flash_player_11_linux.i386.tar.gz

cd ftp.abc.com/ubuntu/ubuntu\ 12.04/adobe-flashplugin/

sleep 1

echo -e '\E[42m'"\033[1mUnzipping .tar File...\033[0m"

sleep 1

tar -xzf install_flash_player_11_linux.i386.tar.gz

echo "Unzipping Compeleted"

sleep 2

echo -e '\E[42m'"\033[1mCopying libflashplayer.so\033[0m"

cp libflashplayer.so /usr/lib/mozilla/plugins/
您还可以抑制密码提示,这可以在sudoers的“配置文件”中配置

这里有更多信息:


有很多例子,谷歌的“linux sudo示例”之类的东西,你会得到很多关于如何使用su、sudo和sudoers命令的例子。

根据你对我之前答案的评论,我是这样做的:

同一目录中有两个文件:

-rwx------ 1 root root 19 Sep 10 13:04 test2.sh
-rwxrwxrwx 1 root root 29 Sep 10 13:06 test.sh
#!/bin/bash
# put your message here
su -c ./test2.sh
#!/bin/bash
echo You run as:
whoami
# put your code here
> ./test.sh
Password:****
You run as:
root
文件test.sh:

-rwx------ 1 root root 19 Sep 10 13:04 test2.sh
-rwxrwxrwx 1 root root 29 Sep 10 13:06 test.sh
#!/bin/bash
# put your message here
su -c ./test2.sh
#!/bin/bash
echo You run as:
whoami
# put your code here
> ./test.sh
Password:****
You run as:
root
文件test2.sh:

-rwx------ 1 root root 19 Sep 10 13:04 test2.sh
-rwxrwxrwx 1 root root 29 Sep 10 13:06 test.sh
#!/bin/bash
# put your message here
su -c ./test2.sh
#!/bin/bash
echo You run as:
whoami
# put your code here
> ./test.sh
Password:****
You run as:
root
结果:

-rwx------ 1 root root 19 Sep 10 13:04 test2.sh
-rwxrwxrwx 1 root root 29 Sep 10 13:06 test.sh
#!/bin/bash
# put your message here
su -c ./test2.sh
#!/bin/bash
echo You run as:
whoami
# put your code here
> ./test.sh
Password:****
You run as:
root

如果您只想禁止此脚本的密码提示,请将“su-c”替换为“sudo”并根据此处的说明配置sudoers文件:

您应该剪切所有必须作为root运行的行,并将其放入另一个.sh文件中,然后在第一个脚本中写入:sudo/path/to/your/script.sh。这里有一个类似的问题:但是在我的场景中,我不能一直给用户sudo权限,因为这个脚本通常在不同的用户中使用。所以请给我一个合适的解决方案。你可以给sudo这个脚本的权利,请阅读我的第二个答案