mirror of
https://github.com/ipmitool/ipmitool.git
synced 2025-05-11 02:57:22 +00:00
use administrator privilege level by default,
add new "exec" command to execute script, use portable file io
This commit is contained in:
parent
eab79363a2
commit
e05ab344af
@ -75,6 +75,8 @@ int verbose = 0;
|
|||||||
|
|
||||||
extern const struct valstr ipmi_privlvl_vals[];
|
extern const struct valstr ipmi_privlvl_vals[];
|
||||||
extern const struct valstr ipmi_authtype_session_vals[];
|
extern const struct valstr ipmi_authtype_session_vals[];
|
||||||
|
|
||||||
|
static int ipmi_exec_main(struct ipmi_intf * intf, int argc, char ** argv);
|
||||||
static int ipmi_shell_main(struct ipmi_intf * intf, int argc, char ** argv);
|
static int ipmi_shell_main(struct ipmi_intf * intf, int argc, char ** argv);
|
||||||
|
|
||||||
struct ipmi_cmd {
|
struct ipmi_cmd {
|
||||||
@ -83,6 +85,7 @@ struct ipmi_cmd {
|
|||||||
char * desc;
|
char * desc;
|
||||||
} ipmi_cmd_list[] = {
|
} ipmi_cmd_list[] = {
|
||||||
{ ipmi_shell_main, "shell", "Launch interactive IPMI shell" },
|
{ ipmi_shell_main, "shell", "Launch interactive IPMI shell" },
|
||||||
|
{ ipmi_exec_main, "exec", "Run list of commands from file" },
|
||||||
{ ipmi_raw_main, "raw", "Send a RAW IPMI request and print response" },
|
{ ipmi_raw_main, "raw", "Send a RAW IPMI request and print response" },
|
||||||
{ ipmi_lanp_main, "lan", "Configure LAN Channels" },
|
{ ipmi_lanp_main, "lan", "Configure LAN Channels" },
|
||||||
{ ipmi_chassis_main, "chassis", "Get chassis status and set power state" },
|
{ ipmi_chassis_main, "chassis", "Get chassis status and set power state" },
|
||||||
@ -158,31 +161,96 @@ static void usage(void)
|
|||||||
|
|
||||||
static char * ipmi_password_file_read(char * filename)
|
static char * ipmi_password_file_read(char * filename)
|
||||||
{
|
{
|
||||||
int fp;
|
FILE * fp;
|
||||||
char * pass = NULL;
|
char * pass = NULL;
|
||||||
|
int l;
|
||||||
|
|
||||||
fp = ipmi_open_file_read((const char *)filename);
|
fp = ipmi_open_file_read((const char *)filename);
|
||||||
if (fp < 0) {
|
if (!fp) {
|
||||||
printf("Unable to open password file %s\n", filename);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pass = malloc(16);
|
pass = malloc(16);
|
||||||
if (!pass) {
|
if (!pass) {
|
||||||
close(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read in id */
|
/* read in id */
|
||||||
if (read(fp, pass, 16) < 0) {
|
if (fgets(pass, 16, fp) == NULL) {
|
||||||
close(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fp);
|
/* remove trailing whitespace */
|
||||||
|
l = strcspn(pass, " \r\n\t");
|
||||||
|
if (l > 0)
|
||||||
|
pass[l] = '\0';
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
return pass;
|
return pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ipmi_exec_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||||
|
{
|
||||||
|
FILE * fp;
|
||||||
|
char buf[512];
|
||||||
|
char * ptr, * tok;
|
||||||
|
int __argc, i;
|
||||||
|
char * __argv[32];
|
||||||
|
|
||||||
|
if (argc < 1) {
|
||||||
|
printf("Usage: exec <filename>\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fp = ipmi_open_file_read(argv[0]);
|
||||||
|
if (!fp) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets(buf, 512, fp) != NULL) {
|
||||||
|
/* clip off optional comment tail indicated by # */
|
||||||
|
ptr = strchr(buf, '#');
|
||||||
|
if (ptr)
|
||||||
|
*ptr = '\0';
|
||||||
|
else
|
||||||
|
ptr = buf + strlen(buf);
|
||||||
|
|
||||||
|
/* clip off trailing and leading whitespace */
|
||||||
|
ptr--;
|
||||||
|
while (isspace(*ptr) && ptr >= buf)
|
||||||
|
*ptr-- = '\0';
|
||||||
|
ptr = buf;
|
||||||
|
while (isspace(*ptr))
|
||||||
|
ptr++;
|
||||||
|
if (!strlen(ptr))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
__argc = 0;
|
||||||
|
|
||||||
|
/* parse it and make argument list */
|
||||||
|
tok = strtok(ptr, " ");
|
||||||
|
while (tok) {
|
||||||
|
if (__argc < 32)
|
||||||
|
__argv[__argc++] = strdup(tok);
|
||||||
|
tok = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
ipmi_cmd_run(intf, __argv[0], __argc-1, &(__argv[1]));
|
||||||
|
|
||||||
|
for (i=0; i<__argc; i++) {
|
||||||
|
if (__argv[i] != NULL) {
|
||||||
|
free(__argv[i]);
|
||||||
|
__argv[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int ipmi_shell_main(struct ipmi_intf * intf, int argc, char ** argv)
|
static int ipmi_shell_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_READLINE
|
#ifdef HAVE_READLINE
|
||||||
@ -371,10 +439,12 @@ int main(int argc, char ** argv)
|
|||||||
ipmi_intf_session_set_password(intf, password);
|
ipmi_intf_session_set_password(intf, password);
|
||||||
if (port)
|
if (port)
|
||||||
ipmi_intf_session_set_port(intf, port);
|
ipmi_intf_session_set_port(intf, port);
|
||||||
if (privlvl)
|
|
||||||
ipmi_intf_session_set_privlvl(intf, privlvl);
|
|
||||||
if (authtype)
|
if (authtype)
|
||||||
ipmi_intf_session_set_authtype(intf, authtype);
|
ipmi_intf_session_set_authtype(intf, authtype);
|
||||||
|
if (privlvl)
|
||||||
|
ipmi_intf_session_set_privlvl(intf, privlvl);
|
||||||
|
else
|
||||||
|
ipmi_intf_session_set_privlvl(intf, IPMI_SESSION_PRIV_ADMIN);
|
||||||
|
|
||||||
/* setup IPMB local and target address if given */
|
/* setup IPMB local and target address if given */
|
||||||
intf->my_addr = my_addr ? : IPMI_BMC_SLAVE_ADDR;
|
intf->my_addr = my_addr ? : IPMI_BMC_SLAVE_ADDR;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user