【发布时间】:2017-05-27 07:40:34
【问题描述】:
我目前正在执行一项任务,希望我的程序能够读取和更新数据库。数据库本身在 oracle 上运行,由我的大学提供(我相信我有自己的架构?)
现在我可以使用诸如 teraterm 或 putty 之类的程序通过 SSH 连接,一旦我登录,它就会带我进入一个交互式菜单,让我可以选择几个不同的选项。其中之一是贝壳。一旦我选择我可以使用 bash 命令登录到 SQL 部分并使用这些命令:
bash$ export ORACLE_HOME=/opt/oracle/product/client/11.2.0
bash$ export TWO_TASK=SSID
bash$ $ORACLE_HOME/bin/sqlplus
连接到 SQL 数据库。很简单。
但是,我希望能够通过我的程序做到这一点,但事实证明这对我来说很困难。我正在使用SSH.NET,并且可以通过 SSH 进行连接,看起来不错。问题是我无法访问 SQL 部分。当我运行命令时,我相信前两个可以正常工作,但最后一个不能。它似乎无法看到 $ORACLE_HOME 之后的任何内容。当我“回显 $ORACLE_HOME /*”时,它甚至告诉我 /bin 是一个文件夹:
/bin /boot /dev /etc /export /hey.php /home /lib /lib64 /local /lost+found /media /misc
/mnt /opt /proc /root /sbin /selinux /srv /stage /sys /tmp /usr /var
但是,当我运行最后一行代码时,我收到了错误消息:
Error = "bash: /bin/sqlplus: No such file or directory\n"
我不确定是否有更简单的方法来访问 SQL 内容...但是我非常接近使用 SSH.NET 但我就是不明白为什么我不能像我一样打开 SQL 部分在腻子或 teraterm...
任何帮助将不胜感激,感谢您的宝贵时间。
我的实际 C# 代码是这样的:
//Connection information
string user = "SSHusername";
string pass = "password";
string host = "address";
//Set up the SSH connection
using (var client = new SshClient(host, user, pass))
{
//Start the connection
client.Connect();
var output = client.RunCommand("export ORACLE_HOME=/opt/oracle/product/client/11.2.0");
Console.WriteLine(output.Result);
output = client.RunCommand("export TWO_TASK=SSID");
Console.WriteLine(output.Result);
output = client.RunCommand("$ORACLE_HOME/bin/sqlplus");
Console.WriteLine(output.Result);
output = client.RunCommand("username");
Console.WriteLine(output.Result);
output = client.RunCommand("password");
Console.WriteLine(output.Result);
output = client.RunCommand("SELECT * FROM users;");
Console.WriteLine(output.Result);
client.Disconnect();
Console.WriteLine(output.Result);
}
【问题讨论】:
-
看起来您的第三个
RunCommand调用没有获取前两个调用设置的环境变量。您是否检查了每个RunCommand调用是否打开了一个新的shell,从而重置了环境变量?